Skip to content

fix: Comparison using is when operands support __eq__ - #17864

Merged
codebytere merged 3 commits into
electron:masterfrom
matbrgz:patch-1
Apr 19, 2019
Merged

codebytere merged 3 commits into
electron:masterfrom
matbrgz:patch-1

Conversation

@matbrgz

@matbrgz matbrgz commented Apr 18, 2019 •

Copy link
Copy Markdown

Comparison using 'is' when equivalence is not the same as identity

When you compare two values using the is or is not operator, it is the object identities of the two values that is tested rather than their equality. If the class of either of the values in the comparison redefines equality then the is operator may return False even though the objects compare as equal. Equality is defined by the eq or, in Python2, cmp method. To compare two objects for equality, use the == or != operator instead.

Recommendation
When you want to compare the value of two literals, use the comparison operator == or != in place of is or is not.

If the uniqueness property or performance are important then use an object that does not redefine equality.

Notes: none

Comparison using 'is' when equivalence is not the same as identity

When you compare two values using the is or is not operator, it is the object identities of the two values that is tested rather than their equality. If the class of either of the values in the comparison redefines equality then the is operator may return False even though the objects compare as equal. Equality is defined by the __eq__ or, in Python2, __cmp__ method. To compare two objects for equality, use the == or != operator instead.

Recommendation
When you want to compare the value of two literals, use the comparison operator == or != in place of is or is not.

If the uniqueness property or performance are important then use an object that does not redefine equality.
@electron-cation electron-cation Bot added the new-pr 🌱 PR opened recently label Apr 18, 2019
@welcome

welcome Bot commented Apr 18, 2019

Copy link
Copy Markdown

💖 Thanks for opening this pull request! 💖

We use semantic commit messages to streamline the release process. Before your pull request can be merged, you should update your pull request title to start with a semantic prefix.

Examples of commit messages with semantic prefixes:

  • fix: don't overwrite prevent_default if default wasn't prevented
  • feat: add app.isPackaged() method
  • docs: app.isDefaultProtocolClient is now available on Linux

Things that will help get your PR across the finish line:

  • Follow the JavaScript, C++, and Python coding style.
  • Run npm run lint locally to catch formatting errors earlier.
  • Document any user-facing changes you've made following the documentation styleguide.
  • Include tests when adding/changing behavior.
  • Include screenshots and animated GIFs whenever possible.

We get a lot of pull requests on this repo, so please be patient and we will get back to you as soon as we can.

@codebytere codebytere left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MatheusRV please update your PR to conform to our expected PR format; it's missing 1) semantic commit prefix and 2) a release notes description section which would have been found in our PR template (in this case you can just add Notes: none at the bottom)

Matheus Rocha Vieira added 2 commits April 18, 2019 13:18
Comparison using 'is' when equivalence is not the same as identity

When you compare two values using the is or is not operator, it is the object identities of the two values that is tested rather than their equality. If the class of either of the values in the comparison redefines equality then the is operator may return False even though the objects compare as equal. Equality is defined by the __eq__ or, in Python2, __cmp__ method. To compare two objects for equality, use the == or != operator instead.

Recommendation
When you want to compare the value of two literals, use the comparison operator == or != in place of is or is not.

If the uniqueness property or performance are important then use an object that does not redefine equality.
@matbrgz

matbrgz commented Apr 18, 2019

Copy link
Copy Markdown
Author

Done, sorry.

@matbrgz matbrgz changed the title Comparison using is when operands support __eq__ fix: Comparison using is when operands support __eq__ Apr 18, 2019
@nornagon

Copy link
Copy Markdown
Contributor
>>> len(range(12)) is len(range(12))
True
>>> len(range(123)) is len(range(123))
True
>>> len(range(1234)) is len(range(1234))
False

Python deduplicates small integers (<= 256) so is works on them, but larger integers get their own instance and don't compare equal with is any more.

@release-clerk

release-clerk Bot commented Apr 19, 2019

Copy link
Copy Markdown

No Release Notes

@welcome

welcome Bot commented Apr 19, 2019

Copy link
Copy Markdown

Congrats on merging your first pull request! 🎉🎉🎉

@matbrgz
matbrgz deleted the patch-1 branch April 20, 2019 17:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants