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 and display ellipsis type #13124

Merged
merged 3 commits into from
Aug 27, 2024

Conversation

chriskrycho
Copy link
Contributor

Summary

Just what it says on the tin: adds basic EllipsisType inference for any time ... appears in the AST.

Test Plan

Test that x = ... produces exactly what we would expect.

Copy link
Contributor

github-actions bot commented Aug 27, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

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

Hmm, not sure about this approach

Comment on lines 302 to 305
Type::EllipsisType => {
// TODO: attribute lookup on Ellipsis type
Type::Unknown
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure this needs to be its own variant in the Type enum... it's conceptually just an instance of types.EllipsisType. types.EllipsisType was only exposed on Python 3.10+, but typeshed already provides a nice little compatibility workaround for us here: https://github.com/python/typeshed/blob/f58dac1d62d33bb2255b762783d06463c40f5065/stdlib/builtins.pyi#L1849-L1865

(builtins.Ellipsis is the same object as ...)

>>> ...
Ellipsis
>>> Ellipsis
Ellipsis
>>> Ellipsis is ...
True

Copy link
Member

Choose a reason for hiding this comment

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

One interesting way in which ... is special is that it's a singleton: it is the only instance of types.EllipsisType that there is, or that there could be. But that can be thought of as just a sealed type that has exactly one member, and we've yet to implement a generalised way of handling sealed types. (We'll need them to tackle enums in Python!) See #12694.

crates/red_knot_python_semantic/src/types/infer.rs Outdated Show resolved Hide resolved
Defer to the built-in type and leave notes about where and how we ought
to be resolving to `typing.EllipsisType` instead of the current output,
which is `Unknown | Literal[EllipsisType]`.
@AlexWaygood AlexWaygood merged commit 81cd438 into astral-sh:main Aug 27, 2024
20 checks passed
@chriskrycho chriskrycho deleted the rk-ellipsis-type branch August 27, 2024 19:54
@carljm carljm added the red-knot Multi-file analysis & type inference label Aug 27, 2024
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