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
fromtypingimport (
Generic,
TypeVar,
)
_MyType=TypeVar("_MyType", str, float)
classMyGeneric(Generic[_MyType]):
def__init__(self, attr: int) ->None:
ls= [0]
self.attr=attr# mypy says: error: Need type annotation for 'attr'if (a:=sum(ls)): # mypy says: error: Cannot determine type of 'ls'passreveal_type(ls) # mypy says: note: Revealed type is 'builtins.list[builtins.int*]'
The title of this bug report was a bit vague because I didn't know how to concisely describe the necessary set of conditions for this bug. Here are those conditions:
ls is not manually annotated.
The walrus operator is used.
The block is inside a generic class.
_MyType is a TypeVar that explicitly enumerates all the possible types (as opposed to being bound=... or just a free TypeVar)
Environment
mypy 0.800+dev.40fd841a005936d95e7c351435f1b7b7b4bdd43d
No mypy.ini config, no cmdline flags.
Python 3.8.3
The text was updated successfully, but these errors were encountered:
I believe I've ran into this same bug when converting one of my classes to a generic class as part of a rewrite. Walrus operators + generic classes seem to combine to break mypy somehow. You can find information on gitter here: https://gitter.im/python/typing?at=6158b2c638377967f42dba74
I ran into this when converting the class from using str to being a generic class of AnyStr. I've left it as str for now before I decide what to do when rewriting the class.
This allows supporting all IO types. This was much more complicated than
it seems since this transition ran into an extremely odd bug in mypy.
The gist is that explicitly bound generic classes and walruses operators
break mypy somehow. See python/mypy#9743
Bug Report
The title of this bug report was a bit vague because I didn't know how to concisely describe the necessary set of conditions for this bug. Here are those conditions:
ls
is not manually annotated._MyType
is a TypeVar that explicitly enumerates all the possible types (as opposed to beingbound=...
or just a free TypeVar)Environment
mypy 0.800+dev.40fd841a005936d95e7c351435f1b7b7b4bdd43d
No mypy.ini config, no cmdline flags.
Python 3.8.3
The text was updated successfully, but these errors were encountered: