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

fire.Fire() ruins fire.Fire(fire.Fire) #3

Closed
ubershmekel opened this issue Mar 4, 2017 · 5 comments
Closed

fire.Fire() ruins fire.Fire(fire.Fire) #3

ubershmekel opened this issue Mar 4, 2017 · 5 comments

Comments

@ubershmekel
Copy link
Contributor

Hi python-fire,

fire.Fire looked like a very interesting function so I wanted to inspect it with fire.Fire. I was surprised that the interpreter printed out a dictionary with all my locals and globals.

I'm not sure if that's what you guys intended to happen, I guess it kind of is because of fire.Fire() defined as a valid invocation which means fire.Fire(globals() + locals()). Perhaps it's worth considering introducing a different function that would do that? fire.FireThisModule()?

@dbieber
Copy link
Member

dbieber commented Mar 4, 2017

You can certainly inspect the Fire function with Fire!

To inspect a function without calling it, and '-- --help' to your command. Without that, the function will get called. And as you noticed, when Fire is called with no arguments, it spits out all your locals and globals.

@dbieber dbieber closed this as completed Mar 5, 2017
@ubershmekel
Copy link
Contributor Author

ubershmekel commented Mar 5, 2017

Why does a callable class not get called?
Compare the following:

import fire
from __future__ import print_function

lamb = lambda: print(123)

class CallableClass:
    def __call__(self):
        print(123)

def function():
    print(123)

def arity(x, y):
    print(123)

fire.Fire(lamb)
fire.Fire(CallableClass) # note `inspect.getsourcefile` throws when this is used in the interpreter
fire.Fire(CallableClass())
fire.Fire(function)
fire.Fire(arity)
fire.Fire()

Unrelated rant: I personally feel the zero-arity function shouldn't get called even when --help is used. I realize a function might have a help argument but I'd rather break that argument name than the expectation of every single command line app with no arguments (I commonly use --help by itself as I'm sure many others do based on the included warning). Sorry I'm kind of using this issue as a feedback post at this point.

@dbieber
Copy link
Member

dbieber commented Mar 7, 2017

Sorry I'm kind of using this issue as a feedback post at this point.

Not a problem.

I personally feel the zero-arity function shouldn't get called even when --help is used.

Yes, largely I agree. Currently I'm leaning toward having --help behave the same as '-- --help' (e.g. preventing the final fn/class from being called/instantiated) except in the case where the fn accepts a help argument (either explicitly or via **kwargs). I want to as much as possible allow Fire to work on any Python component at all.

Why does a callable class not get called?

This is because we currently only look for argspecs on classes and routines, but not on other objects (in your example it's the instantiated object that is callable, not the class itself).
I am certainly open to changing this behavior. It's a nontrivial change and could introduce ambiguous commands, and so will require careful thought.

fire.Fire(CallableClass) # note inspect.getsourcefile throws when this is used in the interpreter

Good catch. We should fix this.

@dbieber
Copy link
Member

dbieber commented Mar 11, 2017

The getsourcefile issue is resolved now. 1239bdd

@1445drewran
Copy link

1445drewran commented Mar 12, 2017 via email

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

3 participants