Skip to content
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

[3.14] annotationlib - get_annotations with Format.FORWARDREF returns _Stringifier instances instead of ForwardRef for module.type annotations where the module is not loaded #125614

Open
DavidCEllis opened this issue Oct 16, 2024 · 1 comment
Assignees
Labels
type-bug An unexpected behavior, bug, or error

Comments

@DavidCEllis
Copy link
Contributor

DavidCEllis commented Oct 16, 2024

Bug report

Bug description:

Tested on 3.14.0a1 - related to implementation of PEP 649 / PEP 749 - #119180

If a type uses dotted access but can't be evaluated, get_annotations returns _Stringifier objects instead of ForwardRef objects.

from annotationlib import get_annotations, Format

class Example:
    dotted: typing.Any
    undotted: Any

ann = get_annotations(Example, format=Format.FORWARDREF)

for name, value in ann.items():
    print(f"{name!r}: {value!r} | type: {type(value)!r}")

Output:

'dotted': typing.Any | type: <class 'annotationlib._Stringifier'>
'undotted': ForwardRef('Any') | type: <class 'annotationlib.ForwardRef'>

I'd expect to see ForwardRef('typing.Any') here so this was surprising. I also found another bug related to dotted access but not related to Stringifiers that I'll raise separately.

CPython versions tested on:

3.14

Operating systems tested on:

Linux

Linked PRs

@DavidCEllis DavidCEllis added the type-bug An unexpected behavior, bug, or error label Oct 16, 2024
@JelleZijlstra JelleZijlstra self-assigned this Oct 16, 2024
@JelleZijlstra
Copy link
Member

This is because we maintain a list of Stringifiers so we can turn them into ForwardRefs later, but we don't track new Stringifiers that are created from an attribute access on another one. The fix should be to also register those new ones on the _StringifierDict.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants