[ty] Implicit type aliases: Add support for typing.Union#21363
Merged
Conversation
Diagnostic diff on typing conformance testsChanges were detected when running ty on typing conformance tests--- old-output.txt 2025-11-12 11:47:18.914860495 +0000
+++ new-output.txt 2025-11-12 11:47:22.351880046 +0000
@@ -1,4 +1,4 @@
-fatal[panic] Panicked at /home/runner/.cargo/git/checkouts/salsa-e6f3bb7c2a062968/05a9af7/src/function/execute.rs:451:17 when checking `/home/runner/work/ruff/ruff/typing/conformance/tests/aliases_typealiastype.py`: `infer_definition_types(Id(19385)): execute: too many cycle iterations`
+fatal[panic] Panicked at /home/runner/.cargo/git/checkouts/salsa-e6f3bb7c2a062968/05a9af7/src/function/execute.rs:451:17 when checking `/home/runner/work/ruff/ruff/typing/conformance/tests/aliases_typealiastype.py`: `infer_definition_types(Id(19b85)): execute: too many cycle iterations`
_directives_deprecated_library.py:15:31: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `int`
_directives_deprecated_library.py:30:26: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
_directives_deprecated_library.py:36:41: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `Self@__add__`
@@ -6,7 +6,6 @@
_directives_deprecated_library.py:45:24: error[invalid-return-type] Function always implicitly returns `None`, which is not assignable to return type `str`
aliases_explicit.py:41:24: error[invalid-type-form] List literals are not allowed in this context in a type expression: Did you mean `tuple[str, str]`?
aliases_explicit.py:45:10: error[invalid-type-form] Variable of type `Literal["int | str"]` is not allowed in a type expression
-aliases_explicit.py:49:5: error[type-assertion-failure] Argument does not have asserted type `int | str`
aliases_explicit.py:52:5: error[type-assertion-failure] Argument does not have asserted type `list[int]`
aliases_explicit.py:53:5: error[type-assertion-failure] Argument does not have asserted type `tuple[str, ...] | list[str]`
aliases_explicit.py:54:5: error[type-assertion-failure] Argument does not have asserted type `tuple[int, int, int, str]`
@@ -18,7 +17,6 @@
aliases_explicit.py:61:5: error[type-assertion-failure] Argument does not have asserted type `int | str`
aliases_explicit.py:101:6: error[call-non-callable] Object of type `UnionType` is not callable
aliases_implicit.py:54:24: error[invalid-type-form] List literals are not allowed in this context in a type expression: Did you mean `tuple[str, str]`?
-aliases_implicit.py:60:5: error[type-assertion-failure] Argument does not have asserted type `int | str`
aliases_implicit.py:63:5: error[type-assertion-failure] Argument does not have asserted type `list[int]`
aliases_implicit.py:64:5: error[type-assertion-failure] Argument does not have asserted type `tuple[str, ...] | list[str]`
aliases_implicit.py:65:5: error[type-assertion-failure] Argument does not have asserted type `tuple[int, int, int, str]`
@@ -1003,5 +1001,5 @@
typeddicts_usage.py:28:17: error[missing-typed-dict-key] Missing required key 'name' in TypedDict `Movie` constructor
typeddicts_usage.py:28:18: error[invalid-key] Invalid key for TypedDict `Movie`: Unknown key "title"
typeddicts_usage.py:40:24: error[invalid-type-form] The special form `typing.TypedDict` is not allowed in type expressions. Did you mean to use a concrete TypedDict or `collections.abc.Mapping[str, object]` instead?
-Found 1005 diagnostics
+Found 1003 diagnostics
WARN A fatal error occurred while checking some files. Not all project files were analyzed. See the diagnostics list above for details.
|
|
| Lint rule | Added | Removed | Changed |
|---|---|---|---|
invalid-key |
2,160 | 0 | 0 |
unused-ignore-comment |
7 | 443 | 0 |
invalid-argument-type |
264 | 5 | 116 |
invalid-assignment |
134 | 12 | 13 |
possibly-missing-attribute |
98 | 8 | 4 |
no-matching-overload |
78 | 0 | 0 |
non-subscriptable |
24 | 18 | 20 |
invalid-return-type |
32 | 5 | 7 |
unresolved-attribute |
40 | 1 | 2 |
invalid-type-form |
24 | 0 | 0 |
unsupported-operator |
12 | 1 | 7 |
call-non-callable |
18 | 1 | 0 |
possibly-missing-implicit-call |
3 | 0 | 10 |
invalid-context-manager |
0 | 0 | 10 |
not-iterable |
7 | 0 | 2 |
invalid-parameter-default |
5 | 0 | 0 |
too-many-positional-arguments |
4 | 0 | 0 |
redundant-cast |
2 | 0 | 0 |
deprecated |
0 | 1 | 0 |
index-out-of-bounds |
1 | 0 | 0 |
possibly-unresolved-reference |
0 | 1 | 0 |
type-assertion-failure |
0 | 1 | 0 |
| Total | 2,913 | 497 | 191 |
CodSpeed Performance ReportMerging #21363 will degrade performances by 91.64%Comparing Summary
Benchmarks breakdown
Footnotes
|
3bf3444 to
00bf795
Compare
6e8d6db to
1734214
Compare
|
9bcd901 to
c59f1ab
Compare
c59f1ab to
bdb9a9d
Compare
This comment was marked as resolved.
This comment was marked as resolved.
bdb9a9d to
60a6a2c
Compare
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
This comment was marked as resolved.
60a6a2c to
1c255da
Compare
This comment was marked as resolved.
This comment was marked as resolved.
carljm
approved these changes
Nov 11, 2025
Contributor
carljm
left a comment
There was a problem hiding this comment.
Nice! Personally I think we can land this even with the big perf regression on pydantic, especially if that regression still leaves us on par with pyrefly. But of course if we can optimize, that's better!
Comment on lines
+529
to
+530
| An empty `typing.Union` leads to a `TypeError` at runtime, so we emit an error. We still infer | ||
| `Never` when used as a type expression, which seems reasonable for an empty union: |
Contributor
There was a problem hiding this comment.
👍 I don't know why this raises an error at runtime, seems silly to me.
1c255da to
e7bd98c
Compare
e9eecba to
a3e179e
Compare
a3e179e to
a4ebe5d
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Add support for
typing.Unionin implicit type aliases / in value position.Typing conformance tests
Two new tests are passing
Ecosystem impact
invalid-keydiagnostics on pydantic are caused by Implement "tagged union" narrowing for namedtuples and arbitrary nominal types ty#1479 (comment).Test Plan
New Markdown tests