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
Add support for detecting whether an import is only made during type checking #64
Comments
Hi Anthony, Thanks for this - and sorry for the delay in responding. Am I right in thinking the code sample should read:
I think potentially it's a good idea, though I would lean towards it not being the default behaviour, as arguably if something knows about a type, then it's a dependency. Happy to look at a PR if you're eager for this functionality to get included, if not I'll just leave it as an open issue for now. |
@seddonym This issue seems stale, any idea if you will ever import this or can you give a pointer on where to start? We are looking to implement |
Hi @blokje, I'm not actively planning to work on this but I would be happy to consider a PR (or two). Perhaps the best way to approach it would be as follows. First - as a general principle this should not be default behaviour for Grimp nor for Import Linter. GrimpPerhaps something like this:
Import Linter
Under the hood, the contract could remove all the What do you think? |
Okay I had fun in grimp and tried to understand how AST works, I think I should be able to work something out and maybe open a PR at some point. However I have a few questions regarding design decisions:
TYPE_CHECKING = None
if TYPE_CHECKING:
import foo
import typing as t
if t.TYPE_CHECKING:
import foo # This is flagged in my proof of concept
if t.TYPE_CHECKING is True
import foo # This is not
if t.TYPE_CHECKING == True
import foo # This is not
if t.TYPE_CHECKING != False
import foo # This is not |
Yeah, I have a feeling if it might actually be computationally impossible to know for sure without actually running the program. So, if we are to include this, we should probably just keep it very simple and make its limitations clear in the docs. I'd be happy, in the first instance, just to support the narrow case of something being enclosed in the Honestly, if it adds too much complexity to Grimp I'm likely to push back, as I'd like to reduce the burden of a potential rewrite in a faster language such as Rust. 😄 |
All right thanks for the input. It isn't too complicated I think, neither a big rewrite. >>> graph.get_import_details(
importer='myproject.foo',
imported='myproject.bar'
)
[
{
'importer': 'myproject.foo',
'imported': 'myproject.bar',
'line_number': 5,
'line_contents': 'from . import baz',
'type_checking_only': False,
},
] |
I think so. I want to minimise the overhead for the static analysis, so this particular check should only run if it's explicitly enabled. |
There should be no significant overhead, all the AST tree is parsed anyways. As for backward compatibility, it's about adding one entry to the returned dictionnary. Do upstream projects check all the items of the dict or only the one they need? |
I tested your current implementation on a large code base and I'm pleased to say it looks like you're right - in fact your change to use the
I'm not concerned about adding an entry to the dictionary, that's fine. To be honest my main concern is that I personally don't see this kind of import as any different from any others, so am reluctant to distinguish them by default, but I admit that might just be prejudice. Let me have a think about it. In the meantime the PR is shaping up nicely, very elegant solution to the problem 👏🏻 |
So I was testing adding a parameter to the function to suppress the type_checking entry, but I couldn't come up with something I'm satisfied with. Either we have to manually remove the entry in the ImportGraph class, which has a performance impact, either we have to pass the parameter down to the parsing logic so the parser doesn't add the information in the first place. Either way, it also makes type checking annotations more complex because we have to duplicate DetailedImport and DirectImport depending on whether the type_checking info is asked. All of this is making me lean more toward the initial solution of just adding the type_checking entry, letting the users eventually ignoring it. |
Apologies for the delay in responding. Ok, I think I can get behind always including it in the return value of |
It just occurred to me that there is another approach we could take. Rather than include the I appreciate this comes after you've already done a fair amount of work, but we should probably at least consider it, as it could impact the eventual speed of Grimp/Import Linter if and when I come to reimplement in a faster language. Pros:
Cons:
Not sure where I stand on this yet but interested to know your thoughts. |
My 2c I would be really interested in this feature, because I use a lot of typing-only imports for better type-checking, however, these imports sometimes do not respect the rules I have for regular code. I would love to see Next, technical problems.
Others can be ignored. It is a users' duty to name it properly, and this is not a problem at all - to refactor this name. |
Thanks for the comment. So @sobolevn your vote would be for the global approach (pass it to I'm not intending to implement this myself, but the original PR is in pretty good shape (notwithstanding the adjustments that might be needed regarding the previous point). @yakMM where are you at with this - would be you interested in picking this one up at some point?
I'm happy with that. |
A global approach would also be sufficient for my use-cases |
Hey, sorry I was MIA, this feature was not at the top of my priority list. If everyone is happy with a global approach we can go with option 1, ie If I remember correctly. the approach of inheriting |
Great news!
Yes absolutely, in fact it slightly speeds up the building of the graph. |
Draft PR here #130 If anyone wants to help with test/doc, feel free to PR against my fork https://github.com/yakMM/grimp/tree/64-type-checking |
Great, looking forward to reviewing - I'll try to take a look early next week. |
This is now released in Grimp 3.1. Nice work everyone! |
I've made an issue in Import Linter to make use of the flag. @yakMM are you interested in taking this one on? Should be much more straightforward! |
Specifically, be able to detect when imports are used under
typing.TYPE_CHECKING
.Raising this in Grimp, as it appears the functionality isn't in place, and so can't be implemented with a custom contract.
While I understand that this is solved by introducing an interface, this results in a lot of boilerplate code when all you're seeking is basic static analysis + hints in an editor.
Given a Contract:
and modules of:
I would expect this to pass the contract.
to reiterate, understand it'd be better to introduce a stop gap, but feel there's a use-case for keeping things simple in the code, while still being able to offer an external contract to prevent mistakes.
The text was updated successfully, but these errors were encountered: