FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-41747: Ensure all dataclass methods uses their parents' qualname by isidentical · Pull Request #22155 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
11 changes: 9 additions & 2 deletions Lib/dataclasses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import builtins
import functools
import _thread
from types import GenericAlias
from types import FunctionType, GenericAlias


__all__ = ['dataclass',
Expand Down Expand Up @@ -749,12 +749,19 @@ def _get_field(cls, a_name, a_type):

return f

def _set_qualname(cls, value):
# Ensure that the functions returned from _create_fn uses the proper
# __qualname__ (the class they belong to).
if isinstance(value, FunctionType):
value.__qualname__ = f"{cls.__qualname__}.{value.__name__}"
return value

def _set_new_attribute(cls, name, value):
# Never overwrites an existing attribute. Returns True if the
# attribute already exists.
if name in cls.__dict__:
return True
_set_qualname(cls, value)
setattr(cls, name, value)
return False

Expand All @@ -769,7 +776,7 @@ def _hash_set_none(cls, fields, globals):

def _hash_add(cls, fields, globals):
flds = [f for f in fields if (f.compare if f.hash is None else f.hash)]
return _hash_fn(flds, globals)
return _set_qualname(cls, _hash_fn(flds, globals))

def _hash_exception(cls, fields, globals):
# Raise an exception.
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_dataclasses.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1934,6 +1934,30 @@ class R:
self.assertEqual(new_sample.x, another_new_sample.x)
self.assertEqual(sample.y, another_new_sample.y)

def test_dataclasses_qualnames(self):
@dataclass(order=True, unsafe_hash=True, frozen=True)
class A:
x: int
y: int

self.assertEqual(A.__init__.__name__, "__init__")
for function in (
'__eq__',
'__lt__',
'__le__',
'__gt__',
'__ge__',
'__hash__',
'__init__',
'__repr__',
'__setattr__',
'__delattr__',
):
self.assertEqual(getattr(A, function).__qualname__, f"TestCase.test_dataclasses_qualnames.<locals>.A.{function}")

with self.assertRaisesRegex(TypeError, r"A\.__init__\(\) missing"):
A()


class TestFieldNoAnnotation(unittest.TestCase):
def test_field_without_annotation(self):
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Ensure all methods that generated from :func:`dataclasses.dataclass`
objects now have the proper ``__qualname__`` attribute referring to
the class they belong to. Patch by Batuhan Taskaya.

Back | FazBrowse Home | New Git URL