-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The __init__
topic
#2
Comments
|
Possible ideas for the following example: class Wall(object):
height: int = field(doc="Height of the wall in mm.")
color: str = field(default='white', doc="Color of the wall.") feature 2
2a- most intuitive: @height.inject
@color.inject
def __init__(self, height, color):
self.height = height
self.color = color pros:
cons:
2b- fancy: @(height & color).inject
def __init__(self, height, color):
self.height = height
self.color = color Solves the "many decorator lines" issue but does not solve the rest. 2+ auto-assignlike in |
Feature 3
@inject_fields(height, color)
def __init__(self, fields):
fields.init() pros:
cons:
|
feature 5
5a with decorator@auto_init('height')
class Wall(object):
height: int = field(doc="Height of the wall in mm.")
color: str = field(default='white', doc="Color of the wall.") pros
cons
5b with decorator but fields declare if they are in init@auto_init
class Wall(object):
height: int = field(doc="Height of the wall in mm.", in_init=True)
color: str = field(default='white', doc="Color of the wall.", in_init=False) pros
cons
5c explicit methodclass Wall(object):
height: int = field(doc="Height of the wall in mm.")
color: str = field(default='white', doc="Color of the wall.")
__init__ = make_init(height, color) pros
I do not see much cons here |
Closing - other tickets have been created for each aspect |
Several cases to support
1. user writes his own init entirely. This is already supported and he can set the fields correctly (or not, for the optional)2. user writes his own entirely but not the type hints, doc and defaults (they have already been declared, why copying again). Support the case where he uses all, or some of, the fields as constructor arguments.(will not be fixed for now)3. user writes his own but does not want to write all the argument names so he could use a special variable(fixed in #13 -fields
in the constructor, and anywhere in his constructor dofields.assign()
orfields.init()
.@inject_fields
)4. user writes his own but wished object creation to be faster than the above. In that case (to benchmark though), we would be rather interested by some kind of a(fixed in 0.5.0 -__post_init__
support like in@dataclass
@init_fields
andmake_init
)5. user wants fully automatic init creation(fixed in 0.5.0 -make_init
)In all these cases,
The initialization order can be by default the order of appearance in the class. We could also add a(now in Support explicitinit_after=<fields>
argument tofield()
so that a field requiring other fields to be set beforevalidation
,converter
ordefault_factory
execution could be initialized afterwards. Another option would be to support a special way to declare that one of these three arguments depends on another field.init_after
constraints so that fields can be initialized in another order than the default #23)we could optionally enable validation that all mandatory fields have been set at the end of init. That could be implicit or explicit (i.e. in use case 3. above, that could be done in(now in Generated init methods should or could check that all mandatory fields in the class have been set. #24)fields.assign
?)The text was updated successfully, but these errors were encountered: