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
b0f4dab8
Commit
b0f4dab8
authored
Aug 20, 2019
by
Eric V. Smith
Committed by
GitHub
Aug 20, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-37868: Improve is_dataclass for instances. (GH-15325)
parent
d3c8d735
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
32 additions
and
2 deletions
+32
-2
Lib/dataclasses.py
Lib/dataclasses.py
+3
-2
Lib/test/test_dataclasses.py
Lib/test/test_dataclasses.py
+26
-0
Misc/NEWS.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst
...S.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst
+3
-0
No files found.
Lib/dataclasses.py
View file @
b0f4dab8
...
@@ -1015,13 +1015,14 @@ def fields(class_or_instance):
...
@@ -1015,13 +1015,14 @@ def fields(class_or_instance):
def
_is_dataclass_instance
(
obj
):
def
_is_dataclass_instance
(
obj
):
"""Returns True if obj is an instance of a dataclass."""
"""Returns True if obj is an instance of a dataclass."""
return
not
isinstance
(
obj
,
type
)
and
hasattr
(
obj
,
_FIELDS
)
return
hasattr
(
type
(
obj
)
,
_FIELDS
)
def
is_dataclass
(
obj
):
def
is_dataclass
(
obj
):
"""Returns True if obj is a dataclass or an instance of a
"""Returns True if obj is a dataclass or an instance of a
dataclass."""
dataclass."""
return
hasattr
(
obj
,
_FIELDS
)
cls
=
obj
if
isinstance
(
obj
,
type
)
else
type
(
obj
)
return
hasattr
(
cls
,
_FIELDS
)
def
asdict
(
obj
,
*
,
dict_factory
=
dict
):
def
asdict
(
obj
,
*
,
dict_factory
=
dict
):
...
...
Lib/test/test_dataclasses.py
View file @
b0f4dab8
...
@@ -1300,6 +1300,32 @@ class TestCase(unittest.TestCase):
...
@@ -1300,6 +1300,32 @@ class TestCase(unittest.TestCase):
self
.
assertTrue
(
is_dataclass
(
d
.
d
))
self
.
assertTrue
(
is_dataclass
(
d
.
d
))
self
.
assertFalse
(
is_dataclass
(
d
.
e
))
self
.
assertFalse
(
is_dataclass
(
d
.
e
))
def
test_is_dataclass_when_getattr_always_returns
(
self
):
# See bpo-37868.
class
A
:
def
__getattr__
(
self
,
key
):
return
0
self
.
assertFalse
(
is_dataclass
(
A
))
a
=
A
()
# Also test for an instance attribute.
class
B
:
pass
b
=
B
()
b
.
__dataclass_fields__
=
[]
for
obj
in
a
,
b
:
with
self
.
subTest
(
obj
=
obj
):
self
.
assertFalse
(
is_dataclass
(
obj
))
# Indirect tests for _is_dataclass_instance().
with
self
.
assertRaisesRegex
(
TypeError
,
'should be called on dataclass instances'
):
asdict
(
obj
)
with
self
.
assertRaisesRegex
(
TypeError
,
'should be called on dataclass instances'
):
astuple
(
obj
)
with
self
.
assertRaisesRegex
(
TypeError
,
'should be called on dataclass instances'
):
replace
(
obj
,
x
=
0
)
def
test_helper_fields_with_class_instance
(
self
):
def
test_helper_fields_with_class_instance
(
self
):
# Check that we can call fields() on either a class or instance,
# Check that we can call fields() on either a class or instance,
# and get back the same thing.
# and get back the same thing.
...
...
Misc/NEWS.d/next/Library/2019-08-17-22-33-54.bpo-37868.hp64fi.rst
0 → 100644
View file @
b0f4dab8
Fix dataclasses.is_dataclass when given an instance that never raises
AttributeError in __getattr__. That is, an object that returns something
for __dataclass_fields__ even if it's not a dataclass.
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