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
Assignment to classvar narrows type seemingly too much #17981
So I have a base class with a class attribute of type tuple[str, ...], I expect that deriving from this base class and assigning a concrete value should be fine.
However, mypy narrows the type of Concrete.x to tuple[str], and then if I try and derive from Concrete, tuple[str, str] is incompatible with tuple[str], so I can't assign a two-tuple to the class attribute in Derived.
Now, if the type of xreally weretuple[str], this would be fine. But I think I've explicitly said "it's a tuple of any length", and both ("foo",) and ("foo", "bar") satisfy is-a tuple[str, ...].
Expected Behavior
I expect mypy to use the type annotation I've provided on the base class, and maintain that the type of x is always tuple[str, ...].
Output
main.py:10: error: Incompatible types in assignment (expression has type "tuple[str, str]", base class "Concrete" defined the type as "tuple[str]") [assignment]
main.py:13: note: Revealed type is "builtins.tuple[builtins.str, ...]"
main.py:14: note: Revealed type is "tuple[builtins.str]"
main.py:15: error: Cannot determine type of "x" [has-type]
main.py:15: note: Revealed type is "Any"
Found 2 errors in 1 file (checked 1 source file)
I am going to close this as fixed by #18847. You will still one error at the definition site, so you will need to either add an explicit annotation in the "middle" class, or a # type: ignore[assignment]. The weird "Cannot determine type" errors at call site(s) should be now gone.
Activity
ilevkivskyi commentedon Mar 31, 2025
I am going to close this as fixed by #18847. You will still one error at the definition site, so you will need to either add an explicit annotation in the "middle" class, or a
# type: ignore[assignment]. The weird "Cannot determine type" errors at call site(s) should be now gone.