Skip to content

Can Fire work with classes that use Singleton-style metaclasses? #687

Description

@glennpow

I use Singletons in my project (I know, I know, that's a whole other discussion), but I'm running into an issue when I try to also use one of those classes as a Fire CLI component, since it makes use or a metaclass that overrides __call__(). I believe this is messing with Fire's ability to parse the class to see which arguments are viable. Here's a simple example:

import fire

class MyMetaclass(type):
    def __call__(cls, *args, **kwargs):
        # This would be some generic Singleton logic...
        instance = super(MyMetaclass, cls).__call__(*args, **kwargs)
        return instance

class MyWithoutMetaclass:
    def foo(self):
        print("MyWithoutMetaclass.foo()")

class MyWithMetaclass(metaclass=MyMetaclass):
    def foo(self):
        print("MyWithMetaclass.foo()")

if __name__ == "__main__":
    # This works
    # fire.Fire(MyWithoutMetaclass)

    # This fails, since it tries to pass "foo" in as an argument into the class instantiation
    fire.Fire(MyWithMetaclass)

I'm sure this is because of the splatted args and kwargs in the metaclass, but this is supposed to be a generic metaclass for the entire project. Is there no clean way that I can get this to work?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions