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
mypy fails to proceed sorting of the empty list, considering it's a list[object], see the reproduce script and the output
To Reproduce
a: list[str] |list[int] = []
a=sorted(a)
Expected Behavior
There shouldn't be errors
Actual Behavior
> mypy .
/tmp/test.py:2: error: Incompatible types in assignment (expression has type "list[SupportsDunderLT[Any] | SupportsDunderGT[Any]]", variable has type "list[str] | list[int]") [assignment]
Your Environment
Mypy version used: mypy 1.14.0 (compiled: no)
Mypy command-line flags: none
Mypy configuration options from mypy.ini (and other config files): none
Felixoid
changed the title
mypy throws assignment error in dataclasses.__post_init
mypy throws wrong assignment error in dataclasses.__post_initFeb 25, 2025
a: list[str] |list[int] = []
b=list(a)
reveal_type(b) # N: Revealed type is "builtins.list[builtins.object]"# ^ you want `b` to be `list[str] | list[int]`, but mypy doesn't do that
I think this not something mypy can fix. I think the issue is that list is taking a Sequence[T] (guessing), so mypy says T = object -- at best it would be T = str | int, so join-v-union doesn't help -- and then returns list[T] so list[object]. AFAIK there's no way for mypy to realize it should process the union elements one at a time. You could use type var values if you care quite a bit.
... I wonder what mypy could do. Maybe someone could prototype checking union items seperately but that sounds inefficient.
(if you want to reopen this just let me know and I'll switch this to being a feature request)
Yeah, I'd like to convert this issue to a feature request. I feel it would be beneficial to have this addressed.
Felixoid
changed the title
mypy throws wrong assignment error in dataclasses.__post_init
mypy throws assignment error for a list created from an empty list
Feb 28, 2025
Bug Report
mypy fails to proceed sorting of the empty list, considering it's a
list[object]
, see the reproduce script and the outputTo Reproduce
Expected Behavior
There shouldn't be errors
Actual Behavior
Your Environment
mypy 1.14.0 (compiled: no)
mypy.ini
(and other config files): noneIn the scope of ClickHouse/ClickHouse#76679
The text was updated successfully, but these errors were encountered: