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] PEP 695 type aliases #14357

Merged
merged 17 commits into from
Nov 22, 2024
Merged

[red-knot] PEP 695 type aliases #14357

merged 17 commits into from
Nov 22, 2024

Conversation

sharkdp
Copy link
Contributor

@sharkdp sharkdp commented Nov 15, 2024

Summary

Add support for (non-generic) type aliases. The main motivation behind this was to get rid of panics involving expressions in (generic) type aliases. But it turned out the best way to fix it was to implement (partial) support for type aliases.

type IntOrStr = int | str

reveal_type(IntOrStr)  # revealed: typing.TypeAliasType
reveal_type(IntOrStr.__name__)  # revealed: Literal["IntOrStr"]

x: IntOrStr = 1

reveal_type(x)  # revealed: Literal[1]

def f() -> None:
    reveal_type(x)  # revealed: int | str

Test Plan

  • Updated corpus test allow list to reflect that we don't panic anymore.
  • Added Markdown-based test for type aliases (type_alias.md)

@sharkdp sharkdp added the red-knot Multi-file analysis & type inference label Nov 15, 2024
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.

Happy to do a VC call next week if anything remains unclear about how this is all supposed to work!

@sharkdp sharkdp force-pushed the david/type-aliases branch 4 times, most recently from 9f827d6 to b9d931c Compare November 20, 2024 15:05
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.

@sharkdp sharkdp force-pushed the david/type-aliases branch 5 times, most recently from e8c92e3 to 06e615d Compare November 20, 2024 21:23
@sharkdp sharkdp marked this pull request as ready for review November 20, 2024 21:24
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.

The updated type inference structure looks great!

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
crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
crates/red_knot_python_semantic/src/types/mro.rs Outdated Show resolved Hide resolved
Comment on lines 271 to 272
// fails with salsa cycle panic:
("crates/ruff_python_parser/resources/inline/err/type_alias_invalid_value_expr.py", true, true),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is interesting. I suspect it might go away if we stop trying to infer the type of .value() on a type alias anytime we encounter it. If not I'll want to take a closer look.

Copy link
Contributor Author

@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.

It happens for self-referential type aliases like type NestedInt = int | list[NestedInt]. We form a genuine cycle when inferring the annotation type on the RHS and look up the name NestedInt.

@sharkdp sharkdp changed the title [red-knot] Preliminary support for type aliases [red-knot] PEP 695 type aliases Nov 21, 2024
@sharkdp sharkdp merged commit a90e404 into main Nov 22, 2024
20 checks passed
@sharkdp sharkdp deleted the david/type-aliases branch November 22, 2024 07:47
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