Skip to content
Merged
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 @@ -152,6 +152,21 @@ def _(c: Callable[[int, str], int]):
reveal_type(c) # revealed: (int, str, /) -> int
```

## Union

```py
from typing import Callable, Union

def _(
c: Callable[[Union[int, str]], int] | None,
d: None | Callable[[Union[int, str]], int],
e: None | Callable[[Union[int, str]], int] | int,
):
reveal_type(c) # revealed: ((int | str, /) -> int) | None
reveal_type(d) # revealed: None | ((int | str, /) -> int)
reveal_type(e) # revealed: None | ((int | str, /) -> int) | int
```

Comment on lines +155 to +169
Copy link
Contributor

@InSyncWithFoo InSyncWithFoo Mar 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about intersection types?

def _(
    c: Intersection[Callable[[Union[int, str]], int], A],
    d: Intersection[A, Callable[[Union[int, str]], int]],
    e: Intersection[A, Callable[[Union[int, str]], int], B],
    f: Intersection[Not[Callable[[int, str], Intersection[int, str]]]]
):
    reveal_type(c)  # revealed: ((int | str, /) -> int) & A
    reveal_type(d)  # revealed: A & ((int | str, /) -> int)
    reveal_type(e)  # revealed: A & ((int | str, /) -> int) & B
    reveal_type(f)  # revealed: ~((int, str, /) -> int & str)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, right now revealed type of f is ~(int, str, /) -> int & str". We can probably do this in another PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it's fine to do this in another PR.

## Nested

A nested `Callable` as one of the parameter types:
Expand Down
9 changes: 8 additions & 1 deletion crates/red_knot_python_semantic/src/types/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,14 @@ impl Display for DisplayUnionType<'_> {
db: self.db,
});
} else {
join.entry(&element.display(self.db));
if let Type::Callable(
CallableType::General(_) | CallableType::MethodWrapperDunderGet(_),
) = element
{
join.entry(&format_args!("({})", element.display(self.db)));
} else {
join.entry(&element.display(self.db));
}
}
}

Expand Down
Loading