Skip to content

Commit bc43dca

Browse files
roopeshvscopybara-github
authored andcommitted
Support functions even when they override getattr in non-standard ways.
This resolves an issue with Beautiful Soup. Copybara import of the project: -- f4cd8e9 by Roopesh V S <[email protected]>: COPYBARA_INTEGRATE_REVIEW=#281 from roopeshvs:master f4cd8e9 PiperOrigin-RevId: 347892296 Change-Id: Ifbbfc9397ce9623cd1a354599252826ac7ddcc92
1 parent a938ef9 commit bc43dca

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

fire/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def main(argv):
7575
import six
7676

7777
if six.PY34:
78-
import asyncio # pylint: disable=g-import-not-at-top,import-error # pytype: disable=import-error
78+
import asyncio # pylint: disable=import-error,g-import-not-at-top # pytype: disable=import-error
7979

8080

8181
def Fire(component=None, command=None, name=None):

fire/decorators.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def SetParseFns(*positional, **named):
7171
def _Decorator(fn):
7272
parse_fns = GetParseFns(fn)
7373
parse_fns['positional'] = positional
74-
parse_fns['named'].update(named)
74+
parse_fns['named'].update(named) # pytype: disable=attribute-error
7575
_SetMetadata(fn, FIRE_PARSE_FNS, parse_fns)
7676
return fn
7777

@@ -85,15 +85,31 @@ def _SetMetadata(fn, attribute, value):
8585

8686

8787
def GetMetadata(fn):
88+
# type: (...) -> dict
89+
"""Gets metadata attached to the function `fn` as an attribute.
90+
91+
Args:
92+
fn: The function from which to retrieve the function metadata.
93+
Returns:
94+
A dictionary mapping property strings to their value.
95+
"""
8896
# Class __init__ functions and object __call__ functions require flag style
8997
# arguments. Other methods and functions may accept positional args.
9098
default = {
9199
ACCEPTS_POSITIONAL_ARGS: inspect.isroutine(fn),
92100
}
93-
return getattr(fn, FIRE_METADATA, default)
101+
try:
102+
metadata = getattr(fn, FIRE_METADATA, default)
103+
if ACCEPTS_POSITIONAL_ARGS in metadata:
104+
return metadata
105+
else:
106+
return default
107+
except: # pylint: disable=bare-except
108+
return default
94109

95110

96111
def GetParseFns(fn):
112+
# type: (...) -> dict
97113
metadata = GetMetadata(fn)
98114
default = dict(default=None, positional=[], named={})
99115
return metadata.get(FIRE_PARSE_FNS, default)

fire/inspectutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import six
2828

2929
if six.PY34:
30-
import asyncio # pylint: disable=g-import-not-at-top,import-error # pytype: disable=import-error
30+
import asyncio # pylint: disable=import-error,g-import-not-at-top # pytype: disable=import-error
3131

3232

3333
class FullArgSpec(object):

0 commit comments

Comments
 (0)