Skip to content

Fix type variable with values as a supertype#21431

Merged
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:fix-typevar-values-subtype
May 7, 2026
Merged

Fix type variable with values as a supertype#21431
ilevkivskyi merged 3 commits intopython:masterfrom
ilevkivskyi:fix-typevar-values-subtype

Conversation

@ilevkivskyi
Copy link
Copy Markdown
Member

Fixes #21424

Unless I am missing something, this should be allowed.

@ilevkivskyi ilevkivskyi requested review from JukkaL and hauntsaninja May 7, 2026 11:13
@github-actions

This comment has been minimized.

Copy link
Copy Markdown
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense. It's odd that this didn't work in the past. I guess type variables with values are usually in the expanded form.

Comment thread mypy/subtypes.py Outdated

# Cases specific w.r.t. right type are easier to handle before entering the SubtypeVisitor.
# Currently, these include Union types and TypeVarType with values.
if isinstance(right, TypeVarType) and right.values and not isinstance(left, TypeVarType):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: what about moving this after the union type case, since this is likely much less common?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 7, 2026

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@ilevkivskyi ilevkivskyi merged commit d0b3fb3 into python:master May 7, 2026
24 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-typevar-values-subtype branch May 7, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[2.0 regression] TypeVar with constrains Sub and Super does not accept Sub as a default value

2 participants