You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When MyPy encounters star (*) imports it considers every symbol defined in the given module as being imported even if an __all__ attribute is present on the module.
For example, say you have mypkg with two files, main.py:
Running "mypy mypkg/app.py" issues the following error: "mypkg/app.py:2: error: Name 'datetime' already defined", though datetime should not be re-imported to the app module.
Thanks for the report! For whoever wants to tackle this, apparently mypy ignore the value of __all__ when computing the effect of from ... import *. That feels like it shouldn't be too hard to fix.
The initializer is not accessible via SymbolTableNode. One way would be to hook into the semantic analysis of assignment statements (special case __all__ at module top level).
My guess is to make it an error if __all__ has any type other than Sequence[str], and, if __all__ is assigned to multiple times, then just try to merge them all or something. So this would be an error:
__all__=1# ERROR: type of __all__ must be Sequence[str]
and this would attempt to make __all__['x', 'y']:
ifTrue:
__all__= ['x']
else:
__all__= ['y']
Any attempts to mutate it via append and such would be ignored (at least for now). How does all that sound?
Activity
gvanrossum commentedon Jun 2, 2016
Thanks for the report! For whoever wants to tackle this, apparently mypy ignore the value of
__all__when computing the effect offrom ... import *. That feels like it shouldn't be too hard to fix.refi64 commentedon Jun 2, 2016
Was trying to tackle this. Given a
SymbolTableNode, is it possible to get the variable's initializer somehow? Can't seem to figure out anything...JukkaL commentedon Jun 2, 2016
The initializer is not accessible via
SymbolTableNode. One way would be to hook into the semantic analysis of assignment statements (special case__all__at module top level).gvanrossum commentedon Jun 2, 2016
refi64 commentedon Jun 3, 2016
How should messes like this be handled:
My guess is to make it an error if
__all__has any type other thanSequence[str], and, if__all__is assigned to multiple times, then just try to merge them all or something. So this would be an error:and this would attempt to make
__all__['x', 'y']:Any attempts to mutate it via
appendand such would be ignored (at least for now). How does all that sound?gvanrossum commentedon Jun 3, 2016
Check the stdlib for patterns. IIRC asyncio has dynamic all.
But yes, it should be Sequence of stress (or Any).
--Guido (mobile)
Acknowledge __all__ (fixes python#1606)