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
{{ message }}
Repository navigation
types.ModuleType should define __getattr__ that returns Any #2147
It seems that this change makes mypy thinks that any module has any attribute, while what we want is that specific module have a known set of attributes and only variables that are just typed as ModuleType have any attribute. This will likely require changes to mypy itself.
Hm, yes, it looks like we have three tests that explicitly check for Module has no attribute "y". So probably this will need some adjustments in mypy (like not using __getattr__ when types.ModuleType is itself a fallback).
TBH, I already forgot why I wanted this :-) Most likely this is because of some limitations of type inference in mypy, when a list of modules is inferred with too wide type, or when people want to patch modules in a function.
However, if we will allow modules as subtypes of protocols (I promised to make a PR and it is still on my TODO list) this will solve all such problems, so maybe we can just abandon this idea? (I vote for protocols.)
Yes, then the protocol solution (which doesn't work yet) would be to just give an explicit annotation to mods:
classFoo(Protocol):
deffoo(self) ->None: ...
importa, b, c# It so happens that each defines a method foo()mods: List[Foo] = [a, b, c] # OKformodinmods:
mod.foo()
Activity
add __getattr__ to ModuleType
JelleZijlstra commentedon Jun 12, 2018
I am trying this in master...JelleZijlstra:modulegetattr, but this produces some errors in mypy's test suite: https://travis-ci.org/JelleZijlstra/typeshed/jobs/391103559.
It seems that this change makes mypy thinks that any module has any attribute, while what we want is that specific module have a known set of attributes and only variables that are just typed as
ModuleTypehave any attribute. This will likely require changes to mypy itself.ilevkivskyi commentedon Jun 12, 2018
Hm, yes, it looks like we have three tests that explicitly check for
Module has no attribute "y". So probably this will need some adjustments in mypy (like not using__getattr__whentypes.ModuleTypeis itself a fallback).gvanrossum commentedon Jun 12, 2018
ilevkivskyi commentedon Jun 12, 2018
TBH, I already forgot why I wanted this :-) Most likely this is because of some limitations of type inference in mypy, when a list of modules is inferred with too wide type, or when people want to patch modules in a function.
However, if we will allow modules as subtypes of protocols (I promised to make a PR and it is still on my TODO list) this will solve all such problems, so maybe we can just abandon this idea? (I vote for protocols.)
gvanrossum commentedon Jun 12, 2018
A typical example would be
IIRC there was a tracker issue (maybe several) about this and I also believe I've seen it in Dropbox internal code.
ilevkivskyi commentedon Jun 13, 2018
Yes, then the protocol solution (which doesn't work yet) would be to just give an explicit annotation to
mods:JelleZijlstra commentedon Jun 14, 2018
So it sounds like we shouldn't make any change in typeshed.