python / python/mypy

Using TypeVar as typing for a function argument disables the --strict-equality check

Open
#16,477 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

When using a TypeVar as typing for a function parameter, the --strict-equality checks are no longer executed.

To Reproduce

from typing import TypeVar

T = TypeVar("T", int, str)


def a(x: T) -> None:
    1 == "a"


def b(x: int | str) -> None:
    1 == "a"

a(1)
b(1)

Playground: https://mypy-play.net/?mypy=master&python=3.10&flags=strict&gist=60fa0e9e47a0ed5683357b1fa2611477

Expected Behavior

I would expect both functions a() and b() to raise the comparison-overlap error.

Actual Behavior

Only function b() where there is no TypeVar used raises the comparison-overlap error.

Note that other, standard, mypy checks are executed and flagged in both functions.

Your Environment

  • Mypy version used: Checked with multiple (1.4.1, 1.7.0, master branch)
  • Mypy command-line flags: --strict or --strict-equality
  • Mypy configuration options from mypy.ini (and other config files): -
  • Python version used: 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

Run the provided reproducer with --strict-equality and compare diagnostics for functions a() and b(). Trace mypy’s strict-equality handling for constrained TypeVar parameters; done means both comparisons report comparison-overlap, with regression coverage confirming the behavior.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.