Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
cpython
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OpenVMS
Python
cpython
Commits
24e2855ed095
Commit
24e2855ed095
authored
4 years ago
by
Eric V. Smith
Browse files
Options
Downloads
Patches
Plain Diff
Remove an unnecessary copy of the 'namespace' parameter to make_dataclass(). (GH-25372)
parent
b40559f8493e
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Lib/dataclasses.py
+13
-8
13 additions, 8 deletions
Lib/dataclasses.py
Misc/NEWS.d/next/Library/2021-04-12-18-01-10.bpo-43820.YkqYW4.rst
+2
-0
2 additions, 0 deletions
...S.d/next/Library/2021-04-12-18-01-10.bpo-43820.YkqYW4.rst
with
15 additions
and
8 deletions
Lib/dataclasses.py
+
13
−
8
View file @
24e2855e
...
@@ -1233,10 +1233,7 @@
...
@@ -1233,10 +1233,7 @@
if
namespace
is
None
:
if
namespace
is
None
:
namespace
=
{}
namespace
=
{}
else
:
# Copy namespace since we're going to mutate it.
namespace
=
namespace
.
copy
()
# While we're looking through the field names, validate that they
# While we're looking through the field names, validate that they
# are identifiers, are not keywords, and not duplicates.
# are identifiers, are not keywords, and not duplicates.
seen
=
set
()
seen
=
set
()
...
@@ -1239,8 +1236,9 @@
...
@@ -1239,8 +1236,9 @@
# While we're looking through the field names, validate that they
# While we're looking through the field names, validate that they
# are identifiers, are not keywords, and not duplicates.
# are identifiers, are not keywords, and not duplicates.
seen
=
set
()
seen
=
set
()
anns
=
{}
annotations
=
{}
defaults
=
{}
for
item
in
fields
:
for
item
in
fields
:
if
isinstance
(
item
,
str
):
if
isinstance
(
item
,
str
):
name
=
item
name
=
item
...
@@ -1249,7 +1247,7 @@
...
@@ -1249,7 +1247,7 @@
name
,
tp
,
=
item
name
,
tp
,
=
item
elif
len
(
item
)
==
3
:
elif
len
(
item
)
==
3
:
name
,
tp
,
spec
=
item
name
,
tp
,
spec
=
item
namespace
[
name
]
=
spec
defaults
[
name
]
=
spec
else
:
else
:
raise
TypeError
(
f
'
Invalid field:
{
item
!r}
'
)
raise
TypeError
(
f
'
Invalid field:
{
item
!r}
'
)
...
@@ -1261,5 +1259,5 @@
...
@@ -1261,5 +1259,5 @@
raise
TypeError
(
f
'
Field name duplicated:
{
name
!r}
'
)
raise
TypeError
(
f
'
Field name duplicated:
{
name
!r}
'
)
seen
.
add
(
name
)
seen
.
add
(
name
)
anns
[
name
]
=
tp
ann
otation
s
[
name
]
=
tp
...
@@ -1265,4 +1263,9 @@
...
@@ -1265,4 +1263,9 @@
namespace
[
'
__annotations__
'
]
=
anns
# Update 'ns' with the user-supplied namespace plus our calculated values.
def
exec_body_callback
(
ns
):
ns
.
update
(
namespace
)
ns
.
update
(
defaults
)
ns
[
'
__annotations__
'
]
=
annotations
# We use `types.new_class()` instead of simply `type()` to allow dynamic creation
# We use `types.new_class()` instead of simply `type()` to allow dynamic creation
# of generic dataclassses.
# of generic dataclassses.
...
@@ -1267,6 +1270,8 @@
...
@@ -1267,6 +1270,8 @@
# We use `types.new_class()` instead of simply `type()` to allow dynamic creation
# We use `types.new_class()` instead of simply `type()` to allow dynamic creation
# of generic dataclassses.
# of generic dataclassses.
cls
=
types
.
new_class
(
cls_name
,
bases
,
{},
lambda
ns
:
ns
.
update
(
namespace
))
cls
=
types
.
new_class
(
cls_name
,
bases
,
{},
exec_body_callback
)
# Apply the normal decorator.
return
dataclass
(
cls
,
init
=
init
,
repr
=
repr
,
eq
=
eq
,
order
=
order
,
return
dataclass
(
cls
,
init
=
init
,
repr
=
repr
,
eq
=
eq
,
order
=
order
,
unsafe_hash
=
unsafe_hash
,
frozen
=
frozen
,
unsafe_hash
=
unsafe_hash
,
frozen
=
frozen
,
match_args
=
match_args
)
match_args
=
match_args
)
...
...
This diff is collapsed.
Click to expand it.
Misc/NEWS.d/next/Library/2021-04-12-18-01-10.bpo-43820.YkqYW4.rst
0 → 100644
+
2
−
0
View file @
24e2855e
Remove an unneeded copy of the namespace passed to
dataclasses.make_dataclass().
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment