Skip to content

return value of sorted() cannot be upcast #17296

Open
@Dunes

Description

@Dunes

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

Activity

yangdanny97

yangdanny97 commented on Jun 2, 2024

@yangdanny97
Contributor

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

hauntsaninja commented on Nov 10, 2024

@hauntsaninja
Collaborator

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:

def fake_sorted[T](x: T) -> T:
    return x

ints: list[int] = [1,2,3]

objects: list[object] = fake_sorted(ints)  # must error
objects.append(object())

ints[-1] + 1  # boom

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

yangdanny97 commented on Nov 10, 2024

@yangdanny97
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-type-contextType context / bidirectional inference

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Dunes@hauntsaninja@yangdanny97@brianschubert

        Issue actions

          return value of sorted() cannot be upcast · Issue #17296 · python/mypy