-
-
Notifications
You must be signed in to change notification settings - Fork 34k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
gh-144191: Dataclasses single field ordering #144222
Conversation
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
johnslavik
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! You can wait for the green light from Eric before going forward or add tests for this even now. I'd probably change test_1_field_compare. And of course a news entry, too.
Lib/dataclasses.py
Outdated
| if len(flds) == 1: | ||
| self_expr = f"self.{flds[0].name}" | ||
| other_expr = f"other.{flds[0].name}" | ||
| else: | ||
| self_expr = _tuple_str('self', flds) | ||
| other_expr = _tuple_str('other', flds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is how I'd approach it:
| if len(flds) == 1: | |
| self_expr = f"self.{flds[0].name}" | |
| other_expr = f"other.{flds[0].name}" | |
| else: | |
| self_expr = _tuple_str('self', flds) | |
| other_expr = _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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I’ve updated the logic using match/case. It’s more readable and makes the intent clearer.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Simplify single-field dataclass ordering comparisons
#144191