PyCQA / PyCQA/pyflakes

F821 with Annotated import alias

Open
#789 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
190
Avg merge
8m
Merged PRs (30d)
13

Description

We have a use-case where we sometimes import Annotated under a different name (since there is a naming collision with an existing object callled Annotations and Annotated[Annotations, ...] can be hard to read. Unfortunately, it looks like if we try to do this, then we fail with a false positive:

from typing_extensions import Annotated as WithSchema


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

fails pyflakes with F821 undefined name 'hello.

Everything works fine if we do not use the Annotated as WithSchema import alias though. Would it be possible to extend the logic to also handle such a case?

cc @nishkakar @ezrilow

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files or tests are named in the issue. First reproduce the F821 warning with the provided Annotated as WithSchema example, then trace pyflakes' handling of Annotated imports and aliases. Done means the aliased example no longer reports the string literal as an undefined name while existing behavior remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.