Skip to content

annotationlib: namespace can be non-dict #132426

Description

@eliegoudout

Hello!

I think the following, made so that get_annotate_function is usable in metaclasses' __new__, should check for Mapping since __prepare__ can provide any mapping (see picture from PEP3115.

if isinstance(obj, dict):

Image

Linked PRs

Activity

  1. added a commit that references this issue on Apr 12, 2025
    93afac8
  2. eliegoudout commented on Apr 12, 2025

    @eliegoudout
    Author

    However, I now realize that this is a major issue since there might be a conflict between the Mapping subtype's own annotations and the annotations it contains as a mapping for another type's namespace.

    Shouldn't we separate get_annotate_function into get_annotate_function and get_namespace_annotations? Or maybe add a keyword get_annotate_function(obj, *, is_namespace: bool)?

  3. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Apr 12, 2025
  4. picnixz commented on Apr 12, 2025

    @picnixz
    Member
  5. unpinned this issue on Apr 12, 2025
  6. JelleZijlstra commented on Apr 13, 2025

    @JelleZijlstra
    Member

    Good point! I think what I'd like to do is drop get_annotate_function completely (it's now equivalent to a simple getattr call and doesn't need to be a function) and add a new function get_annotate_from_class_namespace.

    Your proposed change is a bit dubious because "mapping" at the C level means something a little different (it basically just checks for the mp_subscr slot). Not sure if that will cause practical problems but better to be safe.

    However, I now realize that this is a major issue since there might be a conflict between the Mapping subtype's own annotations and the annotations it contains as a mapping for another type's namespace.

    Since instances no longer have accessible annotations by default, this will only be a problem in exotic cases like a Mapping that is also an instance of type. But again, better to be safe.

  7. added a commit that references this issue on Apr 14, 2025
  8. added a commit that references this issue on May 4, 2025
  9. JelleZijlstra commented on May 4, 2025

    @JelleZijlstra
    Member

    Fixed by #132490, thanks for the report!

  10. added a commit that references this issue on Jul 12, 2025
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

    3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-typingtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions