[Python-checkins] bpo-45755: [typing] Reveal class attributes of generic in generic aliases in `dir()` (GH-29962)

miss-islington webhook-mailer at python.org
Fri Dec 17 06:33:24 EST 2021


https://github.com/python/cpython/commit/87539cc716fab47cd4f501f2441c4ab8e80bce6f
commit: 87539cc716fab47cd4f501f2441c4ab8e80bce6f
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-12-17T03:33:07-08:00
summary:

bpo-45755: [typing] Reveal class attributes of generic in generic aliases in `dir()` (GH-29962)

(cherry picked from commit d6e13747161d7b634b47d2d3d212ed3be4a21fab)

Co-authored-by: Ken Jin <28750310+Fidget-Spinner at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2021-12-07-21-55-22.bpo-45755.bRqKGa.rst
M Lib/test/test_typing.py
M Lib/typing.py

diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index fdec29ea58773..82b6f8c1c6406 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -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__."""
 
diff --git a/Lib/typing.py b/Lib/typing.py
index b743d400c0e5f..25225470afbac 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -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:
diff --git a/Misc/NEWS.d/next/Library/2021-12-07-21-55-22.bpo-45755.bRqKGa.rst b/Misc/NEWS.d/next/Library/2021-12-07-21-55-22.bpo-45755.bRqKGa.rst
new file mode 100644
index 0000000000000..e5201b0dfde2d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-12-07-21-55-22.bpo-45755.bRqKGa.rst
@@ -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.



More information about the Python-checkins mailing list