Skip to content

Conditional annotations are always included in STRING annotations #156515

Description

@DavidCEllis

Bug report

Bug description:

If an annotation is excluded via a conditional block it is correctly excluded in VALUE and FORWARDREF annotations but is incorrectly included under STRING annotations.

from annotationlib import get_annotations, Format

class Example:
    a: int
    if False:
        b: str

print(get_annotations(Example))
print(get_annotations(Example, format=Format.STRING))
{'a': <class 'int'>}
{'a': 'int', 'b': 'str'}

'b' should not be included in string annotations here.

CPython versions tested on:

3.14, 3.15, CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions