python / python/mypy

Treat widening type variables consistently

Open
#18,456 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

Bug Report

Currently, mypy allows widening type variables without an error, then errors (sometimes, see below) at usage time. This seems backwards to me. I'm opening this based on a minimization from @sterliakov in https://github.com/python/mypy/pull/18451.

To Reproduce

There's several interesting snippets:

import typing

IntT = typing.TypeVar("IntT", bound=int)
StrT = typing.TypeVar("StrT", bound=str)
AnyT = typing.TypeVar("AnyT", bound=typing.Any)

class IntC(typing.Generic[IntT]): ...
class StrC(typing.Generic[StrT]): ...

AnyC = IntC[AnyT] | StrC[AnyT]

def run(_: AnyC[StrT]) -> StrT: ...  # E: Type argument "StrT" of "IntC" must be a subtype of "int"

In this case, I would expect an error on the AnyC type alias definition, and maybe at usage time. What about other methods of widening type variables?

import typing

IntT = typing.TypeVar("IntT", bound=int)
StrT = typing.TypeVar("StrT", bound=str)
AnyT = typing.TypeVar("AnyT", bound=typing.Any)

class IntC(typing.Generic[IntT]): ...

class AnyC(IntC[AnyT]):  # ! no error
    pass

def f(x: StrT) -> StrT:
    y: AnyC[StrT]  # !? no error
    return x

This version doesn't error at all! What I would expect is the behavior achieved by using object plus a class to widen:

import typing

IntT = typing.TypeVar("IntT", bound=int)
StrT = typing.TypeVar("StrT", bound=str)
AnyT = typing.TypeVar("AnyT", bound=object)

class IntC(typing.Generic[IntT]): ...
class StrC(typing.Generic[StrT]): ...

class AnyC1(IntC[AnyT]):  # E: Type argument "AnyT" of "IntC" must be a subtype of "int"  
    pass

AnyC2 = IntC[AnyT] | StrC[AnyT]  # ! no error

def f(x: StrT) -> StrT:
    y1: AnyC1[StrT]  # no error, this is fine
    y2: AnyC2[StrT]  # E: Type argument "StrT" of "IntC" must be a subtype of "int"
    return x

Expected Behavior

Consistency.

Actual Behavior

To summarize the above

method of widening bound=object bound=Any
type alias usage error usage error
class definition error no errors (!)

Your Environment

Everything was double checked in mypy playground.

  • Mypy version used: v1.14.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.12

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 reproducing the reported snippets in the mypy playground with mypy v1.14.1, no command-line flags, and compare the bound=object and bound=Any cases for type aliases and classes. Done means widening type variables produces consistent diagnostics at the same stage across these examples, matching the expected behavior described in the issue.

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
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.