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

Disambiguate between str and enum member args to typing.Literal #7414

Merged
merged 5 commits into from Sep 12, 2022

Conversation

lggruspe
Copy link
Contributor

@lggruspe lggruspe commented Sep 5, 2022

Type of Changes

Type
🐛 Bug fix

Description

This is a continuation of #7400. This PR fixes some errors in the handling of typing.Literal.

From https://peps.python.org/pep-0586/#literals-enums-and-forward-references:

One potential ambiguity is between literal strings and forward references to literal enum members. For example, suppose we have the type Literal["Color.RED"]. Does this literal type contain a string literal or a forward reference to some Color.RED enum member?

In cases like these, we always assume the user meant to construct a literal string. If the user wants a forward reference, they must wrap the entire literal type in a string – e.g. "Literal[Color.RED]".

@coveralls
Copy link

coveralls commented Sep 5, 2022

Pull Request Test Coverage Report for Build 3027673818

  • 4 of 4 (100.0%) changed or added relevant lines in 1 file are covered.
  • 231 unchanged lines in 15 files lost coverage.
  • Overall coverage decreased (-0.02%) to 95.314%

Files with Coverage Reduction New Missed Lines %
pylint/extensions/emptystring.py 1 96.77%
pylint/extensions/docparams.py 2 98.53%
pylint/pyreverse/vcg_printer.py 3 92.75%
pylint/config/arguments_manager.py 5 97.25%
pylint/checkers/refactoring/refactoring_checker.py 7 98.2%
pylint/checkers/base/basic_error_checker.py 8 95.56%
pylint/config/find_default_config_files.py 8 89.16%
pylint/checkers/strings.py 12 94.09%
pylint/utils/utils.py 15 87.01%
pylint/checkers/imports.py 17 94.06%
Totals Coverage Status
Change from base Build 2992932506: -0.02%
Covered Lines: 17026
Relevant Lines: 17863

💛 - Coveralls

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Pierre-Sassoulas Pierre-Sassoulas added Work in progress Needs backport Needs to be cherry-picked on the current patch version by a pylint's maintainer False Positive 🦟 A message is emitted but nothing is wrong with the code labels Sep 6, 2022
@@ -2940,7 +2930,19 @@ def visit_const(self, node: nodes.Const) -> None:
if origin is not None and utils.is_typing_literal(origin):
return

self._type_annotation_names.append(node.value)
if node.value.isidentifier():
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you explain why you moved this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved the is_typing_literal part up to stop checking for names if the string node is an arg to Literal. As for the isidentifier check, it's probably not needed anymore. I'll push some more changes.

lggruspe and others added 2 commits September 7, 2022 20:03
…nnotation_py38.py

Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
@github-actions

This comment has been minimized.

DanielNoord
DanielNoord previously approved these changes Sep 9, 2022
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.

This does need a news fragment still.

@github-actions
Copy link
Contributor

🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉

This comment was generated for commit 24fc87f

@DanielNoord
Copy link
Collaborator

Thanks you @lggruspe!

@DanielNoord DanielNoord merged commit 0aece42 into pylint-dev:main Sep 12, 2022
@DanielNoord
Copy link
Collaborator

@Pierre-Sassoulas Should this be included as well? Milestone is missing (my bad probably) but it has the label.

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.15.4 milestone Oct 10, 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 Oct 10, 2022
@Pierre-Sassoulas
Copy link
Member

Let's add it too.

Pierre-Sassoulas pushed a commit to Pierre-Sassoulas/pylint that referenced this pull request Oct 10, 2022
…nt-dev#7414)

Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Pierre-Sassoulas pushed a commit to Pierre-Sassoulas/pylint that referenced this pull request Oct 10, 2022
…nt-dev#7414)

Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
Pierre-Sassoulas pushed a commit that referenced this pull request Oct 10, 2022
Co-authored-by: Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
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.

None yet

4 participants