Skip to content

red-knot: infer and display ellipsis type#13124

Merged
AlexWaygood merged 3 commits intoastral-sh:mainfrom
chriskrycho:rk-ellipsis-type
Aug 27, 2024
Merged

red-knot: infer and display ellipsis type#13124
AlexWaygood merged 3 commits intoastral-sh:mainfrom
chriskrycho:rk-ellipsis-type

Conversation

@chriskrycho
Copy link
Copy Markdown
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.

@github-actions
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 https://github.com/astral-sh/ruff/issues/12694.

Comment thread crates/red_knot_python_semantic/src/types/infer.rs Outdated
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]`.
Comment thread crates/red_knot_python_semantic/src/types/infer.rs Outdated
@AlexWaygood AlexWaygood merged commit 81cd438 into astral-sh:main Aug 27, 2024
@chriskrycho chriskrycho deleted the rk-ellipsis-type branch August 27, 2024 19:54
@carljm carljm added the ty 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

ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants