This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Import type aliases from another module
Type: enhancement Stage: resolved
Components: Build Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: JelleZijlstra, Paragape, levkivskyi
Priority: normal Keywords:

Created on 2017-05-31 02:54 by Paragape, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (5)
msg294807 - (view) Author: John Jackson (Paragape) Date: 2017-05-31 02:54
I have a 'base' module where I define some type aliases, such as:

    from typing import List, Tuple
    Block = [int, Tuple[int]]
    Blocks = List[Block]
    Tags = List[str]

I would like to import these aliases into other modules so that the 'base' module provides the definitive alias.

Currently, if I attempt to import the type aliases using:

    from base_module import Tags, Blocks

Pycharm shows no error, but when I attempt to execute the code I get the error:

    ImportError: cannot import name 'Tags'

I see that there has been some discussion related to this in 2015, but I can't find any documentation saying that something like this has been implemented.
msg294809 - (view) Author: John Jackson (Paragape) Date: 2017-05-31 03:15
I just found out that the problem is even worse.  While PyCharm accepts the syntax below, I still can't compile 'Blocks'.
msg294811 - (view) Author: Jelle Zijlstra (JelleZijlstra) * (Python committer) Date: 2017-05-31 04:38
This is likely an issue with the setup of your project, not with type aliases. You haven't given enough information to tell me what the real problem is.

I'm not sure what you mean by "I still can't compile 'Blocks'".
msg295046 - (view) Author: Ivan Levkivskyi (levkivskyi) * (Python committer) Date: 2017-06-02 17:55
>     Block = [int, Tuple[int]]
>     Blocks = List[Block]

These are both invalid type aliases (I have no idea why PyCharm does not flag them, you could report this at PyCharm issue tracker). I am not sure what exactly you want. If you want a list of either integers or tuples of integers, then you should write for example:

Block = Union[int, Tuple[int, ...]]
Blocks = List[Block]

Concerning import, this is definitely not a problem with aliases. What I have noticed is that you write "I have a 'base' module ..." and then "from base_module import ...", if you have a module named base.py, then you should write:

from base import Blocks, Tags

Or maybe you just have an import cycle...
msg295085 - (view) Author: John Jackson (Paragape) Date: 2017-06-03 16:14
Thanks for your response!  Yes, the problem was a circular definition.  I still have much to learn about Python...
History
Date User Action Args
2022-04-11 14:58:47adminsetgithub: 74703
2017-06-03 16:14:09Paragapesetstatus: open -> closed
resolution: not a bug
messages: + msg295085

stage: resolved
2017-06-02 17:55:08levkivskyisetnosy: + levkivskyi
messages: + msg295046
2017-05-31 04:38:48JelleZijlstrasetnosy: + JelleZijlstra
messages: + msg294811
2017-05-31 03:15:33Paragapesetmessages: + msg294809
2017-05-31 02:54:11Paragapecreate