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

feat(visitor): skip type checking blocks #652

Merged

Conversation

mkniewallner
Copy link
Collaborator

@mkniewallner mkniewallner commented Mar 30, 2024

Resolves #556.

PR Checklist

  • A description of the changes is added to the description of this PR.
  • If there is a related issue, make sure it is linked to this PR.
  • If you've fixed a bug or added code that should be tested, add tests!
  • Documentation in docs is updated

Description of changes

When guarding imports with TYPE_CHECKING in combination with from __future__ import annotations, they are not evaluated at runtime (https://peps.python.org/pep-0563/). This PR implements that behavior, by completely skipping those imports in the import visitor.

An alternative implementation could be to flag dependencies as "typing only" instead of skipping them. This would allow deptry to trigger DEP001 (missing dependencies).

This would also avoid having deptry trigger DEP002 if a dependency is only used for typing, but for this one IMO, it's actually a good thing that it would report a violation, as the dependency is not necessary at runtime, so it should not be a non-dev direct dependency. Still, this is something that could be weird for users that encounter such issues. I've added a new documentation section that details how imports are extracted to at least explain the mechanism used.

I'm not really sure that the extra burden of flagging the dependencies instead of completely skipping them is worth it, but let me know if you think otherwise, I'd be ok with updating the implementation.

Copy link

codecov bot commented Mar 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.6%. Comparing base (dba1b70) to head (5d4ad37).

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #652   +/-   ##
=====================================
  Coverage   90.6%   90.6%           
=====================================
  Files         34      34           
  Lines        961     961           
  Branches     195     195           
=====================================
  Hits         871     871           
  Misses        73      73           
  Partials      17      17           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@mkniewallner mkniewallner force-pushed the feat/skip-type-checking-blocks branch from d925409 to bb8c6c1 Compare March 30, 2024 14:37
@mkniewallner mkniewallner force-pushed the feat/skip-type-checking-blocks branch from bb8c6c1 to c9fcdf8 Compare March 30, 2024 15:12
@mkniewallner mkniewallner added the breaking Change that introduces a breaking change label Mar 30, 2024
@mkniewallner mkniewallner marked this pull request as ready for review March 30, 2024 15:21
docs/usage.md Outdated Show resolved Hide resolved
docs/usage.md Outdated Show resolved Hide resolved
mkniewallner and others added 2 commits March 31, 2024 22:59
Co-authored-by: Florian Maas <fpgmaas@gmail.com>
Co-authored-by: Florian Maas <fpgmaas@gmail.com>
@mkniewallner mkniewallner merged commit 39519c7 into fpgmaas:main Mar 31, 2024
29 checks passed
@mkniewallner mkniewallner deleted the feat/skip-type-checking-blocks branch April 2, 2024 11:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Change that introduces a breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import in if TYPE_CHECKING block is detected as a violation of DEP004
2 participants