Comments (5)
I assume you're using from __future__ import annotations
?
cattrs calls attrs.resolve_types
on every class it sees, which calls into typing.get_type_hints
which explodes. I don't think there's an easy way for us to be selective about which fields we want resolved, unfortunately. cattrs only wants init=True
fields in this case, but I don't think there's an easy way to communicate that to attrs.
from cattrs.
Hi @Tinche, as always: thanks for connecting so quickly 🙃 🥇
I assume you're using
from __future__ import annotations
?
Yes, absolutely. Somehow got lost when copy-pasting.
cattrs calls
attrs.resolve_types
on every class it sees, which calls intotyping.get_type_hints
which explodes. I don't think there's an easy way for us to be selective about which fields we want resolved, unfortunately. cattrs only wantsinit=True
fields in this case, but I don't think there's an easy way to communicate that to attrs.
The sequence of calls sounds logical and is what I would have expected to happen in the back. However – and perhaps this is a stupid question because I don't know the cattrs
internals in depth – why is this an attrs
issue? In my naive opinion: couldn't this problem be avoided by "simply" calling attrs.resolve_types
only on those types that actually need to be resolved? That is, for any class in a given code, it's either "class needs to be serialized -> cattrs/converter needs to bother" or "class needs no serialization, e.g. because it only appears with init=False
-> cattrs/converter can ignore it". So if resolve_types
was only called on the former, the problem wouldn't arise, right?
from cattrs.
Here's the actual piece of code that calls attrs.resolve_types
:
cattrs/src/cattrs/gen/__init__.py
Line 102 in 898e59c
To help with clarity, let's distinguish between classes and fields. We call attrs.resolve_types
on classes, which resolves their fields so we can use them.
So in this case, cattrs will call attrs.resolve_types(Outer)
because that will ensure the following call of attrs.fields(Outer)
will return the proper metadata (that's just how attrs introspection works in general).
Now, we could make this condition a little more sophisticated and account for init=False
fields, but in your example, presumably Outer
would have some other attributes you would want serialized. Those annotations would need to be de-stringified to work. And we can't call resolve_types
and tell it to just resolve init
fields.
from cattrs.
Ah ok, now I get it, thanks for the clarification 👍🏼 So it is an attrs
issue, indeed ... Given that cattrs
is the companion package of attrs
, do you think there is a chance to tackle it from that side to enable such use cases for cattrs
? After all, I've understood that both packages deeply care about performance and this really is a performance-related issue ;)
from cattrs.
Just had a quick look at the source code of resolve_types
and noticed that it takes an optional attribs
parameter that in fact let's control which fields to loop over:
https://github.com/python-attrs/attrs/blob/5ce5d8278f162ec7542a251091427fd88e538554/src/attr/_funcs.py#L463C9-L463C66
In principle, that would offer an easy solution, no? So instead of calling resolve_types
with the class only, one could filter down to init=True
attributes only and thus solve it from the cattrs
side 🤔
from cattrs.
Related Issues (20)
- Constructor selection for structuring HOT 7
- Tagged Union: How to do structure/unstructure hook(s) for particular member type? HOT 4
- A dictionary gets structured into a list without errors/warnings HOT 2
- Question: Correct use of converters HOT 3
- Documentation does not explain unstruct_collection_overrides' input HOT 4
- Detailed validation exception groups with hook errors HOT 2
- cattrs effectively disables attrs validation HOT 3
- Covert to defaultdict instead of plain dict HOT 4
- How to recursively unstructure with hooks? HOT 5
- Dataclass structuring fails with misleading error message.
- Default field conversion conflicts with user-defined field converter function HOT 3
- [macOS] Error when trying to run tests: `ImportError: cannot import name 'CodecOptions' from 'bson'` HOT 2
- How to hook into structuring of a simple dict? HOT 6
- Incorrect dispatch in Python 3.12 HOT 4
- Why does structuring of datetime in attrs class need a structure hook? HOT 1
- `prefer_attrib_converters` doesn't work when the field is aliased HOT 2
- Register structure hook only for optional types HOT 2
- Problem with tagged union example HOT 3
- Subclass disambiguation for nested structures. HOT 3
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 cattrs.