Skip to content

Commit 88f6574

Browse files
committed
Use 'is' in place of '==' for determining whether Python object has changed in Fire.
Copybara generated commit for Python Fire. PiperOrigin-RevId: 149692390 Change-Id: I6fb5413ba2c9d5eed654d6710f8cc756180e90dc Reviewed-on: https://team-review.git.corp.google.com/63904 Reviewed-by: David Bieber <[email protected]>
1 parent 8e29478 commit 88f6574

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

fire/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def _Fire(component, args, context, name=None):
339339
component_trace.AddError(error, initial_args)
340340
return component_trace
341341

342-
if last_component == initial_component:
342+
if last_component is initial_component:
343343
# If the initial component is a class, keep an instance for use with -i.
344344
instance = component
345345

@@ -419,13 +419,13 @@ def _Fire(component, args, context, name=None):
419419
or inspect.isroutine(last_component)):
420420
remaining_args = saved_args
421421
component_trace.AddSeparator()
422-
elif component != last_component:
422+
elif component is not last_component:
423423
remaining_args = [separator] + saved_args
424424
else:
425425
# It was an unnecessary separator.
426426
remaining_args = saved_args
427427

428-
if component == last_component and remaining_args == initial_args:
428+
if component is last_component and remaining_args == initial_args:
429429
# We're making no progress.
430430
break
431431

fire/fire_test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ def testBasicSeparator(self):
317317
# The separator triggers a function call, but there aren't enough arguments.
318318
self.assertEqual(fire.Fire(tc.MixedDefaults, 'identity - _ +'), None)
319319

320+
def testNonComparable(self):
321+
"""Fire should work with classes that disallow comparisons."""
322+
self.assertIsInstance(fire.Fire(tc.NonComparable, ''), tc.NonComparable)
323+
324+
# The first separator instantiates the NonComparable object.
325+
# The second separator causes Fire to check if the separator was necessary.
326+
self.assertIsInstance(fire.Fire(tc.NonComparable, '- -'), tc.NonComparable)
327+
320328
def testExtraSeparators(self):
321329
self.assertEqual(
322330
fire.Fire(tc.ReturnsObj, 'get-obj arg1 arg2 - - as-bool True'), True)

fire/test_components.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
"""Thie module has componenets that are used for testing Python Fire."""
15+
"""Thie module has components that are used for testing Python Fire."""
1616

1717
from __future__ import absolute_import
1818
from __future__ import division
@@ -180,3 +180,12 @@ class ErrorRaiser(object):
180180

181181
def fail(self):
182182
raise ValueError('This error is part of a test.')
183+
184+
185+
class NonComparable(object):
186+
187+
def __eq__(self, other):
188+
raise ValueError('Instances of this class cannot be compared.')
189+
190+
def __ne__(self, other):
191+
raise ValueError('Instances of this class cannot be compared.')

fire/test_components_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ def testTestComponents(self):
2828
self.assertIsNotNone(tc.Empty)
2929
self.assertIsNotNone(tc.OldStyleEmpty)
3030

31+
def testNonComparable(self):
32+
with self.assertRaises(ValueError):
33+
tc.NonComparable() != 2 # pylint: disable=expression-not-assigned
34+
with self.assertRaises(ValueError):
35+
tc.NonComparable() == 2 # pylint: disable=expression-not-assigned
36+
3137

3238
if __name__ == '__main__':
3339
unittest.main()

0 commit comments

Comments
 (0)