Issue33452
Created on 2018-05-09 20:50 by Ricyteach, last changed 2022-04-11 14:59 by admin.
| Messages (3) | |||
|---|---|---|---|
| msg316331 - (view) | Author: Rick Teachey (Ricyteach) * | Date: 2018-05-09 20:50 | |
The dataclasses module is incredibly easy to use. This is a good thing. BUT one downside is it will definitely be utilized by people who don't have a thorough understanding of how it does what it does.
Even for me, despite having a very good understanding of how it works, after heavily using it on a project for about 3 weeks now I made a mistake like the one below:
class ImportantMixin:
def __init__(self):
super().__init__()
important_task()
@dataclass
class NaiveDClass(ImportantMixin):
data1 = int
data2 = int
I then went on along my merry way. Obviously, ImportantMixin.__init__ never gets called and I didn't realize this until it was a bigger problem than it should have been (should have written better tests! but I digress).
It would seem like a good idea for the dataclasses module to let the user know they did this, probably via the warning system. Seems like it would be relatively easy to do: if there is an init method being create, just inspect the MRO for any previously defined init methods that weren't created by dataclasses.
Thanks.
|
|||
| msg316340 - (view) | Author: Eric V. Smith (eric.smith) * ![]() |
Date: 2018-05-09 22:38 | |
I'm okay with the concept, but I don't know how to implement it. You need to not only know if a class has a __init__, but also if it's expected to be called. For example, these don't normally get called, but if you inherit from them (and everyone does inherit from object) you don't want a warning. >>> hasattr(int, '__init__') True >>> hasattr(object, '__init__') True I'm open to ideas. |
|||
| msg316343 - (view) | Author: Rick Teachey (Ricyteach) * | Date: 2018-05-10 02:27 | |
The init method that comes up for int, str, float, etc is just the object init:
assert int.__init__ is object.__init__
Probably the thing to do is grab any init methods that aren't the object.__init__ while stripping out the dataclass-created init methods? Maybe something like:
import warnings
if cls.__dataclass_params__.init:
for pcls in cls.mro():
if pcls.__init__ is not object.__init__:
try:
d_params = getattr(pcls, "__dataclass_params__")
except AttributeError:
warnings.warn('Found a not called init')
else:
if not d_params.init:
warnings.warn('Found a custom dataclass init')
|
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022-04-11 14:59:00 | admin | set | github: 77633 |
| 2018-05-10 02:27:36 | Ricyteach | set | messages: + msg316343 |
| 2018-05-09 22:54:00 | eric.smith | set | assignee: eric.smith |
| 2018-05-09 22:38:47 | eric.smith | set | messages: + msg316340 |
| 2018-05-09 20:50:00 | Ricyteach | create | |
