8000 missing dataclass decorator in match-statement example · Issue #88275 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

missing dataclass decorator in match-statement example #88275

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

Closed
ahmetveburak mannequin opened this issue May 11, 2021 · 7 comments
Closed

missing dataclass decorator in match-statement example #88275

ahmetveburak mannequin opened this issue May 11, 2021 · 7 comments
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir topic-dataclasses type-bug An unexpected behavior, bug, or error

Comments

@ahmetveburak
Copy link
Mannequin
ahmetveburak mannequin commented May 11, 2021
BPO 44109
Nosy @rhettinger, @ericvsmith, @brandtbucher, @akulakov, @ahmetveburak

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2021-05-11.12:36:08.995>
labels = ['type-bug', '3.10', 'docs']
title = 'missing dataclass decorator in match-statement example'
updated_at = <Date 2021-06-26.13:55:21.690>
user = 'https://github.com/ahmetveburak'

bugs.python.org fields:

activity = <Date 2021-06-26.13:55:21.690>
actor = 'andrei.avk'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2021-05-11.12:36:08.995>
creator = 'ahmetveburak'
dependencies = []
files = []
hgrepos = []
issue_num = 44109
keywords = []
message_count = 6.0
messages = ['393454', '393607', '393678', '393812', '396527', '396550']
nosy_count = 6.0
nosy_names = ['rhettinger', 'eric.smith', 'docs@python', 'brandtbucher', 'andrei.avk', 'ahmetveburak']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue44109'
versions = ['Python 3.10']

Linked PRs

@ahmetveburak
Copy link
Mannequin Author
ahmetveburak mannequin commented May 11, 2021

Using Point class as in the documentation example, raises TypeError: Point() takes no arguments
https://docs.python.org/3.10/whatsnew/3.10.html#patterns-and-classes

Also there is same example in the PEP-636's latests parts, Point class used with dataclass decorator and works perfectly.
https://www.python.org/dev/peps/pep-0636/

@ahmetveburak ahmetveburak mannequin added the 3.10 only security fixes label May 11, 2021
@ahmetveburak ahmetveburak mannequin assigned docspython May 11, 2021
@ahmetveburak ahmetveburak mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error 3.10 only security fixes labels May 11, 2021
@ahmetveburak ahmetveburak mannequin assigned docspython May 11, 2021
@ahmetveburak ahmetveburak mannequin added docs Documentation in the Doc dir type-bug An unexpected behavior, bug, or error labels May 11, 2021
@brandtbucher
Copy link
Member

I don't really think there is anything wrong with the documentation. I can copy-and-paste all of the code from the PEP-634 section of the 3.10 What's New into the REPL without any issues (provided that named subjects like point/points/test_variable/color are actually defined).

Note that none of the example snippets actually *create* Point instances. Rather, some of the patterns *match* Point instances... which is sort of the opposite of instantiation.

As the Point class is currently written, instantiation would probably look like:

>> p = Point()
>> p.x = 0
>> p.y = 0

Sure, it would help to have an __init__ defined in real code, but I sort of feel like adding it to this example would just distract (especially since the example doesn't actually *need* it).

Thoughts from others?

@rhettinger
Copy link
Contributor

Thoughts from others?

As it stands, the Point class is weird and atypical by only using class variables. The example would be improved by adding @DataClass or inheriting from typing.NamedTuple.

@ericvsmith
Copy link
Member

I agree with Raymond: a more useful example would use dataclasses or typing.NamedTuple. The use of class variables is unusual, and it took me a while to understand how it would work.

@akulakov
Copy link
Contributor

I agree the example is confusing for all of the stated reasons.

It seems to me it's better to use a plain class with a __init__() for two reason:

  • for people who are not familiar with namedtuples or dataclasses, it would be harder to learn two fairly complex topics at the same time. Docs for both are fairly extensive.

  • for those who *are* familiar with namedtuples or dataclasses (whichever is used in the example), the example may imply they should be used with pattern matching, and that plain classes may not work or not work well.

I can make a PR that adds __init__ if this makes sense..

@akulakov
Copy link
Contributor

Ahmet: once there's agreement on how to fix this, would you like to work on a patch?

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@Eclips4
Copy link
Member
Eclips4 commented Jun 8, 2024

We have a similar example in https://docs.python.org/3.12/tutorial/controlflow.html#match-statements, which is define the __init__ method. Let's make them similar.

@Eclips4 Eclips4 added 3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes and removed 3.10 only security fixes labels Jun 8, 2024
< 8000 div class="TimelineItem-badge ">
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 17, 2025
…honGH-120281)

(cherry picked from commit 7a9d462)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue May 17, 2025
…honGH-120281)

(cherry picked from commit 7a9d462)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
picnixz pushed a commit that referenced this issue May 17, 2025
…-120281) (#134142)

gh-88275: Add missing `__init__` method to `match` example (GH-120281)
(cherry picked from commit 7a9d462)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
picnixz pushed a commit that referenced this issue May 17, 2025
…-120281) (#134143)

gh-88275: Add missing `__init__` method to `match` example (GH-120281)
(cherry picked from commit 7a9d462)

Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
@picnixz picnixz closed this as completed May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 only security fixes 3.13 bugs and security fixes 3.14 bugs and security fixes docs Documentation in the Doc dir topic-dataclasses type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

6 participants
0