diff options
author | Augusto Hack <hack.augusto@gmail.com> | 2019-06-03 02:14:48 (GMT) |
---|---|---|
committer | Eric V. Smith <ericvsmith@users.noreply.github.com> | 2019-06-03 02:14:48 (GMT) |
commit | 01ee12ba35a333e8a6a25c4153c4a21838e9585c (patch) | |
tree | 2bfae29ffb6dd7c2c3b94e90d6f4c4294b7be3e9 /Lib/dataclasses.py | |
parent | 0025350294959594e7f57aef4fc9579c77a0ed1c (diff) | |
download | cpython-01ee12ba35a333e8a6a25c4153c4a21838e9585c.zip cpython-01ee12ba35a333e8a6a25c4153c4a21838e9585c.tar.gz cpython-01ee12ba35a333e8a6a25c4153c4a21838e9585c.tar.bz2 |
bpo-33569 Preserve type information with dataclasses.InitVar (GH-8927)
Diffstat (limited to 'Lib/dataclasses.py')
-rw-r--r-- | Lib/dataclasses.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py index 75113f1..b035cbb 100644 --- a/Lib/dataclasses.py +++ b/Lib/dataclasses.py @@ -201,10 +201,16 @@ _MODULE_IDENTIFIER_RE = re.compile(r'^(?:\s*(\w+)\s*\.)?\s*(\w+)') class _InitVarMeta(type): def __getitem__(self, params): - return self + return InitVar(params) 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, @@ -586,7 +592,8 @@ def _is_classvar(a_type, typing): def _is_initvar(a_type, dataclasses): # The module we're checking against is the module we're # 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): |