-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
Possible to support attr.ib(init=False) ? #40
Comments
Also interested in a fix for this. @Tinche Would by-passing |
@asford The fix needs to be here: https://github.com/Tinche/cattrs/blob/161dbd7b0ebd36123480ea770efab1929b2e57c6/src/cattr/gen.py#L151 (I guess just skipping attributes which are |
I might start on this soonish. But what should the spec be? If the key is present in the payload, use a setter instead of the initializer? Or should we just ignore fields with |
I vote for just skip, I heavily use and abuse |
Yeah I agree, let's skip for now and if there's demand we can revisit later with an option. |
Fully agree, this is the ideal behavior on our end for structure.
Would the plan be to skip init=False on both structure and unstructure?
In the unstructure path cattr (I believe) currently includes all fields.
This seems subtly wrong is the intention is to then skip the fields on
structure.
…On Tue, Mar 23, 2021, 07:40 Tin Tvrtković ***@***.***> wrote:
Yeah I agree, let's skip for now and if there's demand we can revisit
later with an option.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<https://github.com/Tinche/cattrs/issues/40#issuecomment-804958011>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AACFBKE5ACIXPP44QH2FV2LTFCR57ANCNFSM4FQUPLZA>
.
|
Just out of curiosity, what do you folks use |
constants, if I type something with I use it very rarely for this reason, but its the only one I have had so far. |
Hm, can you give an example? Your use case sounds like a |
oh 1000% a class var does the same thing. I don't use it often so I usually remember to just do and forget I should do something else. @attrs(auto_attribs=True, frozen=True)
class Test:
a: str
b: str = attrib(init=False, default="QQ") |
Yes. I have a transpiler, most of component's attr are computed and are optional |
I just found this issue because I'm running into this exact problem. I use |
@jpsnyder Interesting. Could you remove the field and create an unstructuring hook that adds it, for serialization? |
It looks like I don't need to have the type field anymore using your example in #140 Thanks again! However, I would still vote for supporting @attr.define
class Element:
tags: Set[str] = attr.ib(init=False, factory=set)
def add_tag(self, *tags: Iterable[str]):
for tag in tags:
self.tags.add(tag)
return self
@attr.define
class SubOne(Element):
a: int
obj = SubOne(1).add_tag("apple", "banana") |
Any update on this? @attr.define()
class Dataset:
"""A Dataset is used for marking data dependencies between workflows."""
uri: str
extra: Optional[Dict[str, Any]] = None
@define()
class File(Dataset):
path: str
conn_id: Optional[str] = None
filetype: Optional[constants.FileType] = None
normalize_config: Optional[Dict] = None
uri: str = field(init=False, repr=False)
extra: Optional[Dict] = field(init=False, factory=dict, repr=False) This fails with :
|
Hi, I would be also interested in this feature. To answer your question, @Tinche:
Yes, this is my primary use case. I think there are many situations where post init is required for a clean code structure and it would be awesome if cattrs would handle these situations automatically (because I think the only other thing someone would do in this situation is to take the boilerplate route and add the trivial structure hooks manually). Or is there any better way to achieve the same with the current capabilities? |
Hello ! Any update on this? |
It'll be in the next milestone, alongside new attrs aliases! |
Aaaand merged! |
No longer needed since: python-attrs/cattrs#40
Description
Right now cattr can only convert dictionaries back to the respective attr object if the field can be initialized in
__init__
(because it callscl(**conv_obj)
). Is it possible for cattr to work with fields withinit
set toFalse
?What I Did
The text was updated successfully, but these errors were encountered: