Skip to content

Comments

Fix F821 false positive for aliased Annotated and Literal imports#848

Open
worksbyfriday wants to merge 1 commit intoPyCQA:mainfrom
worksbyfriday:fix-annotated-alias
Open

Fix F821 false positive for aliased Annotated and Literal imports#848
worksbyfriday wants to merge 1 commit intoPyCQA:mainfrom
worksbyfriday:fix-annotated-alias

Conversation

@worksbyfriday
Copy link

Summary

  • Use _is_typing() (scope-aware import resolution) alongside _is_name_or_attr() (literal name check) in the SUBSCRIPT handler for Literal and Annotated
  • _is_typing() resolves import aliases through the scope stack, so from typing import Annotated as WithSchema is correctly recognized as a typing member
  • The _is_name_or_attr() fallback is preserved to maintain the existing false-negative-safe behavior for non-typing modules that export names called Literal or Annotated
  • Added three regression tests for aliased imports

Reproducer

from typing_extensions import Annotated as WithSchema

def f(x: WithSchema[int, "hello"]) -> None:
    return

Before: F821 undefined name 'hello'
After: No warning (metadata arguments in Annotated are correctly treated as non-type expressions)

The same fix applies to Literal aliases.

Closes #789

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>
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.

F821 with Annotated import alias

1 participant