Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
01ee12ba
Commit
01ee12ba
authored
Jun 02, 2019
by
Augusto Hack
Committed by
Eric V. Smith
Jun 02, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-33569 Preserve type information with dataclasses.InitVar (GH-8927)
parent
00253502
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
17 additions
and
3 deletions
+17
-3
Lib/dataclasses.py
Lib/dataclasses.py
+10
-3
Lib/test/test_dataclasses.py
Lib/test/test_dataclasses.py
+6
-0
Misc/NEWS.d/next/Library/2018-08-28-03-00-12.bpo-33569.45YlGG.rst
...S.d/next/Library/2018-08-28-03-00-12.bpo-33569.45YlGG.rst
+1
-0
No files found.
Lib/dataclasses.py
View file @
01ee12ba
...
...
@@ -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
):
...
...
Lib/test/test_dataclasses.py
View file @
01ee12ba
...
...
@@ -1097,6 +1097,12 @@ class TestCase(unittest.TestCase):
c
=
C
(
init_param
=
10
)
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
):
# Note that this deliberately tests that a dataclass need not
# have a __post_init__ function if it has an InitVar field.
...
...
Misc/NEWS.d/next/Library/2018-08-28-03-00-12.bpo-33569.45YlGG.rst
0 → 100644
View file @
01ee12ba
dataclasses.InitVar: Exposes the type used to create the init var.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment