-
-
Notifications
You must be signed in to change notification settings - Fork 275
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
Improve consistency of JoinedStr inference #2622
base: main
Are you sure you want to change the base?
Improve consistency of JoinedStr inference #2622
Conversation
Signed-off-by: Eric Vergnaud <eric.vergnaud@databricks.com>
Signed-off-by: Eric Vergnaud <eric.vergnaud@databricks.com>
for more information, see https://pre-commit.ci
…om:ericvergnaud/astroid into improve-consistency-of-joinedstr-inference
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2622 +/- ##
==========================================
- Coverage 93.24% 93.21% -0.04%
==========================================
Files 93 93
Lines 11060 11078 +18
==========================================
+ Hits 10313 10326 +13
- Misses 747 752 +5
Flags with carried forward coverage won't be shown. Click here to find out more.
|
for more information, see https://pre-commit.ci
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 don't think I want the flag to be a thing on the class. astroid
is not really configurable and I think that is actually beneficial in terms of maintainability.
I don't really understand your use case: can't we just return Uninferable
when we can't infer the full string? That seems consistent with other nodes and is also the suggestion I already made when inference of JoinedStr
was first added.
Returning
We want to tell our users that reading spark tables from mounts is no longer supported. |
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.
/mnt/{MISSING_VALUE}/...
should be/mnt/{Uninferable}/...
imo. I'm not sure about this, there might be unexpected problems that I don't see at the moment. But it's probably better to infer a fake string when we know for sure it's going to be a string than to just say everything is uninferable, so +0.1 from me.
I guess l'm wondering why you would detect that by calling >>> from astroid import extract_node
>>> n = extract_node("""
... def method_with_arg(arg):
... spark.table(f"/mnt/root/dir/{arg}/logs")
... """)
>>> from astroid import nodes
>>> js = list(n.nodes_of_class(nodes.JoinedStr))[0]
>>> js.values[0].value
'/mnt/root/dir/' I'll comment further on the issue. |
return | ||
uninferable_already_generated = False | ||
for prefix in nodes[0]._infer(context, **kwargs): | ||
for prefix in cls._safe_infer_from_node(nodes[0], context, **kwargs): |
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.
Ooh, good spot. If we isolate this change I bet we could get quick consensus to merge it 👍
Oh, I see, it's so that both cases (mnt injected by the formatted value) can be handled by one call. I guess I'm not sure astroid needs to support that. |
Personally I feel like this use case is best served with a dedicated method, or an implementation in the I think |
Your proposal is not workable with the following code:
not speaking of even more complex scenarios:
That's what makes |
We're using |
TBH this PR is not proposing to change the current behaviour (see |
@jacobtylerwalls @DanielNoord @Pierre-Sassoulas can you opine on the factory proposal ? |
Agree. But if we know it's a string we can return a string even if it's not 'perfect'. Still lightly in favor of inferring string the best we can even if we have to include 'Uninferrable' in the middle
|
I guess I'm not certain we even know it's a string. Don't these uninferable cases include failures to format? |
Exactly, or they can include memory overflow issues, or calls to functions that crash or exit the interpreter. I don't really see why we would special case this |
Let's remove the flag and return fully inferred const or uninferable then. |
That doesn't solve my problem. |
I think without seeing the code to do this we can't really comment on it. Can't you solve your use cases with a function that takes these nodes? Potentially by making that a method in |
This was just an example. We have dozens of use cases where a partial value is preferable to Uninferable, and the node type could be anything (Name, Attribute). Re node factory, I'm not looking for pre-approval of a PR, rather I'm not interested in investing time providing one if philosophical concerns make it a no-go. I guess the above means 'possibly ok' ? |
Could you provide more? I'm still leaning towards I'm wondering if "Possibly ok" is correct in the sense that we never reject proposals by default. I must warn though that maintainers time on |
A node factory would transfer the responsibility of partial inference behavior to astroid users (via a dedicated class deriving from |
for more information, see https://pre-commit.ci
…om:ericvergnaud/astroid into improve-consistency-of-joinedstr-inference
I'm still not convinced the effort is worth the result, especially considering the alternatives seems much nicer to me:
|
This PR:
Type of Changes
| ✓ | 🐛 Bug fix
| ✓ | ✨ New feature |
Description
See above
Closes #2621