python / python/mypy

`list.sort()` fails to detect type issue

Open
#21,646 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-overloads topic-self-types
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

This code is not failing under mypy:

[2, None].sort()

Interestingly, it fails on ty:

error[invalid-argument-type]: Argument to bound method `list.sort` is incorrect
 --> toto.py:1:1
  |
1 | [None, 2].sort()
  | ^^^^^^^^^^^^^^^^ Argument type `None | int` does not satisfy upper bound `SupportsDunderLT[Any] | SupportsDunderGT[Any]` of type variable `SupportsRichComparisonT`
  |
info: Type variable defined here
   --> stdlib/_typeshed/__init__.pyi:107:1
    |
107 | SupportsRichComparisonT = TypeVar("SupportsRichComparisonT", bound=SupportsRichComparison)  # noqa: Y001
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |

Found 1 diagnostic

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

Reproduce the reported difference between mypy and ty with [2, None].sort(), then inspect the list.sort typing and the referenced stdlib/_typeshed/__init__.pyi definition. Determine the expected diagnostic and add regression coverage for the case, with the behavior made consistent with that expectation.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.