-
-
Notifications
You must be signed in to change notification settings - Fork 387
Field hooks are too clunky with Python 3.10 / string annotations #766
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
Comments
Another option is to do
The hints will have the evalled rhs of the colon example: You can't call attr.resolve_types because |
What I mean is this:
There is just no good way for this. |
I meant do something like this:
Sadly we can't just call |
Ah right, you're correct as usual. I tend to think it would be nice to have helpers for ergonomics, but less APIs is better for now of course so I guess we can fix it and add docs for people who run into the same problem. |
Hm so fun fact, it has to be even more complicated due to the various ways of defining types: def hook(cls, attribs):
hints = get_type_hints(cls)
results[:] = [
(a.name, hints.get(a.name) or a.type) for a in attribs
]
return attribs |
I'm not sure about the If you really wanted an API the only one I came up with was:
|
attribs are on cls, so shouldn't the following work: def resolve_fields(cls):
fields = attr.fields(cls)
hints = get_type_hints(cls)
for f in attr.fields(cls):
t = hints.get(f.name)
if t is not None:
f.type = t
return fields ? 🤔 (I'm trying to use "fields" in new APIs in anticipation of |
At the moment the hook is run |
hmm, it seems to make sense to allow for both cases, would you agree? i.e. |
Hmm. We can probably just build it into
Or we could add a new method. |
I would love to have it part of the old API, but how big is the risk of this breaking something? |
fixed by #774 🎉 |
Currently I had to exclude
test_hooks.py
on Python 3.10, because it would be too painful to make them work with string annotations.The problem is that there is no good helper to transform
"str"
intostr
. We're supposed to be able toeval()
the string.I've talked to @ambv and he suggested the following:
e.g.
typing._eval_type
has been part of the stdlib since 3.5 and I don't expect it to go anywhere.I'm not sure how to call that function.
attr.resolve_type()
sounds good but that's a bit too close toattr.resolve_types()
so I'm open to better suggestions.I'm not sure we can get it done by 21.1.0 but it should be on our short term agenda.
cc @sscherfke @euresti
The text was updated successfully, but these errors were encountered: