Regression with covariant type through built-in function
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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