]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38209: Simplify dataclasses.InitVar by using __class_getitem__(). (GH-16255)
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 22 Sep 2019 10:32:41 +0000 (13:32 +0300)
committerGitHub <noreply@github.com>
Sun, 22 Sep 2019 10:32:41 +0000 (13:32 +0300)
Lib/dataclasses.py

index 9020c905d1176d5eaafe35c883b4bf625a9d21f8..9135b07c9f259a914f7c1d574dc8e87855a3b214 100644 (file)
@@ -199,11 +199,7 @@ _POST_INIT_NAME = '__post_init__'
 # https://bugs.python.org/issue33453 for details.
 _MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
 
-class _InitVarMeta(type):
-    def __getitem__(self, params):
-        return InitVar(params)
-
-class InitVar(metaclass=_InitVarMeta):
+class InitVar:
     __slots__ = ('type', )
 
     def __init__(self, type):
@@ -212,6 +208,9 @@ class InitVar(metaclass=_InitVarMeta):
     def __repr__(self):
         return f'dataclasses.InitVar[{self.type.__name__}]'
 
+    def __class_getitem__(cls, type):
+        return InitVar(type)
+
 
 # Instances of Field are only ever created from within this module,
 # and only from the field() function, although Field instances are