-
-
Couldn't load subscription status.
- Fork 3k
Closed
Labels
false-positivemypy gave an error on correct codemypy gave an error on correct codeneeds discussionpriority-1-normal
Description
I just encountered code like this:
if hasattr(x, 'initialize'):
x.initialize()
The type of x was an ABC but it doesn't include initialize. This can be easily worked around by rewriting it like this:
if hasattr(x, 'initialize'):
cast(Any, x).initialize()
However, mypy could do better, plausibly. For example:
- If
xhas a union type, infer only union item types with attributeinitializeafter thehasattrcheck. So if type ofxisUnion[str, X]andXhasinitialize, infer type ofxto beXin the if body. - Allow specifying "potentially undefined" attributes in types. Accessing these requires a
hasattrcheck (a little likeOptional[...]requiring something like anis not Nonecheck). Not sure what the syntax for this would be like. It would be nice to support these in ABCs as well.
dmfigol, andreymal, PeterJCLaw, Evpok, nickpresta and 53 more
Metadata
Metadata
Assignees
Labels
false-positivemypy gave an error on correct codemypy gave an error on correct codeneeds discussionpriority-1-normal