Skip to content

How to deal with classmethod or staticmethod? #113

Closed

Description

@ssikiki
import fire
class Calculator(object):
  """A simple calculator class."""
  base = 10

  def __init__(self, x):
    self.x = x

  def double(self, number):
    return self.x * number

  @classmethod
  def test(cls, number):
    return cls.base * number

if __name__ == '__main__':
  fire.Fire(Calculator)

=== a.py =====================================
If I want to call func test, I need to do like this: python a.py --x 3 test 5
So how to call classmethod or staticmethod without initialize the class (python a.py test 5 )?

Activity

  1. dbieber commented on Jan 30, 2018

    @dbieber
    Collaborator

    Thanks for raising this to my attention.
    At the moment you do need to initialize the class, so python a.py --x 3 test 5 will work while python a.py test 5 will not.
    We should either fix or document this.

  2. rozuur commented on Oct 3, 2018

    @rozuur

    can I fix this?

  3. rozuur commented on Oct 6, 2018

    @rozuur

    @dbieber I have fixed the issue, let me know if the approach is correct so that I can add test cases to it.

  4. dbieber commented on Oct 6, 2018

    @dbieber
    Collaborator

    Thanks for the fix! Yes, the approach looks good to me. It's a little non-standard for Fire because it updates component twice in the same iteration of the main loop, but that may be OK. One place where we'll have to be careful is if the command ends on an access of a class method or static method, without calling it (e.g. because the user wants help about a class method). We'll have to make sure we handle this case properly.

    Let's add some thorough tests and check this in.

    The linter is rather picky (currently complaining about line-too-long errors.) You can see test and lint output in travis by clicking the red x (or green checkmark) next to your commit / pull request.

  5. rozuur commented on Oct 7, 2018

    @rozuur

    Will add test cases, fix lint errors and update the pull requests.

  6. dbieber commented on Oct 31, 2018

    @dbieber
    Collaborator

    Apologies for the delay on this, I'll keep you posted as I get round to reviewing this.

  7. dbieber commented on Jan 3, 2019

    @dbieber
    Collaborator

    I have (finally) responded to your pull request.
    If you're up for making the requested changes, go for it :). If you've since moved on to other parts of the internet and I don't hear back from you, no worries - my apologies for the slow turnaround.

  8. dbieber commented on Jul 24, 2019

    @dbieber
    Collaborator

    This is now fixed (unintentionally, actually!) It was fixed by the commit that improved support for callable objects (0e1dfe5), allowing users to both call or access members of a callable object. It fixes this issue because it also allows users to both instantiate a class or access its members (such as a classmethod or staticmethod.)

    The fix will be available in this coming Friday's release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

      ⌂ Proxy