Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1975,3 +1975,10 @@ def takes_str_or_float(x: float | str): ...

takes_str_or_float(round(1.0))
```

```py
def f(x: float) -> None:
reveal_type(round(x)) # revealed: int
reveal_type(round(x, None)) # revealed: int
reveal_type(round(x, 1)) # revealed: int | float
```
11 changes: 10 additions & 1 deletion crates/ty_python_semantic/src/types/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2011,7 +2011,6 @@ impl<'db, 'c> SpecializationBuilder<'db, 'c> {
return Err(error);
}
}

(Type::TypeVar(bound_typevar), ty) | (ty, Type::TypeVar(bound_typevar))
if bound_typevar.is_inferable(self.db, self.inferable) =>
{
Expand Down Expand Up @@ -2210,6 +2209,16 @@ impl<'db, 'c> SpecializationBuilder<'db, 'c> {
}
}

(formal @ Type::ProtocolInstance(_), actual @ Type::Union(_)) => {
// Reuse the protocol constraint solver for union-typed arguments too.
// This allows protocols like `_SupportsRound1[T]` to infer `T` from
// annotations such as `float`, which are interpreted as `int | float`.
let when =
actual.when_constraint_set_assignable_to(self.db, formal, self.constraints);
let _ = self.add_type_mappings_from_constraint_set(formal, when, &mut f);
return Ok(());
}

(formal, Type::NominalInstance(actual_nominal)) => {
// Special case: `formal` and `actual` are both tuples.
if let (Some(formal_tuple), Some(actual_tuple)) = (
Expand Down
Loading