seddonym / grimp

Builds a graph of a Python project's internal dependencies.
https://grimp.readthedocs.io
BSD 2-Clause "Simplified" License
51 stars 10 forks source link

Add conditional imports to import graph class #122

Closed jthevos closed 8 months ago

jthevos commented 11 months ago
This PR is a notional POC attempting to capture the following scenario.
if TYPE_CHECKING:
    from package import module

This is motivated by wanting to lint for certain forbidden imports but allow the contracts to be kept if the if condition evaluates to false.
seddonym commented 10 months ago

Thanks for looking at this!

There is actually already a draft PR to add metadata relating to whether or not the import is within an if TYPE_CHECKING block, take a look! It seems to have stalled but I don't think it's that far off.

I'm not sure about expanding this to conditionals in general. I suppose adding a flag is_conditional to each import could be a way to go, but other than for if TYPE_CHECKING, I'm not sure I can think of a genuine use case. Feel free to propose one.

seddonym commented 8 months ago

Superceded by https://github.com/seddonym/grimp/pull/133.