Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Handle inference of
ComprehensionScope
better #1475Handle inference of
ComprehensionScope
better #1475Changes from 7 commits
cf1d77b
db2fbaa
ab48be3
0614091
a3c3e06
84c1df3
df71eec
a30d378
ee7d066
c9ad1d8
fc971a9
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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.
@cdce8p do you have thoughts about this? In the past I believe you were a vote against, see #1284 (review).
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.
Maybe I'm misunderstanding the concept of inference here. If so, please correct me.
However, the way I see it inference means we look at the AST node and tell the caller what the Python result will be without actually executing the code. To give an example
The inference result for
lst
shouldn't be the list comprehension itself. Instead it should be the AST node for a list withSame for generator expressions, dict and set comprehensions.
That is much much harder to do which is likely we it wasn't done already.
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.
Hm, yeah I didn't think of it like that. That would decouple the value of
lst
from the actualast
tree though.For example, we would then have a
ListComp
node innodes.Module.body
but aList
node forlst
? If we create thatList
when calling_infer
onListComp
what would then be thelineno
etc.?Wouldn't it be better to say that
lst
is aListComp
and let downstream users decide if they want to handleListComp
andList
similarly? Like we often do withFunctionDef
andAsyncFunctionDef
.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.
Yeah, it's an inference result after all.
None
? Bothlineno
andcol_offset
attributes are typed as optional.Or the same one as the original
ListComp
.The downstream user still has full control over it. If he once the "raw" data, he can just use the node itself. No need to call
infer
for that. RegardingFunctionDef
, the infer method can actually return aProperty
if the function is decorated. Similarly, inferring aDictUnpack
will return a single dict or a comparison just a bool constantThere 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 guess we should probably handle this comprehension per comprehension then. Instead of one larger PR.
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.
Yeah. There are multiple parts to be aware of: evaluating the iterator, matching the loop var(s) with optional sequence unpacking, checking any if statements and doing late assignments via
:=
, evaluating the loop expression. Likely I'm forgetting something.If you, or someone else wants to go down that route, it's probably best to start with a fairly limited set constraints and only one comprehension type. Once that is down extending it should be fairly easy in comparison.