[Python-checkins] bpo-38209: Simplify dataclasses.InitVar by using __class_getitem__(). (GH-16255)

Serhiy Storchaka webhook-mailer at python.org
Sun Sep 22 06:32:51 EDT 2019


https://github.com/python/cpython/commit/b4d0b39a9b4cd203bcc5b236dc96456e9658119a
commit: b4d0b39a9b4cd203bcc5b236dc96456e9658119a
branch: master
author: Serhiy Storchaka <storchaka at gmail.com>
committer: GitHub <noreply at github.com>
date: 2019-09-22T13:32:41+03:00
summary:

bpo-38209: Simplify dataclasses.InitVar by using __class_getitem__(). (GH-16255)

files:
M Lib/dataclasses.py

diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 9020c905d117..9135b07c9f25 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -199,11 +199,7 @@ def __repr__(self):
 # 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 @@ def __init__(self, type):
     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



More information about the Python-checkins mailing list