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

This commit is contained in:
Serhiy Storchaka 2019-09-22 13:32:41 +03:00 committed by GitHub
parent 1d094af716
commit b4d0b39a9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 5 deletions

View File

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