Skip to content

Understand typing.Optional in annotations#14397

Merged
AlexWaygood merged 5 commits intoastral-sh:mainfrom
Glyphack:redknot-typing-optional
Nov 17, 2024
Merged

Understand typing.Optional in annotations#14397
AlexWaygood merged 5 commits intoastral-sh:mainfrom
Glyphack:redknot-typing-optional

Conversation

@Glyphack
Copy link
Copy Markdown
Contributor

@Glyphack Glyphack commented Nov 17, 2024

Fix: #14354

Summary

Recognize Optional special form in annotation. Both typing and typing_extensions modules are valid for importing Optional.
The result type is a Union with None.

Test Plan

Added tests for importing from modules and assignability and different combinations for the Optional parameter.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Nov 17, 2024

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@MichaReiser MichaReiser added the ty Multi-file analysis & type inference label Nov 17, 2024
pub enum KnownInstanceType<'db> {
/// The symbol `typing.Literal` (which can also be found as `typing_extensions.Literal`)
Literal,
/// The symbol `typing.Optional` (which can also be found as `typing_extensions.Literal`)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Should this comment be repeated on every special form? Most of them have this properties.

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 find the doc-comments helpful. It's true that many will likely be quite similar, but the comment for an enum variant shows up in the tooltip when you hover over that enum variant in your IDE with the ruff repo open

@Glyphack Glyphack marked this pull request as ready for review November 17, 2024 10:34
Comment thread crates/red_knot_python_semantic/src/types.rs Outdated
@Glyphack Glyphack changed the title Understand typing.Optional in annotations Understand typing.Optional in annotations Nov 17, 2024
Comment thread crates/red_knot_python_semantic/src/types/infer.rs
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.

Looks great, thank you!

@AlexWaygood AlexWaygood merged commit ff19629 into astral-sh:main Nov 17, 2024
@Glyphack Glyphack deleted the redknot-typing-optional branch November 17, 2024 17:05
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.

[red-knot] Understand typing.Optional special form in annotations

4 participants