Skip to content

Name lookup in annotation scopes in classes does not work with non-dict globals #119821

Closed

Description

@JelleZijlstra

Bug report

Bug description:

This works:

class customdict(dict):
    def __missing__(self, key):
        return key

code = compile("type Alias = undefined", "test", "exec")
ns = customdict()
exec(code, ns)
Alias = ns["Alias"]
assert Alias.__value__ == "undefined"

But this does not:

code = compile("class A: type Alias = undefined", "test", "exec")
ns = customdict()
exec(code, ns)
Alias = ns["A"].Alias
assert Alias.__value__ == "undefined"

This is a problem for PEP 649 because we're going to rely on a non-dict globals namespace.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added 5 commits that reference this issue on May 31, 2024
  2. encukou commented on Jun 3, 2024

    @encukou
    Member

    3.12 refleak buildbots started failing when the backport was merged.

    $ ./python -m test test_type_params -R:
    Using random seed: 3855735070
    0:00:00 load avg: 0.70 Run 1 test sequentially
    0:00:00 load avg: 0.70 [1/1] test_type_params
    beginning 9 repetitions. Showing number of leaks (. for 0 or less, X for 10 or more)
    12345:6789
    XXXXX XXXX
    test_type_params leaked [12, 12, 12, 12] references, sum=48
    test_type_params leaked [8, 8, 8, 8] memory blocks, sum=32
    test_type_params failed (reference leak)
    
    == Tests result: FAILURE ==
    
    1 test failed:
        test_type_params
    
    Total duration: 326 ms
    Total tests: run=83
    Total test files: run=1/1 failed=1
    Result: FAILURE
    
    
  3. reopened this on Jun 3, 2024
  4. added 2 commits that reference this issue on Jun 3, 2024
  5. JelleZijlstra commented on Jun 5, 2024

    @JelleZijlstra
    MemberAuthor

    Thanks @encukou for noticing the refleak! This has been fixed now.

  6. added a commit that references this issue on Jul 11, 2024
  7. added a commit that references this issue on Jul 17, 2024
  8. added a commit that references this issue on Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions