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
ed7d429e
Commit
ed7d429e
authored
Jan 06, 2018
by
Eric V. Smith
Committed by
GitHub
Jan 06, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-32278: Allow dataclasses.make_dataclass() to omit type information. (gh-5115)
parent
e7ba013d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
6 deletions
+30
-6
Lib/dataclasses.py
Lib/dataclasses.py
+14
-6
Lib/test/test_dataclasses.py
Lib/test/test_dataclasses.py
+14
-0
Misc/NEWS.d/next/Library/2018-01-06-15-15-34.bpo-32278.bGnGc0.rst
...S.d/next/Library/2018-01-06-15-15-34.bpo-32278.bGnGc0.rst
+2
-0
No files found.
Lib/dataclasses.py
View file @
ed7d429e
...
...
@@ -708,9 +708,10 @@ def _astuple_inner(obj, tuple_factory):
def
make_dataclass
(
cls_name
,
fields
,
*
,
bases
=
(),
namespace
=
None
):
"""Return a new dynamically created dataclass.
The dataclass name will be 'cls_name'. 'fields' is an interable
of either (name, type) or (name, type, Field) objects. Field
objects are created by calling 'field(name, type [, Field])'.
The dataclass name will be 'cls_name'. 'fields' is an iterable
of either (name), (name, type) or (name, type, Field) objects. If type is
omitted, use the string 'typing.Any'. Field objects are created by
calling 'field(name, type [, Field])'.
C = make_class('C', [('a', int', ('b', int, Field(init=False))], bases=Base)
...
...
@@ -730,12 +731,19 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None):
# Copy namespace since we're going to mutate it.
namespace
=
namespace
.
copy
()
anns
=
collections
.
OrderedDict
((
name
,
tp
)
for
name
,
tp
,
*
_
in
fields
)
namespace
[
'__annotations__'
]
=
anns
anns
=
collections
.
OrderedDict
()
for
item
in
fields
:
if
len
(
item
)
==
3
:
if
isinstance
(
item
,
str
):
name
=
item
tp
=
'typing.Any'
elif
len
(
item
)
==
2
:
name
,
tp
,
=
item
elif
len
(
item
)
==
3
:
name
,
tp
,
spec
=
item
namespace
[
name
]
=
spec
anns
[
name
]
=
tp
namespace
[
'__annotations__'
]
=
anns
cls
=
type
(
cls_name
,
bases
,
namespace
)
return
dataclass
(
cls
)
...
...
Lib/test/test_dataclasses.py
View file @
ed7d429e
...
...
@@ -2033,6 +2033,20 @@ class TestCase(unittest.TestCase):
self
.
assertEqual
(
C
.
y
,
10
)
self
.
assertEqual
(
C
.
z
,
20
)
def
test_helper_make_dataclass_no_types
(
self
):
C
=
make_dataclass
(
'Point'
,
[
'x'
,
'y'
,
'z'
])
c
=
C
(
1
,
2
,
3
)
self
.
assertEqual
(
vars
(
c
),
{
'x'
:
1
,
'y'
:
2
,
'z'
:
3
})
self
.
assertEqual
(
C
.
__annotations__
,
{
'x'
:
'typing.Any'
,
'y'
:
'typing.Any'
,
'z'
:
'typing.Any'
})
C
=
make_dataclass
(
'Point'
,
[
'x'
,
(
'y'
,
int
),
'z'
])
c
=
C
(
1
,
2
,
3
)
self
.
assertEqual
(
vars
(
c
),
{
'x'
:
1
,
'y'
:
2
,
'z'
:
3
})
self
.
assertEqual
(
C
.
__annotations__
,
{
'x'
:
'typing.Any'
,
'y'
:
int
,
'z'
:
'typing.Any'
})
class
TestDocString
(
unittest
.
TestCase
):
def
assertDocStrEqual
(
self
,
a
,
b
):
...
...
Misc/NEWS.d/next/Library/2018-01-06-15-15-34.bpo-32278.bGnGc0.rst
0 → 100644
View file @
ed7d429e
Make type information optional on dataclasses.make_dataclass(). If omitted,
the string 'typing.Any' is used.
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