Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mypy throws assignment error for a list created from an empty list #18735

Open
Felixoid opened this issue Feb 25, 2025 · 2 comments
Open

mypy throws assignment error for a list created from an empty list #18735

Felixoid opened this issue Feb 25, 2025 · 2 comments

Comments

@Felixoid
Copy link
Contributor

Felixoid commented Feb 25, 2025

Bug Report

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
  • Python version used: 3.13.2

In the scope of ClickHouse/ClickHouse#76679

@Felixoid Felixoid added the bug mypy got something wrong label Feb 25, 2025
@Felixoid Felixoid changed the title mypy throws assignment error in dataclasses.__post_init mypy throws wrong assignment error in dataclasses.__post_init Feb 25, 2025
@A5rocks
Copy link
Collaborator

A5rocks commented Feb 28, 2025

This ultimately is equivalent to:

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)

@A5rocks A5rocks closed this as not planned Won't fix, can't repro, duplicate, stale Feb 28, 2025
@Felixoid
Copy link
Contributor Author

So, the minimal code with issue is the following:

a: list[str] | list[int] = []
a = sorted(a)

Yeah, I'd like to convert this issue to a feature request. I feel it would be beneficial to have this addressed.

@Felixoid 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
@A5rocks A5rocks reopened this Feb 28, 2025
@A5rocks A5rocks added feature topic-type-variables and removed bug mypy got something wrong labels Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants