python / cpython

The Python programming language
https://www.python.org
Other
63.49k stars 30.41k forks source link

missing dataclass decorator in match-statement example #88275

Open 18bac53a-00a3-4fa8-8300-7203320f18d9 opened 3 years ago

18bac53a-00a3-4fa8-8300-7203320f18d9 commented 3 years ago
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: ```python assignee = None closed_at = None created_at = labels = ['type-bug', '3.10', 'docs'] title = 'missing dataclass decorator in match-statement example' updated_at = user = 'https://github.com/ahmetveburak' ``` bugs.python.org fields: ```python activity = actor = 'andrei.avk' assignee = 'docs@python' closed = False closed_date = None closer = None components = ['Documentation'] creation = 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

18bac53a-00a3-4fa8-8300-7203320f18d9 commented 3 years ago

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/

brandtbucher commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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:

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

akulakov commented 3 years ago

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

Eclips4 commented 5 months ago

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.