[issue44109] missing dataclass decorator in match-statement example
New submission from Ahmet Burak <ahmetbozyurtt@gmail.com>: 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/ ---------- assignee: docs@python components: Documentation messages: 393454 nosy: ahmetveburak, docs@python priority: normal severity: normal status: open title: missing dataclass decorator in match-statement example type: behavior versions: Python 3.10 _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
Change by Brandt Bucher <brandtbucher@gmail.com>: ---------- nosy: +brandtbucher _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
Brandt Bucher <brandtbucher@gmail.com> added the comment: 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? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
Raymond Hettinger <raymond.hettinger@gmail.com> added the comment:
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. ---------- nosy: +rhettinger _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
Eric V. Smith <eric@trueblade.com> added the comment: 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. ---------- nosy: +eric.smith _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
Andrei Kulakov <andrei.avk@gmail.com> added the comment: 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.. ---------- nosy: +andrei.avk _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
Andrei Kulakov <andrei.avk@gmail.com> added the comment: Ahmet: once there's agreement on how to fix this, would you like to work on a patch? ---------- _______________________________________ Python tracker <report@bugs.python.org> <https://bugs.python.org/issue44109> _______________________________________
participants (5)
-
Ahmet Burak
-
Andrei Kulakov
-
Brandt Bucher
-
Eric V. Smith
-
Raymond Hettinger