Comments (9)
So the problem here is that when z
is defined, foo
doesn’t exist yet.
It would be possible to pass self into factories but they’d have to be functions which kind of defeats the purpose, right?
I wonder if something like:
@attr.s(post=True)
class C:
x = attr.ib(init=False)
def __post_init__(self):
"""
Called at the end of __init__.
"""
self.x = "whatever"
would make sense?
from attrs.
I think so - if class C
inherited from another class, e.g. class B
, can __post__
access attributes defined in class B
?
from attrs.
I’m thinking that attrs will generate:
def __init__(self):
self._x = None
self.__post_init__()
for you.
from attrs.
Personally I would like to be able to mix and match having the attr generated __init__
along with some user defined __init__
, and be able to decide which is executed first. Is there currently any way at all to execute user defined initialisation other than not decorating your class with @attr.s
?
from attrs.
@hynek I could use something similar to your __post_init__()
as well, but I'd like the usual attr-provided __init__
to be called first. My use case is an object that takes a regex string as an arg and saves a compiled versions to a private attribute.
Currently I'm doing this with a classmethod constructor:
@attr.s
class A:
some_regex = attr.ib()
@classmethod
def create(cls, *args, **kwargs):
self = cls(*args, **kwargs)
self._some_regex = re.compile(self.some_regex)
return self
but this is less than ideal / breaks user expectations since the way to make an A
instance is A.create(..)
instead of A(..)
(which gives you a half-built object).
I initially implemented a version using __init__
that (ab)used side effects in a validator to do the compilation, but then decided that was too hacky even for me.
from attrs.
Why would you say "but"? That's the whole point of post. :)
from attrs.
I was kinda confused by signature of __init__
here and here not taking any arguments, and the value being set in post_init, but I think that's specific to x = attr.ib(init=False)
If we had:
@attr.s
class A:
x = attr.ib()
def __post_init__(self):
assert hasattr(self, 'x')
I'd expect attrs to generate something like:
class A:
def __init__(self, x):
self.x = x
self.__post_init__()
Sorry if I'm confusing things, been up since 4 AM for no good reason.
from attrs.
heh yeah my current plan is what you'd expect. This but thread started in a different direction until it settled on this. :)
I think I'll tackle it once immutably is in.
from attrs.
Closing in favor of #68 which contains a more succinct description of what we came up with.
from attrs.
Related Issues (20)
- Test failures with Python 3.13: stripped indentation from docstrings HOT 1
- Test failure with Python 3.13: TestUpdateAbstractMethods.test_remain_abstract[False] does not raise the expected TypeError HOT 9
- AttributeError propagation is broken in cached_property with slots=True HOT 3
- pyreverse with attrs.org HOT 1
- Special handling of descriptor field like in dataclasses HOT 5
- Test suite fails with pytest 8
- wishlist: attrs.validators.instance_of parse nested types (lists and maps) not needing verbose deep_iterable, deep_map and optional
- Problem with subclassing example? HOT 1
- Validation happens only after the creation of all fields, which can be too late HOT 2
- Private field is not understood by VScode. HOT 4
- `AttributeError` when accessing an attribute with a default value set in a child class HOT 4
- Way to specify KW_ONLY location in the constructor like in dataclass.KW_ONLY
- complain on mutable default HOT 1
- Collections of inner validators for `deep_iterable`/`deep_mapping`
- Typing problem for validator collections
- Wrong error message for inner validators
- Optional inner validators for `deep_mapping`
- Add support for marking classes `typing.final` HOT 3
- Is it possible to validate all attribute types by default?
- Convenience decorators for promoting functions to attrs-compatible validators HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from attrs.