Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-144191: Dataclasses single field ordering#144222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
whyvineet wants to merge6 commits intopython:main
base:main
Choose a base branch
Loading
fromwhyvineet:dataclasses-single-field-ordering
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletionsLib/dataclasses.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1180,21 +1180,27 @@ def _process_class(cls, init, repr, eq, order, unsafe_hash, frozen,
if order:
# Create and set the ordering methods.
flds = [f for f in field_list if f.compare]
self_tuple = _tuple_str('self', flds)
other_tuple = _tuple_str('other', flds)
match flds:
# Special-case single field comparisons. See GH-144191.
case [single_fld]:
self_expr = f'self.{single_fld.name}'
other_expr = f'other.{single_fld.name}'
case _:
self_expr = _tuple_str('self', flds)
other_expr = _tuple_str('other', flds)
for name, op in [('__lt__', '<'),
('__le__', '<='),
('__gt__', '>'),
('__ge__', '>='),
]:
# Create a comparison function. If the fields in the object are
# named 'x' and 'y', thenself_tuple is the string
# '(self.x,self.y)' andother_tuple is the string
# named 'x' and 'y', thenself_expr is the string
# '(self.x,self.y)' andother_expr is the string
# '(other.x,other.y)'.
func_builder.add_fn(name,
('self', 'other'),
[ ' if other.__class__ is self.__class__:',
f' return {self_tuple}{op}{other_tuple}',
f' return {self_expr}{op}{other_expr}',
' return NotImplemented'],
overwrite_error='Consider using functools.total_ordering')

Expand Down
10 changes: 10 additions & 0 deletionsLib/test/test_dataclasses/__init__.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -406,6 +406,16 @@ class C:
self.assertGreaterEqual(C(1), C(0))
self.assertGreaterEqual(C(1), C(1))

@dataclass(order=True)
class C2:
x: int
y: int = field(compare=False)

self.assertLess(C2(0, 10), C2(1, -5))
self.assertLessEqual(C2(1, 10), C2(1, -5))
self.assertGreater(C2(2, -1), C2(1, 999))
self.assertGreaterEqual(C2(1, 10), C2(1, -5))

def test_simple_compare(self):
# Ensure that order=False is the default.
@dataclass
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
Dataclass ordering methods were adjusted to simplify comparisons for classes with a single comparison field, aligning the generated behaviour more closely with hand-written comparison logic.
Loading

[8]ページ先頭

©2009-2026 Movatter.jp