Skip to content

Commit

Permalink
Use 'is' in place of '==' for determining whether Python object has c…
Browse files Browse the repository at this point in the history
…hanged 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]>
  • Loading branch information
dbieber committed Mar 9, 2017
1 parent 8e29478 commit 88f6574
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 4 deletions.
6 changes: 3 additions & 3 deletions fire/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ def _Fire(component, args, context, name=None):
component_trace.AddError(error, initial_args)
return component_trace

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

Expand Down Expand Up @@ -419,13 +419,13 @@ def _Fire(component, args, context, name=None):
or inspect.isroutine(last_component)):
remaining_args = saved_args
component_trace.AddSeparator()
elif component != last_component:
elif component is not last_component:
remaining_args = [separator] + saved_args
else:
# It was an unnecessary separator.
remaining_args = saved_args

if component == last_component and remaining_args == initial_args:
if component is last_component and remaining_args == initial_args:
# We're making no progress.
break

Expand Down
8 changes: 8 additions & 0 deletions fire/fire_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ def testBasicSeparator(self):
# The separator triggers a function call, but there aren't enough arguments.
self.assertEqual(fire.Fire(tc.MixedDefaults, 'identity - _ +'), None)

def testNonComparable(self):
"""Fire should work with classes that disallow comparisons."""
self.assertIsInstance(fire.Fire(tc.NonComparable, ''), tc.NonComparable)

# The first separator instantiates the NonComparable object.
# The second separator causes Fire to check if the separator was necessary.
self.assertIsInstance(fire.Fire(tc.NonComparable, '- -'), tc.NonComparable)

def testExtraSeparators(self):
self.assertEqual(
fire.Fire(tc.ReturnsObj, 'get-obj arg1 arg2 - - as-bool True'), True)
Expand Down
11 changes: 10 additions & 1 deletion fire/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

"""Thie module has componenets that are used for testing Python Fire."""
"""Thie module has components that are used for testing Python Fire."""

from __future__ import absolute_import
from __future__ import division
Expand Down Expand Up @@ -180,3 +180,12 @@ class ErrorRaiser(object):

def fail(self):
raise ValueError('This error is part of a test.')


class NonComparable(object):

def __eq__(self, other):
raise ValueError('Instances of this class cannot be compared.')

def __ne__(self, other):
raise ValueError('Instances of this class cannot be compared.')
6 changes: 6 additions & 0 deletions fire/test_components_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def testTestComponents(self):
self.assertIsNotNone(tc.Empty)
self.assertIsNotNone(tc.OldStyleEmpty)

def testNonComparable(self):
with self.assertRaises(ValueError):
tc.NonComparable() != 2 # pylint: disable=expression-not-assigned
with self.assertRaises(ValueError):
tc.NonComparable() == 2 # pylint: disable=expression-not-assigned


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

0 comments on commit 88f6574

Please sign in to comment.