-
-
Notifications
You must be signed in to change notification settings - Fork 31.2k
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
__annotate__
does not get called if __annotations__
exist
#122285
Comments
(Not sure if it's really a bug or not, so feel free to remove the label; but I think it should only affect 3.14) |
I'm going to say this is working the way it should be working. If a class has annotations, the compiler generates an In your second example, there are no annotations, so the compiler does not generate an So my recommendation is not to change anything here. We could add some tests covering these cases, though. And if anyone thinks this needs bigger discussion, I can add a section discussing these cases to PEP-749. |
I think that this case should be discussed. As it removes the potential use-case for things that are defined using annotations DSL, like dataclasses, attrs, pydantic models, validation, etc. Let's say: class MyObject:
field: 'heavy_import.Something'
def __annotate__(self, format):
import heavy_import
return {'field': heavy_import.Something} ^^^ This won't work as well. And this also seem like a big potential use-case, when people use |
We might also need to invite people from pydantic and attrs to ask them, I am sure they have a lot of potential use-cases. |
Libraries like pydantic or attrs have code that can run after the class is defined (either For your |
Yes, this is what I was thinking of :)
But, note that
Because this will affect code paths that don't specifically ask for annotations. Basically, this is the same as importing For real use-case:
So, I opened this issue. My proposal: let's ask maintainers of these two libs at least, feedback is always great to have for such big features. If they don't have any problems, it would be great! :) If they do, we can address them early. |
Not true, it can be a ForwardRef.
This should already be possible by making the dataclass decorator set a different
Feel free to do so! |
I'm clearly missing something.
Can you please provide a code sample for that? |
I'm not sure I fully understand what you want to do either. What I was thinking was that dataclasses could do something like:
But I don't really see the point; people shouldn't be using string annotations in the first place. |
Now I am more inclined to agree with the current implementation 👍 , but I think we can leave this open until we ask pydantic and attrs teams, just to be safe (working on this) :) |
This code:
Produces:
PEP 649 specifies this as: https://peps.python.org/pep-0649/#annotate-and-annotations
Comment them out:
Produces:
Let's discuss this.
The text was updated successfully, but these errors were encountered: