Open
Description
Bug Report
Mypy allows the return value of list()
to be upcasted. eg. list[int]
-> list[object]
(or list[int | None]
). Mypy does not permit the same behaviour with the return value of sorted()
. Like list()
, sorted()
is a builtin, creates a new list, and does not retain a reference to the returned value. Mypy should allow the return value of sorted()
to be upcast, because it is safe to do so.
To Reproduce
# file: foo.py
ints: list[int] = [1,2,3]
objects: list[object] = sorted(ints) # error!
Command line:
python -m mypy foo.py
Expected Behavior
That no error should be reported, much like if list()
were used instead of sorted()
. ie:
ints: list[int] = [1, 2, 3]
objects: list[object] = list(ints)
# undesirable work around
objects2: list[object] = list(sorted(ints))
Actual Behavior
foo.py:11: error: Incompatible types in assignment (expression has type "list[int]", variable has type "list[object]") [assignment]
foo.py:11: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
foo.py:11: note: Consider using "Sequence" instead, which is covariant
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 1.10.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini
(and other config files): none - Python version used: 3.12.0
Metadata
Metadata
Assignees
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
yangdanny97 commentedon Jun 2, 2024
speculating here, but I don't think there's any special handling for
sorted()
right now so it would just be checked like a normal function looking at its signature in typeshed.Maybe there's a way to change the type variables in the type stubs s.t. it gives the desired behavior
hauntsaninja commentedon Nov 10, 2024
Yeah, there's no special handling for
sorted
.From a type perspective,
sorted
could have the behaviour of the identity function, in which case upcasting is trivially unsafe:Options I see are a) special hack for
sorted
, b) some big type system addition to track mutability. I don't know that mypy would accept a) and I don't think the mypy issue tracker is the right place to discuss b).For workarounds, this is a reasonable place to use
cast
.yangdanny97 commentedon Nov 10, 2024
Since it's a really commonly-used builtin function, the developer friction reduced might justify adding a special case.
Fwiw, Pyre added a special case to handle
sorted
in September: facebook/pyre-check@1a4e09d