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] support typing.Union in type annotations #14499

Merged
merged 13 commits into from
Nov 20, 2024

Conversation

Glyphack
Copy link
Contributor

Fix #14498

Summary

This PR adds typing.Union support

Test Plan

I created new tests in mdtest.

@MichaReiser MichaReiser added the red-knot Multi-file analysis & type inference label Nov 20, 2024
@Glyphack
Copy link
Contributor Author

Is it possible to auto tag my PR as redknot by using a specific word in the description or title?

@Glyphack Glyphack changed the title Redknot typing union [red-knot] support typing.Union in type annotations Nov 20, 2024
@Glyphack Glyphack marked this pull request as draft November 20, 2024 20:22
Copy link
Contributor

github-actions bot commented Nov 20, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@Glyphack Glyphack marked this pull request as ready for review November 20, 2024 20:31
Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Looks good! Just a few small things.

@carljm
Copy link
Contributor

carljm commented Nov 20, 2024

Is it possible to auto tag my PR as redknot by using a specific word in the description or title?

No, unfortunately we don't have anything like that set up. It would be nice to have!

Copy link
Contributor

@carljm carljm left a comment

Choose a reason for hiding this comment

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

Awesome, thank you! Will commit my one remaining comment tweak, and then merge!

@carljm
Copy link
Contributor

carljm commented Nov 20, 2024

Noticed a couple more small changes on reviewing the latest, pushed those too...

@carljm carljm enabled auto-merge (squash) November 20, 2024 21:47
@carljm carljm merged commit aecdb8c into astral-sh:main Nov 20, 2024
19 checks passed
Comment on lines +4570 to +4576
KnownInstanceType::Union => match parameters {
ast::Expr::Tuple(t) => UnionType::from_elements(
self.db,
t.iter().map(|elt| self.infer_type_expression(elt)),
),
_ => self.infer_type_expression(parameters),
},
Copy link
Contributor

@sharkdp sharkdp Nov 21, 2024

Choose a reason for hiding this comment

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

This is extremely subtle, but with this change, we now fail to infer a type for the ast::Expr::Tuple expression itself. We only infer types for the sub-expressions via self.infer_type_expression(elt), but we don't store a type for the overall ast::Expr::Tuple (only for the whole slice expression).

In other words, when we have something like Union[str, int], we do infer types for the whole slice expression Union[str, int]. We also infer types for int and str, but we do not infer/store a type for the int, str tuple expression.

Unfortunately, the test that would have caught this is currently deactivated (see #14391), but I hope we can re-activate it soon.

I'll open a PR with a fix (Edit: #14510).

sharkdp added a commit that referenced this pull request Nov 21, 2024
)

## Summary

Fixes a panic related to sub-expressions of `typing.Union` where we fail
to store a type for the `int, str` tuple-expression in code like this:
```
x: Union[int, str] = 1
```

relates to [my
comment](#14499 (comment))
on #14499.

## Test Plan

New corpus test
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.

[red-knot] Understand typing.Union special form in annotations
5 participants