bpo-33569 Preserve type information with dataclasses.InitVar (GH-8927)

This commit is contained in:
Augusto Hack 2019-06-02 23:14:48 -03:00 committed by Eric V. Smith
parent 0025350294
commit 01ee12ba35
3 changed files with 17 additions and 3 deletions

View File

@ -201,10 +201,16 @@ _MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)')
class _InitVarMeta(type): class _InitVarMeta(type):
def __getitem__(self, params): def __getitem__(self, params):
return self return InitVar(params)
class InitVar(metaclass=_InitVarMeta): class InitVar(metaclass=_InitVarMeta):
pass __slots__ = ('type', )
def __init__(self, type):
self.type = type
def __repr__(self):
return f'dataclasses.InitVar[{self.type.__name__}]'
# Instances of Field are only ever created from within this module, # Instances of Field are only ever created from within this module,
@ -586,7 +592,8 @@ def _is_classvar(a_type, typing):
def _is_initvar(a_type, dataclasses): def _is_initvar(a_type, dataclasses):
# The module we're checking against is the module we're # The module we're checking against is the module we're
# currently in (dataclasses.py). # currently in (dataclasses.py).
return a_type is dataclasses.InitVar return (a_type is dataclasses.InitVar
or type(a_type) is dataclasses.InitVar)
def _is_type(annotation, cls, a_module, a_type, is_type_predicate): def _is_type(annotation, cls, a_module, a_type, is_type_predicate):

View File

@ -1097,6 +1097,12 @@ class TestCase(unittest.TestCase):
c = C(init_param=10) c = C(init_param=10)
self.assertEqual(c.x, 20) self.assertEqual(c.x, 20)
def test_init_var_preserve_type(self):
self.assertEqual(InitVar[int].type, int)
# Make sure the repr is correct.
self.assertEqual(repr(InitVar[int]), 'dataclasses.InitVar[int]')
def test_init_var_inheritance(self): def test_init_var_inheritance(self):
# Note that this deliberately tests that a dataclass need not # Note that this deliberately tests that a dataclass need not
# have a __post_init__ function if it has an InitVar field. # have a __post_init__ function if it has an InitVar field.

View File

@ -0,0 +1 @@
dataclasses.InitVar: Exposes the type used to create the init var.