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] Emit a diagnostic if the value of a starred expression or a yield from expression is not iterable #13240

Merged
merged 5 commits into from
Sep 4, 2024

Conversation

AlexWaygood
Copy link
Member

Summary

This PR builds on #13195. It ensures that we emit the not-iterable diagnostic on expressions such as the following:

class NotIterable: pass

def foo():
    [*NotIterable()]
    yield from NotIterable()

The Type::iterate() method is refactored slightly so that it's easier for callers of the method to all emit the same diagnostic: it now accepts a diagnostic_fn callback that emits a diagnostic if the type is not found to be an iterable type.

I deliberately haven't tackled comprehensions in this PR, for two reasons:

  1. There's some more complications there
  2. I think @dhruvmanila has been working a lot on comprehensions/there are several TODOs in the code that are assigned to him!

Test Plan

cargo test

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

codspeed-hq bot commented Sep 4, 2024

CodSpeed Performance Report

Merging #13240 will not alter performance

Comparing alex/redknot-iterables (53cc0a2) with main (46a4573)

Summary

✅ 32 untouched benchmarks

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.

crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types.rs Outdated Show resolved Hide resolved
@AlexWaygood AlexWaygood enabled auto-merge (squash) September 4, 2024 14:15
@AlexWaygood AlexWaygood merged commit 0512428 into main Sep 4, 2024
17 checks passed
@AlexWaygood AlexWaygood deleted the alex/redknot-iterables branch September 4, 2024 14:19
@carljm
Copy link
Contributor

carljm commented Sep 4, 2024

I don't much like the callback or the new iteration-specific returned enum. What I would prefer is a more general inference-context object that gets passed into all inference methods on Type and has the ability to emit a diagnostic. Then iterate itself can emit a consistent diagnostic.

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.

3 participants