[issue44365] Bad dataclass post-init example

2021-10-31 Thread da2ce7
da2ce7 added the comment: Amazingly, the original example needs a very small change to make it work as expected: @dataclass class Rectangle: height: float width: float @dataclass class Square(Rectangle): side: float height: float = field(init=False) width: float =

[issue44365] Bad dataclass post-init example

2021-10-31 Thread da2ce7
da2ce7 added the comment: Upon Self Review, I think that this slightly updated version is a bit more illustrative. -- Added file: https://bugs.python.org/file50415/dataclass_inheritance_v2_test.py ___ Python tracker

[issue44365] Bad dataclass post-init example

2021-10-31 Thread da2ce7
da2ce7 added the comment: I have made a slightly more comprehensive example. See file attached. Please consider for the updated documentation. -- nosy: +da2ce7 Added file: https://bugs.python.org/file50414/dataclass_inheritance_test.py ___ Python

[issue44365] Bad dataclass post-init example

2021-06-17 Thread Andrei Kulakov
Andrei Kulakov added the comment: It's a good example, but some readers might only have a vague idea (if any) of what FTP is, so a self contained example might be easier to digest? -- ___ Python tracker

[issue44365] Bad dataclass post-init example

2021-06-17 Thread Eric V. Smith
Eric V. Smith added the comment: I was thinking about something like: @dataclass class FtpHelper(ftplib.FTP): my_host: str my_user: str lookup_password: InitVar[Callable] def __post_init__(self, lookup_password): super().__init__(host=self.my_host, user=self.my_user,

[issue44365] Bad dataclass post-init example

2021-06-14 Thread Andrei Kulakov
Andrei Kulakov added the comment: How about this example: @dataclass class Rect: x: int y: int r=Rect(5,2) @dataclass class HyperRect(Rect): z: int def __post_init__(self): self.vol = self.x*self.y*self.z hr=HyperRect(5,2,3) print("hr.vol", hr.vol) Hyper

[issue44365] Bad dataclass post-init example

2021-06-10 Thread Micael Jarniac
Micael Jarniac added the comment: Well, at least for this example, to call `super().__init__()`, I'd need to provide it the two arguments it expects, `x` and `y`, otherwise it'd give an error: > TypeError: __init__() missing 2 required positional arguments: 'x' and 'y' If I try calling it

[issue44365] Bad dataclass post-init example

2021-06-10 Thread Eric V. Smith
Eric V. Smith added the comment: I'm not sure directly calling __post_init__ is a good pattern. Why would not calling __init__, like you would with any other class, not be the preferred thing to do? -- ___ Python tracker

[issue44365] Bad dataclass post-init example

2021-06-10 Thread Micael Jarniac
Micael Jarniac added the comment: I'm trying to think of an example, and what I've thought of so far is having a base dataclass that has a `__post_init__` method, and another dataclass that inherits from it and also has a `__post_init__` method. In that case, the subclass might need to call

[issue44365] Bad dataclass post-init example

2021-06-10 Thread Eric V. Smith
Eric V. Smith added the comment: The example was added in https://github.com/python/cpython/pull/25967 When reviewing it, I think I missed the fact that the base class is a dataclass. The example and text make more sense if Rectangle isn't a dataclass. Still, I don't like the example at

[issue44365] Bad dataclass post-init example

2021-06-09 Thread Eric V. Smith
Eric V. Smith added the comment: Agreed that that's not a good (or even workable) example. Thanks for pointing it out. I'll come up with something better. -- assignee: docs@python -> eric.smith versions: +Python 3.10, Python 3.11, Python 3.9 ___

[issue44365] Bad dataclass post-init example

2021-06-09 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- nosy: +eric.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue44365] Bad dataclass post-init example

2021-06-09 Thread Micael Jarniac
New submission from Micael Jarniac : https://docs.python.org/3/library/dataclasses.html#post-init-processing https://github.com/python/cpython/blob/3.9/Doc/library/dataclasses.rst#post-init-processing In the example, a base class "Rectangle" is defined, and then a "Square" class inherits