Skip to content
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

[red-knot] Infer Unknown for the loop var in async for loops #13243

Merged
merged 1 commit into from
Sep 4, 2024

Conversation

AlexWaygood
Copy link
Member

Summary

async for loops use the asynchronous iteration protocol (involving __aiter__ and __anext__) rather than the synchronous iteration protocol (involving __iter__ and __next__). Understanding __anext__ calls is beyond our capabilities until we can understanding async call expressions (which is probably blocked on generic coroutine types). For now, inferring Unknown for loop vars in async for loops is more accurate than erroneously using the synchronous iteration protocol for the types of these definitions.

Test Plan

cargo test -p red_knot_python_semantic --lib

@AlexWaygood AlexWaygood added the red-knot Multi-file analysis & type inference label Sep 4, 2024
Copy link
Contributor

github-actions bot commented Sep 4, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Base automatically changed from alex/redknot-iterables to main September 4, 2024 14:19
@AlexWaygood AlexWaygood force-pushed the alex/redknot-async-for branch from 4b482f5 to 90eee6e Compare September 4, 2024 14:20
@AlexWaygood AlexWaygood enabled auto-merge (squash) September 4, 2024 14:20
@AlexWaygood AlexWaygood merged commit e965f9c into main Sep 4, 2024
18 checks passed
@AlexWaygood AlexWaygood deleted the alex/redknot-async-for branch September 4, 2024 14:24
.iterate(self.db)
.unwrap_with_diagnostic(iterable.into(), self);
let loop_var_value_ty = if is_async {
// TODO(Alex): async iterables/iterators!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't brought this up in reviews before, but I would prefer if we don't tag our TODOs with a name. The TODOs belong to the project, and anyone might address them in future. I realize it's intended as a way to take responsibility for the issue, but I think in practice it functions more like cookie-licking (https://communitymgt.fandom.com/wiki/Cookie_Licking), discouraging anyone else from contributing an improvement, while the person who created the TODO may not have time for it yet either.

cc @dhruvmanila

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh. Dhruv just this morning told me via DM that we were encouraged to do this at Astral :) he linked this by way of explanation

I don't have a strong preference either way!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I see that rationale, and I am more OK with it if the interpretation is more "this person might have relevant context" and not "you shouldn't work on this unless you check with this person first." But I'm still concerned that the non-cookie-licking interpretation isn't necessarily clear to a new contributor reading the code.

Overall I still think I don't like it. If there's relevant context that's needed to understand the TODO, I'd rather that be in the comment with the TODO, rather than a person's name to go talk to. The case where you really need to go talk to a specific person to understand some code shouldn't be that common, if we are writing reasonably understandable and well-commented code, and when that case arises, git blame exists. (That case can just as well arise for changes in an area of the code that doesn't have a TODO comment, so should we just comment every piece of code we write with our name, because git blame is too hard to use?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened a convo in Discord about this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charliermarsh may have more context. He prefers adding the name.

I agree that git blame may give you the same context. But it can be difficult if the code went through multiple refactors or was even moved to a different location.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really have a preference here, though I think it's pretty easy for code to move around enough to make git blame useless at our pace of refactoring and development. I wouldn't mind skipping the inclusion of a name.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before I was sent the above link, it was 100% my assumption that the meaning of the name in the TODO comment was "This is a TODO for me, i.e. I am going to fix it" (and thus, implicitly, nobody else should do it without asking me first.)

We can of course clarify this internally, but that doesn't mean it will be clear to the rest of the world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants