-
-
Notifications
You must be signed in to change notification settings - 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
x
has a union type, infer only union item types with attributeinitialize
after thehasattr
check. So if type ofx
isUnion[str, X]
andX
hasinitialize
, infer type ofx
to beX
in the if body. - Allow specifying "potentially undefined" attributes in types. Accessing these requires a
hasattr
check (a little likeOptional[...]
requiring something like anis not None
check). 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 52 more
Metadata
Metadata
Assignees
Labels
false-positivemypy gave an error on correct codemypy gave an error on correct codeneeds discussionpriority-1-normal