[Python-checkins] bpo-44524: Do not set _name of _SpecialForm without need (GH-27861)

serhiy-storchaka webhook-mailer at python.org
Sat Aug 21 02:48:09 EDT 2021


https://github.com/python/cpython/commit/4ceec495598e78f0776dd46d511dcc612a434dc3
commit: 4ceec495598e78f0776dd46d511dcc612a434dc3
branch: main
author: Serhiy Storchaka <storchaka at gmail.com>
committer: serhiy-storchaka <storchaka at gmail.com>
date: 2021-08-21T09:47:59+03:00
summary:

bpo-44524: Do not set _name of _SpecialForm without need (GH-27861)

Because setting non-empty _name affects behavior of other code.

In most cases __name__ can be derived from __origin__.__name__.

files:
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 d090c5f8818da..3bd5894f42574 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -4929,7 +4929,6 @@ def test_special_attrs(self):
                 self.assertEqual(cls.__name__, name, str(cls))
                 self.assertEqual(cls.__qualname__, name, str(cls))
                 self.assertEqual(cls.__module__, 'typing', str(cls))
-                self.assertEqual(getattr(cls, '_name', name), name, str(cls))
                 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                     s = pickle.dumps(cls, proto)
                     loaded = pickle.loads(s)
diff --git a/Lib/typing.py b/Lib/typing.py
index eeeb295b6c2c2..a283fe40d2d3f 100644
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -461,7 +461,7 @@ class Starship:
     be used with isinstance() or issubclass().
     """
     item = _type_check(parameters, f'{self} accepts only single type.')
-    return _GenericAlias(self, (item,), name="ClassVar")
+    return _GenericAlias(self, (item,))
 
 @_SpecialForm
 def Final(self, parameters):
@@ -482,7 +482,7 @@ class FastConnector(Connection):
     There is no runtime checking of these properties.
     """
     item = _type_check(parameters, f'{self} accepts only single type.')
-    return _GenericAlias(self, (item,), name="Final")
+    return _GenericAlias(self, (item,))
 
 @_SpecialForm
 def Union(self, parameters):
@@ -520,12 +520,9 @@ def Union(self, parameters):
     parameters = _remove_dups_flatten(parameters)
     if len(parameters) == 1:
         return parameters[0]
-
     if len(parameters) == 2 and type(None) in parameters:
-        name = "Optional"
-    else:
-        name = "Union"
-    return _UnionGenericAlias(self, parameters, name=name)
+        return _UnionGenericAlias(self, parameters, name="Optional")
+    return _UnionGenericAlias(self, parameters)
 
 @_SpecialForm
 def Optional(self, parameters):
@@ -570,7 +567,7 @@ def open_helper(file: str, mode: MODE) -> str:
     except TypeError:  # unhashable parameters
         pass
 
-    return _LiteralGenericAlias(self, parameters, name="Literal")
+    return _LiteralGenericAlias(self, parameters)
 
 
 @_SpecialForm
@@ -609,7 +606,7 @@ def Concatenate(self, parameters):
                         "ParamSpec variable.")
     msg = "Concatenate[arg, ...]: each arg must be a type."
     parameters = tuple(_type_check(p, msg) for p in parameters)
-    return _ConcatenateGenericAlias(self, parameters, name="Concatenate")
+    return _ConcatenateGenericAlias(self, parameters)
 
 
 @_SpecialForm
@@ -657,7 +654,7 @@ def is_str(val: Union[str, float]):
     PEP 647 (User-Defined Type Guards).
     """
     item = _type_check(parameters, f'{self} accepts only single type.')
-    return _GenericAlias(self, (item,), name="TypeGuard")
+    return _GenericAlias(self, (item,))
 
 
 class ForwardRef(_Final, _root=True):
@@ -962,7 +959,7 @@ def __mro_entries__(self, bases):
 
     def __getattr__(self, attr):
         if attr in {'__name__', '__qualname__'}:
-            return self._name
+            return self._name or self.__origin__.__name__
 
         # We are careful for copy and pickle.
         # Also for simplicity we just don't relay all dunder names



More information about the Python-checkins mailing list