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 #3299 false positives with names in string literal type annotations #7400

Merged
merged 12 commits into from Sep 4, 2022

Conversation

lggruspe
Copy link
Contributor

@lggruspe lggruspe commented Sep 2, 2022

Type of Changes

Type
🐛 Bug fix

Description

Closes #3299

Changes:

  • VariablesChecker now checks for possible names in Const strings that appear in a typing annotation context. This fixes some false positives for unused-import and unused-variable.
  • string Consts used as args to typing.Literal aren't treated as names.
  • String annotations like "os.PathLike[str]" or "graphlib.TopologicalSorter[T]" now work

@lggruspe lggruspe changed the title Issue 3299 Fix #3299 false positives with names in string literal type annotations Sep 2, 2022
@github-actions

This comment has been minimized.

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.

Thank you, LGTM, I have only a small question.

@Pierre-Sassoulas Pierre-Sassoulas added the False Positive 🦟 A message is emitted but nothing is wrong with the code label Sep 3, 2022
@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.2 milestone Sep 3, 2022
@Pierre-Sassoulas Pierre-Sassoulas added the Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer label Sep 3, 2022
@coveralls
Copy link

coveralls commented Sep 3, 2022

Pull Request Test Coverage Report for Build 2987707904

  • 37 of 37 (100.0%) changed or added relevant lines in 2 files are covered.
  • 11 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.01%) to 95.328%

Files with Coverage Reduction New Missed Lines %
pylint/lint/pylinter.py 11 95.26%
Totals Coverage Status
Change from base Build 2977378583: 0.01%
Covered Lines: 16978
Relevant Lines: 17810

💛 - Coveralls

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

Thanks for this @lggruspe.

Some minor stylistic changes but the changes itself look very good!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Collaborator

@DanielNoord DanielNoord left a comment

Choose a reason for hiding this comment

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

For some reason the coverage is going down, but I think that is unrelated to the changes here. Probably coveralls screwing something up.

Thanks @lggruspe! 😄

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2022

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

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

  1. unused-import:
    Unused Any imported from typing
    https://github.com/psf/black/blob/92c93a278036870a76740d5b0b8f06504925e7dc/src/black/concurrency.py#L15
  2. unused-import:
    Unused import os
    https://github.com/psf/black/blob/92c93a278036870a76740d5b0b8f06504925e7dc/src/blib2to3/pgen2/pgen.py#L22

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

  1. unused-import:
    Unused Any imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/cursor_async.py#L8
  2. unused-import:
    Unused TupleRow imported from rows
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/connection_async.py#L20
  3. unused-import:
    Unused Any imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/_encodings.py#L11
  4. unused-import:
    Unused Optional imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/_encodings.py#L11
  5. unused-import:
    Unused Sequence imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/_dns.py#L12
  6. unused-import:
    Unused Any imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/_adapters_map.py#L7
  7. unused-import:
    Unused Any imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/_pipeline.py#L9
  8. unused-import:
    Unused ReferenceType imported from weakref
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/connection.py#L13
  9. unused-import:
    Unused TupleRow imported from rows
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/connection.py#L26
  10. unused-import:
    Unused Type imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/types/net.py#L7
  11. unused-import:
    Unused Any imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/types/datetime.py#L10
  12. unused-import:
    Unused Type imported from typing
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/crdb/connection.py#L8
  13. unused-import:
    Unused TupleRow imported from rows
    https://github.com/psycopg/psycopg/blob/5b4b9cc8bddb2ef20af03c773bd8c4efc0db7d90/psycopg/psycopg/crdb/connection.py#L12

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

  1. unused-import:
    Unused import os
    https://github.com/pytest-dev/pytest/blob/4ed2b3a733b0240dfa2260dba98d327213cf04ed/src/_pytest/doctest.py#L4
  2. unused-import:
    Unused import os
    https://github.com/pytest-dev/pytest/blob/4ed2b3a733b0240dfa2260dba98d327213cf04ed/src/_pytest/compat.py#L5
  3. unused-import:
    Unused cached_property imported from functools as cached_property
    https://github.com/pytest-dev/pytest/blob/4ed2b3a733b0240dfa2260dba98d327213cf04ed/src/_pytest/compat.py#L356
  4. unused-argument:
    Unused argument 'pdb'
    https://github.com/pytest-dev/pytest/blob/4ed2b3a733b0240dfa2260dba98d327213cf04ed/src/_pytest/hookspec.py#L953
  5. unused-argument:
    Unused argument 'pdb'
    https://github.com/pytest-dev/pytest/blob/4ed2b3a733b0240dfa2260dba98d327213cf04ed/src/_pytest/hookspec.py#L964

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

  1. unused-import:
    Unused User imported from sentry.incidents.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/incidents/charts.py#L16
  2. unused-import:
    Unused Future imported from concurrent.futures
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/ingest/ingest_consumer.py#L5
  3. unused-import:
    Unused Redis imported from redis
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/utils/kvstore/redis.py#L4
  4. unused-import:
    Unused Project imported from sentry.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/api/helpers/group_index/delete.py#L11
  5. unused-import:
    Unused Organization imported from sentry.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/api/helpers/group_index/index.py#L15
  6. unused-import:
    Unused Project imported from sentry.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/api/helpers/group_index/index.py#L15
  7. unused-import:
    Unused User imported from sentry.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/api/helpers/group_index/index.py#L15
  8. unused-import:
    Unused Actor imported from sentry.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/api/helpers/group_index/validators/group.py#L6
  9. unused-import:
    Unused User imported from sentry.incidents.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/integrations/slack/unfurl/metric_alerts.py#L12
  10. unused-import:
    Unused User imported from sentry.models.user
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/integrations/slack/unfurl/discover.py#L16
  11. unused-import:
    Unused User imported from sentry.models
    https://github.com/getsentry/sentry/blob/f5d9a4c3e403d3419dea9b0005408d34d30c6015/src/sentry/integrations/slack/unfurl/issues.py#L8

This comment was generated for commit 8a562df

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.

Thank you @lggruspe. The changes look good, and the primer results warm my heart.

@Pierre-Sassoulas Pierre-Sassoulas modified the milestones: 2.15.2, 2.15.1 Sep 4, 2022
@Pierre-Sassoulas Pierre-Sassoulas merged commit 2f766a1 into pylint-dev:main Sep 4, 2022
@Pierre-Sassoulas Pierre-Sassoulas added Backported and removed Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer labels Sep 6, 2022
Pierre-Sassoulas pushed a commit to Pierre-Sassoulas/pylint that referenced this pull request Sep 6, 2022
… annotations (pylint-dev#7400)

Don't emit 'unused-variable' or 'unused-import' on names in string literal type annotations (pylint-dev#3299)
Don't treat strings inside typing.Literal as names
Pierre-Sassoulas pushed a commit that referenced this pull request Sep 6, 2022
…ns (#7400)

Don't emit 'unused-variable' or 'unused-import' on names in string literal type annotations (#3299)
Don't treat strings inside typing.Literal as names
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
Development

Successfully merging this pull request may close these issues.

Pylint incorrectly flags as unused-import things that are used in string literal type annotations
4 participants