Skip to content

Unwrap type[...] before intersection narrowing for TypeIs/TypeGuard#21678

Open
ygale wants to merge 2 commits into
python:masterfrom
ygale:fix-typeis-typetype-unwrap
Open

Unwrap type[...] before intersection narrowing for TypeIs/TypeGuard#21678
ygale wants to merge 2 commits into
python:masterfrom
ygale:fix-typeis-typetype-unwrap

Conversation

@ygale

@ygale ygale commented Jul 6, 2026

Copy link
Copy Markdown

Fixes #21677

issubclass()/isinstance() narrowing unwraps type[...] before computing an ad-hoc intersection and rewraps the result. TypeIs/TypeGuard narrowing never did this, so a TypeIs[type[M]] guard on a type[A] expression silently failed to narrow.

LLM Disclosure

I was assisted by Claude (Sonnet 5), under my close guidance and supervision. The work is mine.

ygale and others added 2 commits July 6, 2026 09:37
issubclass()/isinstance() narrowing unwraps type[...] before computing
an ad-hoc intersection and rewraps the result. TypeIs/TypeGuard
narrowing never did this, so a TypeIs[type[M]] guard on a type[A]
expression silently failed to narrow.
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

koda-validate (https://github.com/keithasaurus/koda-validate)
+ koda_validate/dataclasses.py:117: error: Redundant cast to "type[_DCT]"  [redundant-cast]

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.

TypeIs and TypeGuard fail to narrow type[C]

1 participant