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

Imports protected by TYPE_CHECKING are treated as mandatory #390

Open
chrisburr opened this issue Nov 3, 2023 · 1 comment
Open

Imports protected by TYPE_CHECKING are treated as mandatory #390

chrisburr opened this issue Nov 3, 2023 · 1 comment
Labels
good first issue Good for newcomers P3 minor: not priorized parsing-imports

Comments

@chrisburr
Copy link

Describe the bug

Code which is protected by TYPE_CHECKING is listed as a required import. I'm not sure if to class this as a bug or a feature request but this template fits better.

For example:

from typing import TYPE_CHECKING
import foo
if TYPE_CHECKING:
    import bar

To Reproduce

bash-5.2$ echo $'from typing import TYPE_CHECKING\nimport foo\nif TYPE_CHECKING:\n    import bar' | fawltydeps --list-imports --code - --detailed
<stdin>:2: foo
<stdin>:4: bar

Expected behavior

Import protected by TYPE_CHECKING can be ignored.

Environment

  • Version of the code: 0.13.2

Additional context

@jherland jherland added the P3 minor: not priorized label Nov 3, 2023
@jherland
Copy link
Member

jherland commented Nov 3, 2023

Thanks for reporting! Agree that TYPE_CHECKING imports should be disregarded by FawltyDeps. Although they are needed by Mypy (or other type checkers) these tools are perfectly capable of reporting the missing dependency themselves.

We're not sure when we'll get to this, so if you want to take a stab at a PR in the meantime we'll be happy to look at it.

Edit: We don't see this as an urgent issue, as the workaround here is straightforward: Add bar (the modules conditionally imported under TYPE_CHECKING) to the set of undeclared dependencies to be ignored, i.e. compare:

echo $'from typing import TYPE_CHECKING\nimport foo\nif TYPE_CHECKING:\n    import bar' \
  | fawltydeps --check-undeclared --code - --detailed

to

echo $'from typing import TYPE_CHECKING\nimport foo\nif TYPE_CHECKING:\n    import bar' \
  | fawltydeps --check-undeclared --code - --detailed --ignore-undeclared bar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers P3 minor: not priorized parsing-imports
Projects
None yet
Development

No branches or pull requests

3 participants