**kwargs unexpectedly parses flags for later chained method

Open
#659 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
55/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
python
Domain
cli

Research direction

Reproduce the behavior with the shown python -m fire command and the chained transform and do_something calls. Start by tracing argument parsing for chained method calls; done means flags after transform are passed to do_something instead of being absorbed into transform's kwargs, with coverage for this example.

Written by the indexing model from the issue text.

Description

Suppose we have the following program

from dataclasses import dataclass

@dataclass
class MyClass:
    x: int

    def transform(self, **kwargs):
        return MyClass(self.x * kwargs.get("multiplier", 2))

    def do_something(self, msg):
        print(f"{msg}: {self.x}")

my_obj = MyClass(3)

And we call it with python -m fire my_module my_obj transform --multiplier=3 do_something --msg="test", then the --msg is parsed when calling transform into the kwargs dict. Then it errors saying --msg is missing. Can we instead support parsing flags before the next chained method call? The kwargs is useful for example, to transform the object in ways that only implementation classes know what to do.

My current workarounds are (1) pass msg as positional arg to do_something, (2) have do_something take **kwargs and call transform within it. Neither is ideal as (1) would surprise anyone who uses my code and (2) adds extra plumbing that could be avoided.

Dominant language
Python
Stars
28.2k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from google/python-fire

All issues in google/python-fire

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.