Fix F821 false positive for aliased Annotated and Literal imports#848
Open
worksbyfriday wants to merge 1 commit intoPyCQA:mainfrom
Open
Fix F821 false positive for aliased Annotated and Literal imports#848worksbyfriday wants to merge 1 commit intoPyCQA:mainfrom
worksbyfriday wants to merge 1 commit intoPyCQA:mainfrom
Conversation
Use _is_typing() in addition to _is_name_or_attr() when checking for Literal and Annotated subscripts. _is_typing() resolves import aliases through the scope stack, so `from typing import Annotated as Ann` is correctly recognized. The _is_name_or_attr() fallback is kept to preserve the existing false-negative-safe behavior for non-typing modules that happen to export names called Literal or Annotated. Closes PyCQA#789 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
_is_typing()(scope-aware import resolution) alongside_is_name_or_attr()(literal name check) in theSUBSCRIPThandler forLiteralandAnnotated_is_typing()resolves import aliases through the scope stack, sofrom typing import Annotated as WithSchemais correctly recognized as a typing member_is_name_or_attr()fallback is preserved to maintain the existing false-negative-safe behavior for non-typing modules that export names calledLiteralorAnnotatedReproducer
Before:
F821 undefined name 'hello'After: No warning (metadata arguments in
Annotatedare correctly treated as non-type expressions)The same fix applies to
Literalaliases.Closes #789