Skip to content

Commit 509843f

Browse files
committed
Retain Unknown
1 parent d0df10a commit 509843f

File tree

4 files changed

+37
-41
lines changed

4 files changed

+37
-41
lines changed

crates/ty_python_semantic/resources/mdtest/call/overloads.md

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ from typing_extensions import reveal_type
880880
def _(a: int | None):
881881
reveal_type(
882882
# error: [no-matching-overload]
883-
# revealed: Any
883+
# revealed: Unknown
884884
f(
885885
A(),
886886
a1=a,
@@ -1231,8 +1231,8 @@ def _(list_int: list[int], list_any: list[Any]):
12311231
# All materializations of `list[Any]` are assignable to `list[int]` and `list[Any]`, but the
12321232
# return type of first and second overloads are not equivalent, so the overload matching
12331233
# is ambiguous.
1234-
reveal_type(f(list_any)) # revealed: Any
1235-
reveal_type(f(*(list_any,))) # revealed: Any
1234+
reveal_type(f(list_any)) # revealed: Unknown
1235+
reveal_type(f(*(list_any,))) # revealed: Unknown
12361236
```
12371237

12381238
### Single tuple argument
@@ -1277,8 +1277,8 @@ def _(int_str: tuple[int, str], int_any: tuple[int, Any], any_any: tuple[Any, An
12771277

12781278
# All materializations of `tuple[Any, Any]` are assignable to the parameters of all the
12791279
# overloads, but the return types aren't equivalent, so the overload matching is ambiguous
1280-
reveal_type(f(any_any)) # revealed: Any
1281-
reveal_type(f(*(any_any,))) # revealed: Any
1280+
reveal_type(f(any_any)) # revealed: Unknown
1281+
reveal_type(f(*(any_any,))) # revealed: Unknown
12821282
```
12831283

12841284
### `Unknown` passed into an overloaded function annotated with protocols
@@ -1309,15 +1309,15 @@ def f(a: Foo, b: list[str], c: list[LiteralString], e):
13091309
reveal_type(a.join(b)) # revealed: str
13101310
reveal_type(a.join(c)) # revealed: LiteralString
13111311

1312-
# Since both overloads match and they have return types that are not equivalent,
1312+
# since both overloads match and they have return types that are not equivalent,
13131313
# step (5) of the overload evaluation algorithm says we must evaluate the result of the
1314-
# call as `Any`.
1314+
# call as `Unknown`.
13151315
#
13161316
# Note: although the spec does not state as such (since intersections in general are not
13171317
# specified currently), `(str | LiteralString) & Unknown` might also be a reasonable type
13181318
# here (the union of all overload returns, intersected with `Unknown`) -- here that would
13191319
# simplify to `str & Unknown`.
1320-
reveal_type(a.join(e)) # revealed: Any
1320+
reveal_type(a.join(e)) # revealed: Unknown
13211321
```
13221322

13231323
### Multiple arguments
@@ -1367,8 +1367,8 @@ def _(list_int: list[int], list_any: list[Any], int_str: tuple[int, str], int_an
13671367
# All materializations of first argument is assignable to the second overload and for the second
13681368
# argument, they're assignable to the third overload, so no overloads are filtered out; the
13691369
# return types of the remaining overloads are not equivalent, so overload matching is ambiguous
1370-
reveal_type(f(list_int, any_any)) # revealed: Any
1371-
reveal_type(f(*(list_int, any_any))) # revealed: Any
1370+
reveal_type(f(list_int, any_any)) # revealed: Unknown
1371+
reveal_type(f(*(list_int, any_any))) # revealed: Unknown
13721372
```
13731373

13741374
### `LiteralString` and `str`
@@ -1400,8 +1400,8 @@ def _(literal: LiteralString, string: str, any: Any):
14001400

14011401
# `Any` matches both overloads, but the return types are not equivalent.
14021402
# Pyright and mypy both reveal `str` here, contrary to the spec.
1403-
reveal_type(f(any)) # revealed: Any
1404-
reveal_type(f(*(any,))) # revealed: Any
1403+
reveal_type(f(any)) # revealed: Unknown
1404+
reveal_type(f(*(any,))) # revealed: Unknown
14051405
```
14061406

14071407
### Generics
@@ -1436,11 +1436,11 @@ def _(list_int: list[int], list_str: list[str], list_any: list[Any], any: Any):
14361436
reveal_type(f(list_str)) # revealed: str
14371437
reveal_type(f(*(list_str,))) # revealed: str
14381438

1439-
reveal_type(f(list_any)) # revealed: Any
1440-
reveal_type(f(*(list_any,))) # revealed: Any
1439+
reveal_type(f(list_any)) # revealed: Unknown
1440+
reveal_type(f(*(list_any,))) # revealed: Unknown
14411441

1442-
reveal_type(f(any)) # revealed: Any
1443-
reveal_type(f(*(any,))) # revealed: Any
1442+
reveal_type(f(any)) # revealed: Unknown
1443+
reveal_type(f(*(any,))) # revealed: Unknown
14441444
```
14451445

14461446
### Generics (multiple arguments)
@@ -1513,7 +1513,7 @@ def _(a_int: A[int], a_str: A[str], a_any: A[Any]):
15131513
def _(b_int: B[int], b_str: B[str], b_any: B[Any]):
15141514
reveal_type(b_int.method()) # revealed: int
15151515
reveal_type(b_str.method()) # revealed: str
1516-
reveal_type(b_any.method()) # revealed: Any
1516+
reveal_type(b_any.method()) # revealed: Unknown
15171517
```
15181518

15191519
### Ambiguous `Any` overloads (multiple arguments)
@@ -1550,8 +1550,7 @@ def _(x: A[None], y: A[Any]) -> None:
15501550
assert_type(op(x, x), A[None])
15511551
assert_type(op(x, y), A[None])
15521552
assert_type(op(y, x), A[None])
1553-
assert_type(op(y, y), Any)
1554-
reveal_type(op(y, y)) # revealed: Any
1553+
reveal_type(op(y, y)) # revealed: Unknown
15551554
```
15561555

15571556
### Variadic argument
@@ -1595,11 +1594,11 @@ def _(arg: list[Any]):
15951594
# Matches both overload and the return types are equivalent
15961595
reveal_type(f1(*arg)) # revealed: A
15971596
# Matches both overload but the return types aren't equivalent
1598-
reveal_type(f2(*arg)) # revealed: Any
1597+
reveal_type(f2(*arg)) # revealed: Unknown
15991598
# Filters out the final overload and the return types are equivalent
16001599
reveal_type(f3(*arg)) # revealed: A
16011600
# Filters out the final overload but the return types aren't equivalent
1602-
reveal_type(f4(*arg)) # revealed: Any
1601+
reveal_type(f4(*arg)) # revealed: Unknown
16031602
```
16041603

16051604
### Varidic argument with generics
@@ -1658,15 +1657,15 @@ def _(args1: list[int], args2: list[Any]):
16581657
reveal_type(f2()) # revealed: tuple[Any, ...]
16591658
reveal_type(f2(1, 2)) # revealed: tuple[Literal[1], Literal[2]]
16601659
# TODO: Should be `tuple[Literal[1], Literal[2]]`
1661-
reveal_type(f2(x1=1, x2=2)) # revealed: Any
1660+
reveal_type(f2(x1=1, x2=2)) # revealed: Unknown
16621661
# TODO: Should be `tuple[Literal[2], Literal[1]]`
1663-
reveal_type(f2(x2=1, x1=2)) # revealed: Any
1662+
reveal_type(f2(x2=1, x1=2)) # revealed: Unknown
16641663
reveal_type(f2(1, 2, z=3)) # revealed: tuple[Any, ...]
16651664

16661665
reveal_type(f3(1, 2)) # revealed: tuple[Literal[1], Literal[2]]
16671666
reveal_type(f3(1, 2, 3)) # revealed: tuple[Any, ...]
16681667
# TODO: Should be `tuple[Literal[1], Literal[2]]`
1669-
reveal_type(f3(x1=1, x2=2)) # revealed: Any
1668+
reveal_type(f3(x1=1, x2=2)) # revealed: Unknown
16701669
reveal_type(f3(z=1)) # revealed: dict[str, Any]
16711670

16721671
# error: [no-matching-overload]
@@ -1823,8 +1822,8 @@ from typing import Any
18231822
from overloaded import A, B, C, f
18241823

18251824
def _(arg: tuple[A | B, Any]):
1826-
reveal_type(f(arg)) # revealed: A | Any
1827-
reveal_type(f(*(arg,))) # revealed: A | Any
1825+
reveal_type(f(arg)) # revealed: A | Unknown
1826+
reveal_type(f(*(arg,))) # revealed: A | Unknown
18281827
```
18291828

18301829
#### Both argument lists ambiguous
@@ -1857,8 +1856,8 @@ from typing import Any
18571856
from overloaded import A, B, C, f
18581857

18591858
def _(arg: tuple[A | B, Any]):
1860-
reveal_type(f(arg)) # revealed: Any
1861-
reveal_type(f(*(arg,))) # revealed: Any
1859+
reveal_type(f(arg)) # revealed: Unknown
1860+
reveal_type(f(*(arg,))) # revealed: Unknown
18621861
```
18631862

18641863
### Unknown argument with TypeVar overload
@@ -1888,10 +1887,10 @@ from nonexistent_module import something_unknown # error: [unresolved-import]
18881887

18891888
reveal_type(something_unknown) # revealed: Unknown
18901889

1891-
# The result should be `Any`, not `Literal[b""]`.
1892-
reveal_type(f(something_unknown)) # revealed: Any
1893-
reveal_type(f((something_unknown, something_unknown, something_unknown))) # revealed: Any
1894-
reveal_type(f((something_unknown, None, something_unknown))) # revealed: Any
1890+
# The result should be `Unknown`, not `Literal[b""]`.
1891+
reveal_type(f(something_unknown)) # revealed: Unknown
1892+
reveal_type(f((something_unknown, something_unknown, something_unknown))) # revealed: Unknown
1893+
reveal_type(f((something_unknown, None, something_unknown))) # revealed: Unknown
18951894

18961895
# Concrete arguments should still resolve correctly.
18971896
def _(s: str):

crates/ty_python_semantic/resources/mdtest/snapshots/overloads.md_-_Overloads_-_Argument_type_expans…_-_Optimization___Limit_…_(cd61048adbc17331).snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mdtest path: crates/ty_python_semantic/resources/mdtest/call/overloads.md
3838
4 | def _(a: int | None):
3939
5 | reveal_type(
4040
6 | # error: [no-matching-overload]
41-
7 | # revealed: Any
41+
7 | # revealed: Unknown
4242
8 | f(
4343
9 | A(),
4444
10 | a1=a,
@@ -82,7 +82,7 @@ error[no-matching-overload]: No overload of function `f` matches arguments
8282
--> src/mdtest_snippet.py:8:9
8383
|
8484
6 | # error: [no-matching-overload]
85-
7 | # revealed: Any
85+
7 | # revealed: Unknown
8686
8 | / f(
8787
9 | | A(),
8888
10 | | a1=a,

crates/ty_python_semantic/resources/mdtest/typed_dict.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,7 +1361,7 @@ def _(
13611361
reveal_type(person[str_key]) # revealed: Unknown
13621362

13631363
# No error here:
1364-
reveal_type(person[unknown_key]) # revealed: Any
1364+
reveal_type(person[unknown_key]) # revealed: Unknown
13651365

13661366
reveal_type(being["name"]) # revealed: str
13671367

crates/ty_python_semantic/src/types/call/bind.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use crate::types::signatures::{Parameter, ParameterForm, ParameterKind, Paramete
4444
use crate::types::tuple::{TupleLength, TupleSpec, TupleType};
4545
use crate::types::{
4646
BoundMethodType, BoundTypeVarIdentity, BoundTypeVarInstance, CallableSignature, CallableType,
47-
CallableTypeKind, ClassLiteral, DATACLASS_FLAGS, DataclassFlags, DataclassParams, DynamicType,
47+
CallableTypeKind, ClassLiteral, DATACLASS_FLAGS, DataclassFlags, DataclassParams,
4848
FieldInstance, GenericAlias, InternedConstraintSet, IntersectionType, KnownBoundMethodType,
4949
KnownClass, KnownInstanceType, LiteralValueTypeKind, MemberLookupPolicy, NominalInstanceType,
5050
PropertyInstanceType, SpecialFormType, TypeAliasType, TypeContext, TypeVarBoundOrConstraints,
@@ -2612,10 +2612,7 @@ impl<'db> CallableBinding<'db> {
26122612
}),
26132613
);
26142614

2615-
if top_materialized_argument_type
2616-
.when_assignable_to(db, parameter_types, InferableTypeVars::None)
2617-
.is_always_satisfied(db)
2618-
{
2615+
if top_materialized_argument_type.is_assignable_to(db, parameter_types) {
26192616
filter_remaining_overloads = true;
26202617
}
26212618
}
@@ -2773,7 +2770,7 @@ impl<'db> CallableBinding<'db> {
27732770
return match overload_call_return_type {
27742771
OverloadCallReturnType::ArgumentTypeExpansion(return_type) => return_type,
27752772
OverloadCallReturnType::ArgumentTypeExpansionLimitReached(_)
2776-
| OverloadCallReturnType::Ambiguous => Type::Dynamic(DynamicType::Any),
2773+
| OverloadCallReturnType::Ambiguous => Type::unknown(),
27772774
};
27782775
}
27792776
if let Some((_, first_overload)) = self.matching_overloads().next() {

0 commit comments

Comments
 (0)