This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: missing dataclass decorator in match-statement example
Type: behavior Stage:
Components: Documentation Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: docs@python Nosy List: ahmetveburak, andrei.avk, brandtbucher, docs@python, eric.smith, rhettinger
Priority: normal Keywords:

Created on 2021-05-11 12:36 by ahmetveburak, last changed 2022-04-11 14:59 by admin.

Messages (6)
msg393454 - (view) Author: Ahmet Burak (ahmetveburak) Date: 2021-05-11 12:36
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/
msg393607 - (view) Author: Brandt Bucher (brandtbucher) * (Python committer) Date: 2021-05-13 21:21
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?
msg393678 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2021-05-14 17:06
> 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.
msg393812 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-05-17 14:23
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.
msg396527 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-06-25 21:00
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..
msg396550 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2021-06-26 13:55
Ahmet: once there's agreement on how to fix this, would you like to work on a patch?
History
Date User Action Args
2022-04-11 14:59:45adminsetgithub: 88275
2021-06-26 13:55:21andrei.avksetmessages: + msg396550
2021-06-25 21:00:02andrei.avksetnosy: + andrei.avk
messages: + msg396527
2021-05-17 14:23:54eric.smithsetnosy: + eric.smith
messages: + msg393812
2021-05-14 17:06:16rhettingersetnosy: + rhettinger
messages: + msg393678
2021-05-13 21:21:44brandtbuchersetmessages: + msg393607
2021-05-13 20:16:59brandtbuchersetnosy: + brandtbucher
2021-05-11 12:36:09ahmetveburakcreate