google / google/python-fire

**kwargs unexpectedly parses flags for later chained method

Offen
#659 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
Python
Sterne
28.2k
Forks
1.5k
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

Suppose we have the following program

```python
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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.