-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
TYPE_CHECKING and init=False #531
Comments
I assume you're using cattrs calls |
Hi @Tinche, as always: thanks for connecting so quickly 🙃 🥇
Yes, absolutely. Somehow got lost when copy-pasting.
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 |
Here's the actual piece of code that calls cattrs/src/cattrs/gen/__init__.py Line 102 in 898e59c
To help with clarity, let's distinguish between classes and fields. We call So in this case, cattrs will call Now, we could make this condition a little more sophisticated and account for |
Ah ok, now I get it, thanks for the clarification 👍🏼 So it is an |
Just had a quick look at the source code of |
Due to serialization issues: python-attrs/cattrs#531
Due to serialization issues: python-attrs/cattrs#531
Due to serialization issues: python-attrs/cattrs#531
Due to serialization issues: python-attrs/cattrs#531
Due to serialization issues: python-attrs/cattrs#531
Due to serialization issues: python-attrs/cattrs#531
Before we give up on this issue, quickly pulling in @hynek with the hope he could share his opinion on the @hynek, quick summary for you, so you don't need to go through the entire chain of messages. The described problem is roughly:
Can we somehow circumvent the problem? My current (unsatisfactory workaround) is that I omit the types from the code and only have them in a comment for me as a reminder, losing the ability to fully type-check my classes. |
Heh you’re asking the guy who asks @Tinche whenever he runs into a tricky typing problem. I’m afraid his replies are as good as it gets. |
Description
Hi, I think I'm facing a similar situation as described in #160 but with a few subtleties that potentially motivate refining the internal type resolution of
cattrs
.What I Did
Consider the following two files:
Running the latter gives
NameError: name 'Inner' is not defined
because the inner class is not available inouter.py
.While one could fix this by informing
cattrs
about the class before attempting to unstructure, I don't think it's a convenient solution in my case because I don't even see a reason why it should be necessary in the first place: the corresponding attribute isinit=False
and with the default settings where such fields are ignored,cattrs
shouldn't even need to bother about the field at all ... assuming of course that I do not overlook some other reason.The motivation for my example: in my case,
Inner
relies on some heavy external dependencies (such astorch
) and many execution paths of my code don't actually require loading it, so I instead use lazy-loading for improved startup time. That said, I wouldn't want to load it just to satisfycattrs
to perform an unstructuring operation that actually does not require the class due toinit=False
.For now, I can simply remove that type annotation, which solves the problem, but this is of course suboptimal. Any thoughts would be highly appreciated.
The text was updated successfully, but these errors were encountered: