PyCQA / PyCQA/flake8-pyi

Flag forbidden imports on the line they occur (after dropping support for Python 3.9)

Open
#413 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

deferred
Dominant language
Python
Stars
83
Forks
22
PR merge metrics
No merged PRs in 30d

Description

Currently we have this behaviour:

from collections.abc import (  # Y057 flagged on this line for the forbidden ByteString import
    AsyncIterator,
    Awaitable,
    ByteString,
    Collection,
    Container,
    MutableMapping,
    MutableSequence,
)

It would be nice if we could have this behaviour instead, where import-related errors were flagged on the line where the import actually occurs when it's part of a multiline import statement:

from collections.abc import (
    AsyncIterator,
    Awaitable,
    ByteString,  # Y057 flagged on this line for the forbidden ByteString import
    Collection,
    Container,
    MutableMapping,
    MutableSequence,
)

As discussed in #411, however, this is pretty hard to do on Python <3.10 due to the fact that ast.alias nodes only have lineno and col_offset attributes on 3.10+. And while we still support Python <3.10, it's good to consistently flag errors on the same line regardless of the Python version being used. So this can't be addressed for now.

Once we drop support for Python 3.9, however, we should change our behaviour to use the more precise line numbers from the ast.alias nodes.

Contributor guide

Open the contributing guide

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

Start by locating the Y057 and other import-related checks, then inspect how they use ast.alias line information. After Python 3.9 support is dropped, update the behavior to use the alias line for multiline imports and verify that forbidden imports are reported on their own lines.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.