ty_python_semantic: add union type context to function call type errors

This context gets added only when calling a function through a union
type.
This commit is contained in:
Andrew Gallant 2025-05-09 09:58:18 -04:00 committed by Andrew Gallant
parent 5ea3a52c8a
commit 346e82b572
42 changed files with 235 additions and 101 deletions

View file

@ -68,7 +68,7 @@ info[revealed-type]: Revealed type
```
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f` is incorrect
--> src/mdtest_snippet.py:12:15
|
10 | reveal_type(f(True)) # revealed: Literal[True]

View file

@ -83,7 +83,7 @@ info[revealed-type]: Revealed type
```
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f` is incorrect
--> src/mdtest_snippet.py:13:15
|
11 | reveal_type(f(None)) # revealed: None

View file

@ -65,7 +65,7 @@ info[revealed-type]: Revealed type
```
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f` is incorrect
--> src/mdtest_snippet.py:9:15
|
7 | reveal_type(f(True)) # revealed: Literal[True]

View file

@ -80,7 +80,7 @@ info[revealed-type]: Revealed type
```
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f` is incorrect
--> src/mdtest_snippet.py:10:15
|
8 | reveal_type(f(None)) # revealed: None

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:5
|
2 | return x * x

View file

@ -23,7 +23,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to bound method `square` is incorrect
--> src/mdtest_snippet.py:6:10
|
5 | c = C()

View file

@ -27,7 +27,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:3:13
|
1 | import package

View file

@ -22,7 +22,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:2:9
|
1 | def bar():

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:8
|
2 | return x * y * z

View file

@ -25,7 +25,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:8:8
|
6 | return x * y * z

View file

@ -24,7 +24,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:7:5
|
5 | # error: [invalid-argument-type]
@ -44,7 +44,7 @@ info: `invalid-argument-type` is enabled by default
```
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:7:10
|
5 | # error: [invalid-argument-type]
@ -64,7 +64,7 @@ info: `invalid-argument-type` is enabled by default
```
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:7:15
|
5 | # error: [invalid-argument-type]

View file

@ -20,7 +20,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `loads` is incorrect
--> src/mdtest_snippet.py:3:12
|
1 | import json

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:11
|
2 | return x * y * z

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:11
|
2 | return x * y * z

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:11
|
2 | return x * y * z

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:8
|
2 | return x * y * z

View file

@ -23,7 +23,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to bound method `__call__` is incorrect
--> src/mdtest_snippet.py:6:3
|
5 | c = C()

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:14
|
2 | return len(numbers)

View file

@ -21,7 +21,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/invalid_argu
# Diagnostics
```
error[invalid-argument-type]: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `foo` is incorrect
--> src/mdtest_snippet.py:4:20
|
2 | return len(numbers)

View file

@ -31,7 +31,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/union_call.m
# Diagnostics
```
error: lint:invalid-argument-type: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f2` is incorrect
--> src/mdtest_snippet.py:14:11
|
12 | # error: [too-many-positional-arguments]
@ -48,12 +48,14 @@ info: Function defined here
| ^^ --------- Parameter declared here
5 | return 0
|
info: `lint:invalid-argument-type` is enabled by default
info: Union variant `def f2(name: str) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int)`
info: `invalid-argument-type` is enabled by default
```
```
error: lint:too-many-positional-arguments: Too many positional arguments to function `f1`: expected 0, got 1
error[too-many-positional-arguments]: Too many positional arguments to function `f1`: expected 0, got 1
--> src/mdtest_snippet.py:14:11
|
12 | # error: [too-many-positional-arguments]
@ -61,6 +63,8 @@ error: lint:too-many-positional-arguments: Too many positional arguments to func
14 | x = f(3)
| ^
|
info: `lint:too-many-positional-arguments` is enabled by default
info: Union variant `def f1() -> int` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int)`
info: `too-many-positional-arguments` is enabled by default
```

View file

@ -30,7 +30,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/union_call.m
# Diagnostics
```
error: lint:invalid-argument-type: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f2` is incorrect
--> src/mdtest_snippet.py:13:11
|
11 | f = f2
@ -47,6 +47,8 @@ info: Function defined here
| ^^ --------- Parameter declared here
5 | return 0
|
info: `lint:invalid-argument-type` is enabled by default
info: Union variant `def f2(name: str) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1(a: int) -> int) | (def f2(name: str) -> int)`
info: `invalid-argument-type` is enabled by default
```

View file

@ -31,7 +31,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/union_call.m
# Diagnostics
```
error: lint:parameter-already-assigned: Multiple values provided for parameter `name` of function `f1`
error[parameter-already-assigned]: Multiple values provided for parameter `name` of function `f1`
--> src/mdtest_snippet.py:14:18
|
12 | # error: [parameter-already-assigned]
@ -39,12 +39,14 @@ error: lint:parameter-already-assigned: Multiple values provided for parameter `
14 | y = f("foo", name="bar", unknown="quux")
| ^^^^^^^^^^
|
info: `lint:parameter-already-assigned` is enabled by default
info: Union variant `def f1(name: str) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1(name: str) -> int) | (def any(*args, **kwargs) -> int)`
info: `parameter-already-assigned` is enabled by default
```
```
error: lint:unknown-argument: Argument `unknown` does not match any known parameter of function `f1`
error[unknown-argument]: Argument `unknown` does not match any known parameter of function `f1`
--> src/mdtest_snippet.py:14:30
|
12 | # error: [parameter-already-assigned]
@ -52,6 +54,8 @@ error: lint:unknown-argument: Argument `unknown` does not match any known parame
14 | y = f("foo", name="bar", unknown="quux")
| ^^^^^^^^^^^^^^
|
info: `lint:unknown-argument` is enabled by default
info: Union variant `def f1(name: str) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1(name: str) -> int) | (def any(*args, **kwargs) -> int)`
info: `unknown-argument` is enabled by default
```

View file

@ -53,9 +53,9 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/union_call.m
39 | else:
40 | f = PossiblyNotCallable()
41 | # error: [too-many-positional-arguments]
42 | # error: [invalid-argument-type] "Argument to this function is incorrect: Expected `str`, found `Literal[3]`"
42 | # error: [invalid-argument-type] "Argument to function `f2` is incorrect: Expected `str`, found `Literal[3]`"
43 | # error: [missing-argument]
44 | # error: [invalid-argument-type] "Argument to this function is incorrect: Argument type `Literal[3]` does not satisfy upper bound of type variable `T`"
44 | # error: [invalid-argument-type] "Argument to function `f4` is incorrect: Argument type `Literal[3]` does not satisfy upper bound of type variable `T`"
45 | # error: [call-non-callable] "Object of type `Literal[5]` is not callable"
46 | # error: [no-matching-overload]
47 | # error: [call-non-callable] "Object of type `PossiblyNotCallable` is not callable (possibly unbound `__call__` method)"
@ -65,7 +65,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/diagnostics/union_call.m
# Diagnostics
```
error: lint:call-non-callable: Object of type `Literal[5]` is not callable
error[call-non-callable]: Object of type `Literal[5]` is not callable
--> src/mdtest_snippet.py:48:9
|
46 | # error: [no-matching-overload]
@ -73,12 +73,14 @@ error: lint:call-non-callable: Object of type `Literal[5]` is not callable
48 | x = f(3)
| ^^^^
|
info: `lint:call-non-callable` is enabled by default
info: Union variant `Literal[5]` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `call-non-callable` is enabled by default
```
```
error: lint:call-non-callable: Object of type `PossiblyNotCallable` is not callable (possibly unbound `__call__` method)
error[call-non-callable]: Object of type `PossiblyNotCallable` is not callable (possibly unbound `__call__` method)
--> src/mdtest_snippet.py:48:9
|
46 | # error: [no-matching-overload]
@ -86,12 +88,14 @@ error: lint:call-non-callable: Object of type `PossiblyNotCallable` is not calla
48 | x = f(3)
| ^^^^
|
info: `lint:call-non-callable` is enabled by default
info: Union variant `PossiblyNotCallable` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `call-non-callable` is enabled by default
```
```
error: lint:missing-argument: No argument provided for required parameter `b` of function `f3`
error[missing-argument]: No argument provided for required parameter `b` of function `f3`
--> src/mdtest_snippet.py:48:9
|
46 | # error: [no-matching-overload]
@ -99,12 +103,14 @@ error: lint:missing-argument: No argument provided for required parameter `b` of
48 | x = f(3)
| ^^^^
|
info: `lint:missing-argument` is enabled by default
info: Union variant `def f3(a: int, b: int) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `missing-argument` is enabled by default
```
```
error: lint:no-matching-overload: No overload of method wrapper `__get__` of function `f` matches arguments
error[no-matching-overload]: No overload of method wrapper `__get__` of function `f` matches arguments
--> src/mdtest_snippet.py:48:9
|
46 | # error: [no-matching-overload]
@ -112,12 +118,14 @@ error: lint:no-matching-overload: No overload of method wrapper `__get__` of fun
48 | x = f(3)
| ^^^^
|
info: `lint:no-matching-overload` is enabled by default
info: Union variant `<method-wrapper `__get__` of `f`>` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `no-matching-overload` is enabled by default
```
```
error: lint:invalid-argument-type: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f2` is incorrect
--> src/mdtest_snippet.py:48:11
|
46 | # error: [no-matching-overload]
@ -134,12 +142,14 @@ info: Function defined here
| ^^ --------- Parameter declared here
7 | return 0
|
info: `lint:invalid-argument-type` is enabled by default
info: Union variant `def f2(name: str) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `invalid-argument-type` is enabled by default
```
```
error: lint:invalid-argument-type: Argument to this function is incorrect
error[invalid-argument-type]: Argument to function `f4` is incorrect
--> src/mdtest_snippet.py:48:11
|
46 | # error: [no-matching-overload]
@ -156,12 +166,14 @@ info: Type variable defined here
| ^^^^^^
13 | return 0
|
info: `lint:invalid-argument-type` is enabled by default
info: Union variant `def f4(x: T) -> int` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `invalid-argument-type` is enabled by default
```
```
error: lint:too-many-positional-arguments: Too many positional arguments to function `f1`: expected 0, got 1
error[too-many-positional-arguments]: Too many positional arguments to function `f1`: expected 0, got 1
--> src/mdtest_snippet.py:48:11
|
46 | # error: [no-matching-overload]
@ -169,6 +181,8 @@ error: lint:too-many-positional-arguments: Too many positional arguments to func
48 | x = f(3)
| ^
|
info: `lint:too-many-positional-arguments` is enabled by default
info: Union variant `def f1() -> int` is incompatible with this call site
info: Attempted to call union type `(def f1() -> int) | (def f2(name: str) -> int) | (def f3(a: int, b: int) -> int) | (def f4(x: T) -> int) | Literal[5] | Unknown | (<method-wrapper `__get__` of `f`>) | PossiblyNotCallable`
info: `too-many-positional-arguments` is enabled by default
```