Closed
Description
Bug Report
from typing import (
Generic,
TypeVar,
)
_MyType = TypeVar("_MyType", str, float)
class MyGeneric(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'
pass
reveal_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 beingbound=...
or just a free TypeVar)
Environment
mypy 0.800+dev.40fd841a005936d95e7c351435f1b7b7b4bdd43d
No mypy.ini config, no cmdline flags.
Python 3.8.3