Issue 30518: Import type aliases from another module (original) (raw)

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:47 admin set github: 74703
2017-06-03 16:14:09 Paragape set status: open -> closedresolution: not a bugmessages: + stage: resolved
2017-06-02 17:55:08 levkivskyi set nosy: + levkivskyimessages: +
2017-05-31 04:38:48 JelleZijlstra set nosy: + JelleZijlstramessages: +
2017-05-31 03:15:33 Paragape set messages: +
2017-05-31 02:54:11 Paragape create