python / python/mypy

Regression with covariant type through built-in function

Open
#16,476 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-type-variables
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

Regression from 1.6.1 to 1.7: when passing a simple covariant type through a builtin (like sorted) the return type becomes a wider generic based type.

To Reproduce

def foo(a: list[int] | list[str]) -> list[int] | list[str]:
    return sorted(a)

Expected Behavior

running mypy on the simple code above produces no error in 1.6.1.

Actual Behavior

error: Incompatible return value type (got "list[SupportsDunderLT[Any] | SupportsDunderGT[Any]]", expected "list[int] | list[str]")  [return-value]

Your Environment

Regression from 1.6.1 to 1.7.0. Seen on multiple python versions (3.10, 3.11)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the provided reproducer against mypy 1.6.1 and 1.7.0 on Python 3.10 or 3.11 to confirm the regression. Trace handling of the built-in sorted call and covariant union inference, then add a regression test showing that the result is accepted as list[int] | list[str].

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.