Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a false positive for simplify-boolean-expression when multiple values inferred #7627

Merged

Conversation

jacobtylerwalls
Copy link
Member

Type of Changes

Type
🐛 Bug fix

Description

If multiple values are possible for a constant, we should silence the refactoring messages that claim to offer simplifications, since the inferred value is not reliable.

Adds a keyword-only compare_constants argument to safe_infer to accomplish this in a reusable way.

Closes #7626

I'm wondering if @areveny you might wish to have a look, as I saw #5227? No pressure.

…values inferred

Adds a keyword-only `compare_constants` argument to `safe_infer`.
@jacobtylerwalls jacobtylerwalls added the Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer label Oct 16, 2022
@jacobtylerwalls jacobtylerwalls added this to the 2.15.5 milestone Oct 16, 2022
@coveralls
Copy link

coveralls commented Oct 16, 2022

Pull Request Test Coverage Report for Build 3260078556

  • 7 of 7 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0005%) to 95.354%

Totals Coverage Status
Change from base Build 3245605436: 0.0005%
Covered Lines: 17137
Relevant Lines: 17972

💛 - Coveralls

@github-actions

This comment has been minimized.

areveny
areveny previously approved these changes Oct 16, 2022
Copy link
Contributor

@areveny areveny left a comment

Choose a reason for hiding this comment

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

Looks good to me! Left one suggestion. Requesting another approver.

I wonder if there's anywhere else compare_constants=True could be used but after perusing usages of safe_infer no other applications are immediately clear.

pylint/checkers/utils.py Outdated Show resolved Hide resolved
@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Oct 16, 2022
Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

We could re-add the old test for uninferable value and fix it, but I'll let you decide :)

@jacobtylerwalls
Copy link
Member Author

Ah, I think I follow you now. I've got versions of the original cases, plus two new cases.

@github-actions
Copy link
Contributor

🤖 Effect of this PR on checked open source code: 🤖

Effect on django:
The following messages are no longer emitted:

  1. consider-using-ternary:
    Consider using ternary (self.cache_key_prefix + session_key in self._cache if session_key else super().exists(session_key))
    https://github.com/django/django/blob/7c884afe5acc00d1df83390848227f42918ec0d4/django/contrib/sessions/backends/cached_db.py#L47
  2. consider-using-ternary:
    Consider using ternary (self.rhs_params[-1] == 'spheroid' if len(self.rhs_params) == 2 else None)
    https://github.com/django/django/blob/7c884afe5acc00d1df83390848227f42918ec0d4/django/contrib/gis/db/models/lookups.py#L360
  3. consider-using-ternary:
    Consider using ternary (urlsplit(unquote(filename)).path.strip() if filename else clean_name)
    https://github.com/django/django/blob/7c884afe5acc00d1df83390848227f42918ec0d4/django/contrib/staticfiles/storage.py#L109

Effect on pandas:
The following messages are no longer emitted:

  1. consider-using-ternary:
    Consider using ternary (any((d['selector'] not in IGNORED_WRAPPERS for d in table_styles)) if table_styles is not None else caption is not None)
    https://github.com/pandas-dev/pandas/blob/c671f6ca3dba3f4e6f3077c00c1ff773d1ef7f45/pandas/io/formats/style_render.py#L2081
  2. consider-using-ternary:
    Consider using ternary (isinstance(self.fill_value, type(other.fill_value)) if other._is_na_fill_value else isinstance(other.fill_value, type(self.fill_value)))
    https://github.com/pandas-dev/pandas/blob/c671f6ca3dba3f4e6f3077c00c1ff773d1ef7f45/pandas/core/arrays/sparse/dtype.py#L125

Effect on psycopg:
The following messages are no longer emitted:

  1. consider-using-ternary:
    Consider using ternary (self.sub_dumper.oid if self.sub_dumper else TEXT_OID)
    https://github.com/psycopg/psycopg/blob/d7a3785804ac97e139355e52bded734a13b62f26/psycopg/psycopg/types/array.py#L247

Effect on pytest:
The following messages are no longer emitted:

  1. consider-using-ternary:
    Consider using ternary (clscol.obj if clscol else None)
    https://github.com/pytest-dev/pytest/blob/15ac0349b2c7d8dc48fe2e25a1b4fa47c9fda25c/src/_pytest/python.py#L479
  2. consider-using-ternary:
    Consider using ternary (nextitem.listchain() if nextitem else [])
    https://github.com/pytest-dev/pytest/blob/15ac0349b2c7d8dc48fe2e25a1b4fa47c9fda25c/src/_pytest/runner.py#L514
  3. consider-using-ternary:
    Consider using ternary (excinfo.typename if excinfo else '')
    https://github.com/pytest-dev/pytest/blob/15ac0349b2c7d8dc48fe2e25a1b4fa47c9fda25c/src/_pytest/_code/code.py#L827
  4. consider-using-ternary:
    Consider using ternary (excinfo if last == entry else None)
    https://github.com/pytest-dev/pytest/blob/15ac0349b2c7d8dc48fe2e25a1b4fa47c9fda25c/src/_pytest/_code/code.py#L870

Effect on sentry:
The following messages are no longer emitted:

  1. consider-using-ternary:
    Consider using ternary (subscription_option.reason if subscription_option else GroupSubscriptionReason.implicit)
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/models/groupsubscription.py#L138
  2. consider-using-ternary:
    Consider using ternary (is_prev if self.desc else not (self.desc or is_prev))
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/api/paginator.py#L44
  3. consider-using-ternary:
    Consider using ternary (is_prev if self.desc else not (self.desc or is_prev))
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/api/paginator.py#L614
  4. consider-using-ternary:
    Consider using ternary (metadata._asdict() if metadata else None)
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/api/serializers/models/integration.py#L160
  5. consider-using-ternary:
    Consider using ternary (dist.name if dist else None)
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/lang/javascript/processor.py#L324
  6. consider-using-ternary:
    Consider using ternary (dist.name if dist else None)
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/lang/javascript/processor.py#L359
  7. consider-using-ternary:
    Consider using ternary (dist.name if dist else None)
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/lang/javascript/processor.py#L448
  8. consider-using-ternary:
    Consider using ternary (reversed_alias_map.get(resolved) if reversed_alias_map is not None else resolved)
    https://github.com/getsentry/sentry/blob/af2b4f91b6895b2a09682dabf2d5ff5b40bb4062/src/sentry/lang/native/symbolicator.py#L715

This comment was generated for commit d97504e

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

👌

@jacobtylerwalls
Copy link
Member Author

Thanks for the reviews!

@jacobtylerwalls jacobtylerwalls merged commit 00b6aa8 into pylint-dev:main Oct 16, 2022
@jacobtylerwalls jacobtylerwalls deleted the multiple-values-for-constant branch October 16, 2022 17:36
@finite-state-machine
Copy link

Goodness, that was quick! My thanks to all who were involved, for this and everything else you do!

@Pierre-Sassoulas
Copy link
Member

This is going to be released in pylint 2.15.5 :)

cdce8p pushed a commit to cdce8p/pylint that referenced this pull request Oct 20, 2022
…values inferred (pylint-dev#7627)

Add a keyword-only `compare_constants` argument to `safe_infer`.
@cdce8p cdce8p added Backported and removed Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer labels Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported False Positive 🦟 A message is emitted but nothing is wrong with the code
Projects
None yet
7 participants