python / python/cpython

Conditional annotations are always included in `STRING` annotations

Open
#156,515 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.14 3.15 3.16 stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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
  • gh-156516

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the example with get_annotations and Format.STRING first, then inspect the annotationlib implementation and its relevant tests. Done means conditional annotations such as b are absent from STRING results, matching VALUE and FORWARDREF behavior, with the regression covered by passing tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.