Skip to content

stubtest: False-positive errors for type[T] attributes with non-type metaclasses #13316

@AlexWaygood

Description

@AlexWaygood

Bug Report

Given a stub like so:

from abc import ABCMeta
class Y(metaclass=ABCMeta): ...
class Z:
    foo: type[Y]

And a runtime like so:

from abc import ABCMeta
class Y(metaclass=ABCMeta): ...
class Z:
    foo = Y

Stubtest will issue the following complaint:

error: Z.foo variable differs from runtime type abc.ABCMeta

This error can be reproduced with any custom metaclass; it's not just ABCMeta that triggers the bug:

# STUB
class Meta(type): ...
class Y(metaclass=Meta): ...
class Z:
    foo: type[Y]

# RUNTIME:
class Meta(type): ...
class Y(metaclass=Meta): ...
class Z:
    foo = Y

To Reproduce

  1. Check out a local clone of mypy
  2. Activate a virtual environment with an editable install of mypy
  3. Apply this diff:
diff --git a/mypy/test/teststubtest.py b/mypy/test/teststubtest.py
index 2adbfaac2..60ddebc7f 100644
--- a/mypy/test/teststubtest.py
+++ b/mypy/test/teststubtest.py
@@ -208,6 +208,21 @@ class StubtestUnit(unittest.TestCase):
             """,
             error="X.mistyped_var",
         )
+        yield Case(
+            stub="""
+            class Meta(type): ...
+            class Y(metaclass=Meta): ...
+            class Z:
+                foo: type[Y]
+            """,
+            runtime="""
+            class Meta(type): ...
+            class Y(metaclass=Meta): ...
+            class Z:
+                foo = Y
+            """,
+            error=None
+        )
  1. Run pytest ./mypy/test/teststubtest.py

Expected Behavior

No error should be emitted.

Actual Behavior

An error was emitted.

Cc. @hauntsaninja

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions