GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — annotationlib: `ref.evaluate()` not working for generics as local variables · Issue #138151 · python/cpython

annotationlib: ref.evaluate() not working for generics as local variables #138151

New issue
Copy link
New issue
Copy link
Closed
Closed
annotationlib: ref.evaluate() not working for generics as local variables#138151
Copy link
Labels
stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@dr-carlos
dr-carlos
opened on Aug 26, 2025
Issue body actions

Bug report

Bug description:

Usually, when a local variable is introduced after getting a ForwardRef, evaluate() picks this up and correctly evaluates it using the self.__cell__ short-circuit. However, this does not work when that local variable is a generic. It does still work for globals.

from annotationlib import Format, get_annotations

def works_global():
    class Demo:
        x: sequence_a[int]

    fwdref = get_annotations(Demo, format=Format.FORWARDREF)['x']
    print(f"Global {fwdref!r}")

    global sequence_a
    sequence_a = list
    evaluated = fwdref.evaluate()
    print(f"Evaluated {evaluated!r}")

def works_nongeneric():
    class Demo:
        nonlocal alias # Optional
        x: alias

    fwdref = get_annotations(Demo, format=Format.FORWARDREF)['x']
    print(f"Global {fwdref!r}")

    alias = int
    evaluated = fwdref.evaluate()
    print(f"Evaluated {evaluated!r}")


def fails():
    class Demo:
        nonlocal sequence_b
        x: sequence_b[int]

    fwdref = get_annotations(Demo, format=Format.FORWARDREF)['x']
    print(f"Local {fwdref!r}")

    sequence_b = list
    evaluated = fwdref.evaluate() # NameError
    print(f"Evaluated {evaluated!r}")


works_global()
works_nongeneric()
fails()

CPython versions tested on:

CPython main branch, 3.14

Operating systems tested on:

Linux

Linked PRs

  • gh-138151: Fix ForwardRef.evaluate() for locally defined generics #138161
  • gh-138151: Fix annotationlib handling of multiple nonlocals #138164
  • [3.14] gh-138151: Fix annotationlib handling of multiple nonlocals (GH-138164) #140949

Activity

  1. dr-carlos
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Aug 26, 2025
  2. JelleZijlstra commented on Aug 26, 2025

    @JelleZijlstra
    JelleZijlstra
    on Aug 26, 2025
    Member
    More actions

    I wouldn't expect your fails() to work: you set sequence_b only in that function's local scope, and that's not the scope the annotation was defined in.

    Reacted by dr-carlos
  3. JelleZijlstra
    added
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Aug 26, 2025
  4. dr-carlos commented on Aug 26, 2025

    @dr-carlos
    dr-carlos
    on Aug 26, 2025
    ContributorAuthor
    More actions

    I wouldn't expect your fails() to work: you set sequence_b only in that function's local scope, and that's not the scope the annotation was defined in.

    Thanks, this is a good point, I forgot to consider that when writing up the example.
    I've edited the OP with more complete examples, and put the classes inside the function definitions so the scoping should be correct.

  5. JelleZijlstra commented on Aug 26, 2025

    @JelleZijlstra
    JelleZijlstra
    on Aug 26, 2025
    Member
    More actions

    The problem here is that we don't properly preserve a reference to the cell object that was used to create the ForwardRef. This should be done when we create the ForwardRef (i.e., in get_annotations or code called from it), not in ForwardRef.evaluate. We can probably fix this by putting more information in the __cell__ attribute of the ForwardRef.

    Reacted by dr-carlos
  6. JelleZijlstra
    removed
    pendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is provided
    on Aug 26, 2025
  7. JelleZijlstra
    added a commit that references this issue on Aug 26, 2025

    pythongh-138151: Fix annotationlib handling of multiple nonlocals

    1c86ad9
  8. StanFromIreland
    added
    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
    on Aug 26, 2025
  9. dr-carlos commented on Aug 27, 2025

    @dr-carlos
    dr-carlos
    on Aug 27, 2025
    ContributorAuthor
    More actions

    @JelleZijlstra Is #138161 still necessary given the broader fix in #138164 , or should I close it?

  10. JelleZijlstra commented on Aug 27, 2025

    @JelleZijlstra
    JelleZijlstra
    on Aug 27, 2025
    Member
    More actions

    I don't think #138161 is necessary, but someone will need to review #138164.

    Reacted by dr-carlos
  11. JelleZijlstra
    added a commit that references this issue on Nov 3, 2025

    gh-138151: Fix annotationlib handling of multiple nonlocals (#138164)

    b1027d4
  12. miss-islington
    added a commit that references this issue on Nov 3, 2025

    pythongh-138151: Fix annotationlib handling of multiple nonlocals (py…

    321c693
  13. JelleZijlstra
    added a commit that references this issue on Nov 3, 2025

    [3.14] gh-138151: Fix annotationlib handling of multiple nonlocals (G…

    08012a9
  14. efimov-mikhail commented on Nov 3, 2025

    @efimov-mikhail
    efimov-mikhail
    on Nov 3, 2025
    Member
    More actions

    I've checked that merged PRs fix this issue on main and 3.14. So I'll close this.

    Reacted by dr-carlos
  15. efimov-mikhail
    closed this as completedon Nov 3, 2025
  16. StanFromIreland
    added a commit that references this issue on Dec 6, 2025

    pythongh-138151: Fix annotationlib handling of multiple nonlocals (py…

    552abfa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.