Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

co_annotations fails with non dict return from __prepare__ #20

Open
zrothberg opened this issue Mar 14, 2023 · 0 comments
Open

co_annotations fails with non dict return from __prepare__ #20

zrothberg opened this issue Mar 14, 2023 · 0 comments

Comments

@zrothberg
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant