diff --git a/mypy/stubgenc.py b/mypy/stubgenc.py index 6cd5135839b4..d0df9309e495 100755 --- a/mypy/stubgenc.py +++ b/mypy/stubgenc.py @@ -11,6 +11,7 @@ import inspect import keyword import os.path +from contextlib import suppress from types import FunctionType, ModuleType from typing import Any, Callable, Mapping @@ -497,6 +498,8 @@ def is_skipped_attribute(self, attr: str) -> bool: "__firstlineno__", "__static_attributes__", "__annotate__", + "__orig_bases__", + "__parameters__", ) or attr in self.IGNORED_DUNDERS or is_pybind_skipped_attribute(attr) # For pickling @@ -875,6 +878,10 @@ def generate_class_stub(self, class_name: str, cls: type, output: list[str]) -> self.dedent() bases = self.get_base_types(cls) + if inline_generic != "": + # Removes typing.Generic form bases if it exists for python3.12 inline generics + with suppress(ValueError): + bases.remove("typing.Generic") if bases: bases_str = "(%s)" % ", ".join(bases) else: diff --git a/mypy/test/teststubgen.py b/mypy/test/teststubgen.py index 4dc490926957..325c57d8ce21 100644 --- a/mypy/test/teststubgen.py +++ b/mypy/test/teststubgen.py @@ -948,6 +948,14 @@ class TestClass: def test_generic_class(self) -> None: exec("class Test[A]: ...") + class TestClass[A]: ... + + output: list[str] = [] + mod = ModuleType("module", "") + gen = InspectionStubGenerator(mod.__name__, known_modules=[mod.__name__], module=mod) + gen.generate_class_stub("C", TestClass, output) + assert_equal(output, ["class C[A]: ..."]) + @unittest.skipIf(sys.version_info < (3, 12), "Inline Generics not supported before Python3.12") def test_inline_generic_function(self) -> None: