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

fix: AttributeError by checking method types in custom metaclasses #125

Merged

Conversation

fkapsahili
Copy link
Contributor

@fkapsahili fkapsahili commented Apr 16, 2024

Thanks for the great package!

I had a case where I tried to inherit from EnforceOverrides when using a custom metaclass, inheriting from abc.ABCMeta at the same time. As this was not possible due to Python's requirement that the metaclass of a subclass must be a subclass of the metaclass of all its bases, I ended up importing EnforceOverridesMeta directly from overrides.enforce and using it as the base of my custom metaclass.

Here is an example when using the EnforceOverridesMeta on a custom metaclass:

from overrides.enforce import EnforceOverridesMeta, EnforceOverrides

class MyMeta(EnforceOverridesMeta):
    def foo(self):
        pass

class MyClass(EnforceOverrides, metaclass=MyMeta):
    def bar(self):
        pass

Attempting to set the __ignored__ attribute on foo raises an AttributeError because the method objects, particulary those bound to a class, do not support attribute assignments.

I think it would be great if we could export EnforceOverridesMeta from the package directly and use it as the base of our own custom metaclasses.

This PR exports the EnforceOverridesMeta metaclass from the package and implements a check for the type of method objects in the metaclass to differentiate between function objects and other callable types that do not support attribute assignments. By identifying function types by types.FunctionType, this fix avoids the AttributeError.

@fkapsahili fkapsahili marked this pull request as ready for review April 16, 2024 12:51
@mkorpela mkorpela merged commit 5c5bdaa into mkorpela:main May 11, 2024
6 checks passed
@mkorpela
Copy link
Owner

@fkapsahili thank you for the contribution!

@fkapsahili
Copy link
Contributor Author

@mkorpela Many thanks. Would it be possible for you to release a new patch version 7.7.1 on pypi?

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

Successfully merging this pull request may close these issues.

2 participants