Skip to content

Commit

Permalink
stubgen: Do not remove Generic from base classes (#15316)
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdanal authored May 27, 2023
1 parent da5dffc commit 7fe1fdd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
10 changes: 2 additions & 8 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -900,13 +900,7 @@ def visit_class_def(self, o: ClassDef) -> None:
if isinstance(o.metaclass, (NameExpr, MemberExpr)):
meta = o.metaclass.accept(AliasPrinter(self))
base_types.append("metaclass=" + meta)
elif self.analyzed and o.info.is_protocol:
type_str = "Protocol"
if o.info.type_vars:
type_str += f'[{", ".join(o.info.type_vars)}]'
base_types.append(type_str)
self.add_typing_import("Protocol")
elif self.analyzed and o.info.is_abstract:
elif self.analyzed and o.info.is_abstract and not o.info.is_protocol:
base_types.append("metaclass=abc.ABCMeta")
self.import_tracker.add_import("abc")
self.import_tracker.require_name("abc")
Expand All @@ -933,7 +927,7 @@ def get_base_types(self, cdef: ClassDef) -> list[str]:
"""Get list of base classes for a class."""
base_types: list[str] = []
p = AliasPrinter(self)
for base in cdef.base_type_exprs:
for base in cdef.base_type_exprs + cdef.removed_base_type_exprs:
if isinstance(base, (NameExpr, MemberExpr)):
if self.get_fullname(base) != "builtins.object":
base_types.append(get_qualified_name(base))
Expand Down
11 changes: 11 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,17 @@ class D(Generic[T]): ...
[out]
class D(Generic[T]): ...

[case testGenericClass_semanal]
from typing import Generic, TypeVar
T = TypeVar('T')
class D(Generic[T]): ...
[out]
from typing import Generic, TypeVar

T = TypeVar('T')

class D(Generic[T]): ...

[case testObjectBaseClass]
class A(object): ...
[out]
Expand Down

0 comments on commit 7fe1fdd

Please sign in to comment.