Repository navigation
Incorrect format passed to __annotate__ function #125507
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errorstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory3.14bugs and security fixesbugs and security fixes
on Oct 15, 2024 Ok, this is not so easy. What happens here:
- We try to get
__annotations__first withformat=VALUEviaLine 817 in aa18fd5
ann = _BASE_GET_ANNOTATIONS(obj) - If it fails with a
NameErrorwe actually try to call__annotate__withformat=FORWARDREF:Lines 692 to 693 in aa18fd5
# But if __annotations__ threw a NameError, we try calling __annotate__ ann = _get_and_call_annotate(obj, format)
My code in #122262 never had any
NameErrors, so it never actually received the second callBut the question is: is this a good API? For me it feels like a very nuanced one with a lot of corner-cases.
- We try to get
Practical question, how can I differentiate two calls in
Lines 1614 to 1630 in 3b26e36
def annotate_method(format): typing = sys.modules.get("typing") if typing is None and format == annotationlib.Format.FORWARDREF: typing_any = annotationlib.ForwardRef("Any", module="typing") return { ann: typing_any if t is _ANY_MARKER else t for ann, t in annotations.items() } from typing import Any ann_dict = { ann: Any if t is _ANY_MARKER else t for ann, t in annotations.items() } if format == annotationlib.Format.STRING: return annotationlib.annotations_to_string(ann_dict) return ann_dict - When calling with
format=VALUEthe first time, so it can be later called withformat=FORWARDREF - When calling with
format=VALUEthe only time, because this is what was asked by the end user
- When calling with
Would #124415 help here?
@JelleZijlstra hm, I don't think so. Here's my simplified reproducer with #124415 applied:
import annotationlib class My: def __annotate__(format): assert format == annotationlib.Format.FORWARDREF, format # Fails with: `AssertionError: 1` return {} annotationlib.get_annotations(My, format=annotationlib.Format.FORWARDREF)
So, looks like it does still make the first call with
format=1, even when asked to get forward refs explicitly.I understand the issue now. You want to return
typing.Anyin VALUE format, but in FORWARDREF format you want to avoid importingtypingand returnForwardRef("typing.Any")directly.For FORWARDREF format, we currently try using
__annotations__directly before we call__annotate__(FORWARDREF). If we change the order in which these operations happen, we could avoid your issue. I'll work on a PR for that.Reacted by sobolevnThough I'm curious if @larryhastings and @carljm have opinions here: If an object has
__annotations__that can be retrieved without error, but it also has an__annotate__that directly works with the FORWARDREF format, which should we prefer?At the sprint, we changed it so we preferred returning
__annotations__over evaluating__annotate__in the fake globals environment to implement the FORWARDREF format, which makes sense to me. But if a user-written__annotate__supports FORWARDREF directly, shouldn't we believe it?To me it seems like a useful, and sensible, invariant that if you successfully get
VALUESannotations (the default), thenFORWARDREFSannotations will be identical to that.FORWARDREFSis intended for cases whereVALUESwould fail withNameError, and the API is designed around that assumption. I would prefer not to permit objects whereFORWARDREFSannotations permanently differ fromVALUESannotations. So I would rather not make changes here.To me, the proposed approach for dataclasses seems like over-optimization. I think it's fine to always import
typing-- someone is introspecting an annotation that usestyping.Any, after all. There may be other possibilities to avoid this, but I would need to look at the problem more.Reacted by larryhastingsWhat Carl said. This should be documented behavior: if
__annotations__is set, that value will be returned forVALUESorFORWARDREFformats and__annotate__won't be called.Thanks, then let's stick with the current behavior.
Bug report
Consider this example:
This will fail with:
I think that we should still pass the format in
_BASE_GET_ANNOTATIONS.I will try to fix this, since this affects my other PR: #122262
CC @JelleZijlstra
Linked PRs