Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4994,6 +4994,17 @@ def test_special_attrs2(self):
loaded = pickle.loads(s)
self.assertIs(SpecialAttrsP, loaded)

def test_genericalias_dir(self):
class Foo(Generic[T]):
def bar(self):
pass
baz = 3
# The class attributes of the original class should be visible even
# in dir() of the GenericAlias. See bpo-45755.
self.assertIn('bar', dir(Foo[int]))
self.assertIn('baz', dir(Foo[int]))


class AllTests(BaseTestCase):
"""Tests for __all__."""

Expand Down
3 changes: 3 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,9 @@ def __subclasscheck__(self, cls):
raise TypeError("Subscripted generics cannot be used with"
" class and instance checks")

def __dir__(self):
return list(set(super().__dir__()
+ [attr for attr in dir(self.__origin__) if not _is_dunder(attr)]))

# Special typing constructs Union, Optional, Generic, Callable and Tuple
# use three special attributes for internal bookkeeping of generic types:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:mod:`typing` generic aliases now reveal the class attributes of the
original generic class when passed to ``dir()``. This was the behavior up to
Python 3.6, but was changed in 3.7-3.9.