Skip to content

__setattr__ not called on Metaclass #1661

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

Closed
slozier opened this issue Feb 24, 2023 Discussed in #1660 · 1 comment · Fixed by #1670
Closed

__setattr__ not called on Metaclass #1661

slozier opened this issue Feb 24, 2023 Discussed in #1660 · 1 comment · Fixed by #1670
Labels
confirmed core Issues relating to core functionality (e.g. syntax)

Comments

@slozier
Copy link
Contributor

slozier commented Feb 24, 2023

Discussed in #1660

Originally posted by zwadar February 23, 2023
Hi,
I'm using 3.4.0 and trying to use setattr on a metaclass but it doesn't seem to be called. As far as I can see this should be supported and I don't see any issues reported, so I was wondering if I might be doing something wrong?

class Test(type):
    def __new__(cls, name, bases, namespace):
        cls = super(Test, cls).__new__(cls, name, bases, namespace)
        return cls        

    def __setattr__(cls, attr, value):
        print("Called Set Attr")
        super(Test, self).__setattr__(attr, value)

class TestMeta(object, metaclass=Test):
    aaa = 10

TestMeta.aaa = 80
print(TestMeta.aaa)

but the printed output is just

80

Can I somehow overcome it? This is pretty crucial to me.

Thank you


Here's a potential test case:

class Test(type):
    def __setattr__(cls, attr, value):
        super().__setattr__("last_attr", (attr, value))
        super().__setattr__(attr, value)

class TestMeta(metaclass=Test):
    aaa = 10

TestMeta.aaa = 80

assert TestMeta.aaa == 80
assert TestMeta.last_attr == ("aaa", 80)
@slozier slozier added core Issues relating to core functionality (e.g. syntax) confirmed labels Feb 24, 2023
@zwadar
Copy link

zwadar commented Feb 24, 2023

fiy I had tried to fix it by changing the line
https://github.com/IronLanguages/ironpython3/blob/master/Src/IronPython/Runtime/Types/PythonType.cs#L1304

to
((BuiltinMethodDescriptor)pts).DeclaringType == typeof(PythonType);

and it actually worked, except it had broke ctypes tests. But I supposes this method is the one that needs fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed core Issues relating to core functionality (e.g. syntax)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants