python / python/mypy

`isinstance` doesn't work with `TypeAlias` for classes inherited from `NamedTuple`

Open
#21,100 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-named-tuple topic-type-alias
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

isinstance throws Parameterized generics cannot be used with class or instance checks [misc] for TypeAlias for a class inherited from NamedTuple. This is related to https://github.com/python/mypy/issues/18488

To Reproduce

from typing import Any, TypeAlias, NamedTuple

class Foo(NamedTuple):
    pass

Alias: TypeAlias = Foo

def is_foo(x: Any) -> bool:
    return isinstance(x, Alias)

https://mypy-play.net//?mypy=latest&python=3.13&gist=91206dbc88693a2e88ec03e79a8d8235

Expected Behavior
Should not throw.

Actual Behavior
Throws Parameterized generics cannot be used with class or instance checks [misc]

  • Mypy version used: 1.19.1
  • Mypy command-line flags: mypy . --strict
  • Mypy configuration options from pyproject.toml:
[tool.mypy]
strict = true
allow_redefinition = true
allow_redefinition_new = true
local_partial_types = true
follow_untyped_imports = true
  • Python version used: 3.13.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

Start with the provided NamedTuple, TypeAlias, and isinstance reproduction in mypy-play, using mypy 1.19.1 with Python 3.13 and strict settings. Trace the diagnostic that reports parameterized generics in class or instance checks, then confirm the fix by rerunning the reproduction and verifying that no error is reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.