[ty] Truncate Literal type display in some situations (#20928)

This commit is contained in:
Mark Z. Ding 2025-10-17 07:50:58 -04:00 committed by GitHub
parent baaa8dad3a
commit fc3b341529
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 158 additions and 37 deletions

View file

@ -138,3 +138,27 @@ def _(n: int):
# error: [unknown-argument]
y = f("foo", name="bar", unknown="quux")
```
### Truncation for long unions and literals
This test demonstrates a call where the expected type is a large mixed union. The diagnostic must
therefore truncate the long expected union type to avoid overwhelming output.
```py
from typing import Literal, Union
class A: ...
class B: ...
class C: ...
class D: ...
class E: ...
class F: ...
def f1(x: Union[Literal[1, 2, 3, 4, 5, 6, 7, 8], A, B, C, D, E, F]) -> int:
return 0
def _(n: int):
x = n
# error: [invalid-argument-type]
f1(x)
```

View file

@ -0,0 +1,56 @@
---
source: crates/ty_test/src/lib.rs
assertion_line: 427
expression: snapshot
---
---
mdtest name: union_call.md - Calling a union of function types - Try to cover all possible reasons - Truncation for long unions and literals
mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/union_call.md
---
# Python source files
## mdtest_snippet.py
```
1 | from typing import Literal, Union
2 |
3 | class A: ...
4 | class B: ...
5 | class C: ...
6 | class D: ...
7 | class E: ...
8 | class F: ...
9 |
10 | def f1(x: Union[Literal[1, 2, 3, 4, 5, 6, 7, 8], A, B, C, D, E, F]) -> int:
11 | return 0
12 |
13 | def _(n: int):
14 | x = n
15 | # error: [invalid-argument-type]
16 | f1(x)
```
# Diagnostics
```
error[invalid-argument-type]: Argument to function `f1` is incorrect
--> src/mdtest_snippet.py:16:8
|
14 | x = n
15 | # error: [invalid-argument-type]
16 | f1(x)
| ^ Expected `Literal[1, 2, 3, 4, 5, ... omitted 3 literals] | A | B | ... omitted 4 union elements`, found `int`
|
info: Function defined here
--> src/mdtest_snippet.py:10:5
|
8 | class F: ...
9 |
10 | def f1(x: Union[Literal[1, 2, 3, 4, 5, 6, 7, 8], A, B, C, D, E, F]) -> int:
| ^^ ----------------------------------------------------------- Parameter declared here
11 | return 0
|
info: rule `invalid-argument-type` is enabled by default
```