python / python/mypy

Lint against cases where user likely intended to use constrained type variables

Open
#20,318 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

*Memo:

  • mypy test.py
  • mypy 1.18.2
  • Python 3.14.0
  • Windows 11

Instantiating the object of the class with the wrong syntax setting constraint types to the 2nd and 3rd arguments gets error properly as shown below:

                  # ↓↓↓↓↓↓↓↓↓↓ Wrong syntax
class MyCls[T: int, float, str]:
    num: T
    def __init__(self, x: T) -> None:
        self.num = x

# Instantiation
MyCls[int](100)
# Error

error: Type application has too few types

But only defining the class with the wrong syntax doesn't get the error as shown below so the error should occur:

                  # ↓↓↓↓↓↓↓↓↓↓ Wrong syntax
class MyCls[T: int, float, str]:
    num: T
    def __init__(self, x: T) -> None:
        self.num = x
# No error

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 minimal Python 3.14 example with mypy 1.18.2 and compare diagnostics for the declaration alone versus the type application. Trace the generic type-variable and constrained-type analysis entry point, then add coverage so the standalone declaration reports the intended diagnostic without regressing valid syntax.

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.