-
-
Notifications
You must be signed in to change notification settings - Fork 646
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
[python-infer]: allow ignoring unowned imports #18094
[python-infer]: allow ignoring unowned imports #18094
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, for whatever that's worth. I think this will also be useful for serverless stuff where some modules are provided by the runtime.
Do you think we should hook this into the debug information? The dependency inference script just drops ignored imports (instead of flagging them as ignored). But also, there's a strong locality of the pragma and the import, so it would be clearer why Pants wasn't complaining about not finding the import. Although, I'm not sure that the debug goal is widely used, and we could probably split this addition into a separate MR.
@@ -206,6 +206,33 @@ def _collect_imports_info( | |||
) | |||
|
|||
|
|||
def _filter_unowned_imports( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the name should include why we're filtering, maybe _remove_ignored_imports
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, a lot better, done. :)
|
||
run_dep_inference("warning") | ||
assert len(caplog.records) == 1 | ||
assert ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it worth extracting a helper for asserting when owners are not found? There are only 2 uses, but it would be clearer to see something like assert_owners_not_found_error(target="src/python/cheesey.py", not_found=[...])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure thing, great idea, done :)
@@ -166,3 +166,27 @@ class PythonInferSubsystem(Subsystem): | |||
""" | |||
), | |||
) | |||
|
|||
ignore_unowned_imports = StrListOption( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bikeshed, feel free to disregard:
this name reads to me like a boolean flag, doing what unowned_dependency_behavior
would do. I can't really find a good alternative, though. Maybe ignored_unowned_imports
, ignorable_unowned_imports
, or maybe just unowned_imports
? or maybe more like the pragma with non_inferable_dependencies
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that's actually rather helpful! I like ignored_unowned_imports
a lot and have changed the option name. Thank you!
fd69af6
to
dc0076d
Compare
I see you've tweaked the namings already, but I have yet another angle. As this spins off the fact that using [python-infer]
no_infer_dep = [
"src.generated.app",
"src.generated.client.connect"
] Or the slightly longer |
Thank you @kaos very much for spending some time thinking about it. I see what you mean, this makes perfect sense. I guess I was relating to the However, I think we have to generalize as we can't be sure about the reason why one would want to ignore an import - is it because they know it's not going to be owned (e.g. a generated module at runtime) or is it because it has an owner, but they just don't want to make a module depend on that another module they infer? So the Do you think a plural form i.e. |
Let's see if we can get some more opinions on the matter :) |
IIUC So either the name Overall the implementation looks good, we just should decide on naming/behavior. |
Good catch Josh on the nuance. I'm not sure I agree it would be a misnomer though. We don't say "do not try to infer a dep", we're saying "do not infer a dep". Whether Pants goes looking for a matching dep or not is beside the point. I see it merely as a missed opportunity to optimize in case there's a "no_infer_dep" in effect for an import being followed up. |
@lilatomic sorry, what do you mean by
Or do you refer to having Pants running with |
@kaos @thejcannon thanks for the brainstorming efforts! My understanding is:
a) there's a performance hit checking for every import whether it's in the list of ignored paths (and we want to make sure that So I think what I have now makes sense, however, happy to be told otherwise :) also happy to change the name from |
It comes down to matter of taste I think, how you associate and think about this option. as presented and makes sense to me, it relates closely to the |
The current name makes sense to me, but this literally came up in the context of us trying to enable |
This PR adds a new option to the
[python-infer]
section.This is to let users provide unowned imports that should be ignored.
If there are any unowned import statements and adding the
# pants: no-infer-dep
to the lines of the import is impractical, you can instead provide a list of imports
that Pants should ignore. You can declare a specific import or a path to a package
if you would like any of the package imports to be ignored.
For example, you could ignore all the following imports of the code
by setting
ignored-unowned-imports=["src.generated.app", "src.generated.client.connect"]
.By default, this has no impact on existing users of Pants as there are no imports to be ignored, so behavior will stay the same. Only if a user has provided a list of imports that will be ignored, then it will impact the dependency reference and what's reported as unowned imports.
This should be helpful for the following use cases:
# pants: no-infer-dep
is not feasible; before this PR, the current implementation forces the users who don't know (or don't need to care) about Pants to add some comments, potentially extending any existing comments they may have set on the import statements to satisfy other tools (e.g.pylint
)# pants: no-infer-dep
comments is less appealing when one can set it just once in the configuration file.