Skip to content

[regression] isinstance complains about parametrized generics with any use of TypeAlias #18488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Tracked by #18487
wesleywright opened this issue Jan 19, 2025 · 2 comments · Fixed by #18512
Closed
Tracked by #18487
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-type-alias TypeAlias and other type alias issues

Comments

@wesleywright
Copy link
Collaborator

Bug Report

Any attempt to call isinstance with a variable annotated as a TypeAlias seems to result in the error message error: Parameterized generics cannot be used with class or instance checks, even if the aliased type is non-generic. mypy_primer points at #18173 as the breaking commit, but that PR seems to have been intended to deal with type aliases of generic types; it seems that we're treating all TypeAlias-based aliases as generic types, which AFAICT is broader than intended.

To Reproduce

MyPy playground link, using current master

from typing import Any, TypeAlias

class Foo:
    pass

Alias: TypeAlias = Foo

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

Expected Behavior

MyPy finds no errors, matching the runtime behavior. Pyright and mypy 1.4 both find no errors in my minimal example.

Actual Behavior

Per the playground link:

main.py:9: error: Parameterized generics cannot be used with class or instance checks  [misc]
Found 1 error in 1 file (checked 1 source file)

Your Environment

I found this when dogfooding mypy master commit c4e2eb7 against Dropbox's internal Python repo, but mypy_primer also reproduces the same thing, as does mypy playground.

@wesleywright wesleywright added the bug mypy got something wrong label Jan 19, 2025
@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Jan 19, 2025

cc @brianschubert in case you have time to take a look!

We could do e.g. the following to unblock

diff --git a/mypy/semanal.py b/mypy/semanal.py
index 034d8fb28..a92227d79 100644
--- a/mypy/semanal.py
+++ b/mypy/semanal.py
@@ -4022,7 +4022,7 @@ class SemanticAnalyzer(
             and not res.args
             and not empty_tuple_index
             and not pep_695
-            and not pep_613
+            # and not pep_613
         )
         if isinstance(res, ProperType) and isinstance(res, Instance):
             if not validate_instance(res, self.fail, empty_tuple_index):

Would have to disable some of the new test cases, but the spec is maybe a little aggressive in disallowing that type alias

@sterliakov sterliakov added topic-type-alias TypeAlias and other type alias issues topic-runtime-semantics mypy doesn't model runtime semantics correctly labels Jan 20, 2025
hauntsaninja added a commit to hauntsaninja/mypy that referenced this issue Jan 23, 2025
This is a partial revert of python#18173 to unblock the 1.15 release

Fixes python#18488
@hauntsaninja
Copy link
Collaborator

Maybe we do this to unblock the release? #18512

wesleywright pushed a commit that referenced this issue Jan 23, 2025
This is a partial revert of #18173 to unblock the 1.15 release

Fixes #18488
x612skm pushed a commit to x612skm/mypy-dev that referenced this issue Feb 24, 2025
This is a partial revert of python#18173 to unblock the 1.15 release

Fixes python#18488
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-runtime-semantics mypy doesn't model runtime semantics correctly topic-type-alias TypeAlias and other type alias issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants