-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathrepro_issue_382.py
More file actions
69 lines (54 loc) · 1.72 KB
/
repro_issue_382.py
File metadata and controls
69 lines (54 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import sys
# Use local Fire without installing.
sys.path.insert(0, os.path.dirname(__file__))
# Fire depends on termcolor for pretty output; to keep this repro standalone
# in minimal environments, provide a tiny stub.
try:
import termcolor # type: ignore
except ModuleNotFoundError: # pragma: no cover
import types
termcolor = types.SimpleNamespace(colored=lambda s, *args, **kwargs: s)
sys.modules["termcolor"] = termcolor
import fire
class Foo:
def bar(self):
return "bar"
def do_stuff(self, arg1):
return f"stuff {arg1}"
def main() -> None:
# Enable completion output.
# In Fire, `-- --completion` is passed after the Fire arguments separator.
# We'll call Fire in a way that simulates CLI invocation.
#
# The bug report says that completion output includes a `--self` flag, which should
# never be suggested to the user.
argv = [
"prog",
"--",
"--completion",
]
# Fire uses sys.argv.
old_argv = sys.argv
try:
sys.argv = argv
# Fire writes completion script to stdout. Capture by redirecting.
from io import StringIO
import contextlib
buf = StringIO()
with contextlib.redirect_stdout(buf):
try:
fire.Fire(Foo)
except SystemExit as e:
# Fire may exit after printing completion.
if e.code not in (0, None):
raise
out = buf.getvalue()
finally:
sys.argv = old_argv
assert "--self" not in out, (
"Completion output must not include the implicit 'self' parameter as a flag.\n"
f"Output was:\n{out[:2000]}"
)
if __name__ == "__main__":
main()