Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better types for modules #3500

Open
ilevkivskyi opened this issue Jun 6, 2017 · 4 comments
Open

Better types for modules #3500

ilevkivskyi opened this issue Jun 6, 2017 · 4 comments

Comments

@ilevkivskyi
Copy link
Member

ilevkivskyi commented Jun 6, 2017

In situations with dynamically/conditionally imported modules:

if random() > 0.5:
    import mod1
else:
    import mod2 as mod1

mypy can infer only types.ModuleType for the mod1 node. Another (quite widespread) example is something like:

try:
    import fastmath
except ImportError:
    import math as fastmath

In such case all symbol table information for math and fastmath is "lost". We could support (at least internally) some special type that will hold symbol table information and will be allowed to appear in unions (or support structural joins). For example, in the above case type of fastmath would be Union[Module['/path/to/fastmath'], Module['math']]. Then mypy will be able to infer fastmath.sin as Callable[[float], float].

@gvanrossum
Copy link
Member

gvanrossum commented Jun 6, 2017 via email

@ilevkivskyi
Copy link
Member Author

then mypy will assume the type of the first import

Actually mypy assumes the type of the (textually) last encountered import

Honestly that seems reasonable to me.

Except that there is a dozen issues on the tracker about try ... except ImportError and similar.

But if you really want the Module[] notation

I am more interested in having a better internal handling of modules first. Having something conceptually equivalent to "a union of symbol tables" would solve many of the existing issues. Then, depending on experience we could consider adding syntactic support for such types.

@ilevkivskyi
Copy link
Member Author

Related #1153 and #1297 (plus another issues mentioned in the second).

@gvanrossum
Copy link
Member

gvanrossum commented Jun 7, 2017 via email

JukkaL pushed a commit that referenced this issue Jan 30, 2020
Resolves #3494 (Since module types are tracked in #3500)

Following #8187 (comment) and #3494, if I understand correctly and the semantics 
in #3494's example code has been fixed (report error on re-assign Alias = B, the 
remaining work of #3494 is to update the docs, which is the main focus of this PR.

Newly added docs are in common issues and solutions section, with the content mostly adapted from Ivan's example in #3494. And a note point to the docs is also added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants