Skip to content

co_annotations fails with non dict return from __prepare__ #20

Description

@zrothberg

Currently __prepare__ only requires that the returned item implements __setitem__ and __getitem__. It appears that co_annotations require that the object actually must be a subclass of dict. Minimum code to reproduce.

class TestDict:
    def __init__(self):
        self.data = {}
    def __setitem__(self, key, value):
        self.data[key] = value
    def __getitem__(self, item):
        return self.data[item]

class CustomType(type):
    @classmethod
    def __prepare__(metacls, name, bases):
        return TestDict()
    def __new__(metacls, name, bases, namespace, /, **kwargs):
        namespace = namespace.data
        cls = super().__new__(metacls, name, bases, namespace, **kwargs)
        return cls


class A(metaclass=CustomType):
    val:str

A.__annotations__
#errors here 

__new__ does require that the input to its third argument be a subclass of dictionary but any metaclass that handles it like above would be avoiding that issue.

Not sure if this is just a bad assertion and its not actually an issue for the code or if it is an issue with the code.

Activity

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

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions