Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Decompose bool to Literal[True, False] in unions and intersections (v2) #15773

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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 @@ -58,7 +58,22 @@ reveal_type(c >= d) # revealed: Literal[True]
#### Results with Ambiguity

```py
def _(x: bool, y: int):
class P:
def __lt__(self, other: "P") -> bool:
return True

def __le__(self, other: "P") -> bool:
return True

def __gt__(self, other: "P") -> bool:
return True

def __ge__(self, other: "P") -> bool:
return True

class Q(P): ...

def _(x: P, y: Q):
a = (x,)
b = (y,)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ else:
reveal_type(x) # revealed: slice
finally:
# TODO: should be `Literal[1] | str | bytes | bool | memoryview | float | range | slice`
reveal_type(x) # revealed: bool | float | slice
reveal_type(x) # revealed: bool | slice | float

reveal_type(x) # revealed: bool | float | slice
reveal_type(x) # revealed: bool | slice | float
```

## Nested `try`/`except` blocks
Expand Down Expand Up @@ -534,7 +534,7 @@ try:
reveal_type(x) # revealed: slice
finally:
# TODO: should be `Literal[1] | str | bytes | bool | memoryview | float | range | slice`
reveal_type(x) # revealed: bool | float | slice
reveal_type(x) # revealed: bool | slice | float
x = 2
reveal_type(x) # revealed: Literal[2]
reveal_type(x) # revealed: Literal[2]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ else:
if x and not x:
reveal_type(x) # revealed: Never
else:
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | tuple[()] | None

if not (x and not x):
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | tuple[()] | None
else:
reveal_type(x) # revealed: Never

if x or not x:
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | tuple[()] | None
else:
reveal_type(x) # revealed: Never

if not (x or not x):
reveal_type(x) # revealed: Never
else:
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | None | tuple[()]
reveal_type(x) # revealed: Literal[0, -1, "", "foo", b"", b"bar"] | bool | tuple[()] | None

if (isinstance(x, int) or isinstance(x, str)) and x:
reveal_type(x) # revealed: Literal[-1, True, "foo"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,16 @@ static_assert(is_assignable_to(Never, type[str]))
static_assert(is_assignable_to(Never, type[Any]))
```

### `bool` is assignable to unions that include `bool`

Since we decompose `bool` to `Literal[True, False]` in unions, it would be surprisingly easy to get
this wrong if we forgot to normalize `bool` to `Literal[True, False]` when it appeared on the
left-hand side in `Type::is_assignable_to()`.

```py
from knot_extensions import is_assignable_to, static_assert

static_assert(is_assignable_to(bool, str | bool))
```

[typing documentation]: https://typing.readthedocs.io/en/latest/spec/concepts.html#the-assignable-to-or-consistent-subtyping-relation
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,48 @@ class R: ...
static_assert(is_equivalent_to(Intersection[tuple[P | Q], R], Intersection[tuple[Q | P], R]))
```

## Unions containing tuples containing `bool`

```py
from knot_extensions import is_equivalent_to, static_assert
from typing_extensions import Literal

class P: ...

static_assert(is_equivalent_to(tuple[Literal[True, False]] | P, tuple[bool] | P))
static_assert(is_equivalent_to(P | tuple[bool], P | tuple[Literal[True, False]]))
```

## Unions and intersections involving `AlwaysTruthy`, `bool` and `AlwaysFalsy`

```py
from knot_extensions import AlwaysTruthy, AlwaysFalsy, static_assert, is_equivalent_to, Not
from typing_extensions import Literal

static_assert(is_equivalent_to(AlwaysTruthy | bool, Literal[False] | AlwaysTruthy))
static_assert(is_equivalent_to(AlwaysFalsy | bool, Literal[True] | AlwaysFalsy))
static_assert(is_equivalent_to(Not[AlwaysTruthy] | bool, Not[AlwaysTruthy] | Literal[True]))
static_assert(is_equivalent_to(Not[AlwaysFalsy] | bool, Literal[False] | Not[AlwaysFalsy]))
```

## Unions and intersections involving `AlwaysTruthy`, `LiteralString` and `AlwaysFalsy`

```py
from knot_extensions import AlwaysTruthy, AlwaysFalsy, static_assert, is_equivalent_to, Not, Intersection
from typing_extensions import Literal, LiteralString

# TODO: these should all pass!

# error: [static-assert-error]
static_assert(is_equivalent_to(AlwaysTruthy | LiteralString, Literal[""] | AlwaysTruthy))
# error: [static-assert-error]
static_assert(is_equivalent_to(AlwaysFalsy | LiteralString, Intersection[LiteralString, Not[Literal[""]]] | AlwaysFalsy))
# error: [static-assert-error]
static_assert(is_equivalent_to(Not[AlwaysFalsy] | LiteralString, Literal[""] | Not[AlwaysFalsy]))
# error: [static-assert-error]
static_assert(
is_equivalent_to(Not[AlwaysTruthy] | LiteralString, Not[AlwaysTruthy] | Intersection[LiteralString, Not[Literal[""]]])
)
```

[the equivalence relation]: https://typing.readthedocs.io/en/latest/spec/glossary.html#term-equivalent
Original file line number Diff line number Diff line change
Expand Up @@ -449,5 +449,31 @@ static_assert(not is_subtype_of(Intersection[Unknown, int], int))
static_assert(not is_subtype_of(tuple[int, int], tuple[int, Unknown]))
```

## `bool` is a subtype of `AlwaysTruthy | AlwaysFalsy`

`bool` is equivalent to `Literal[True] | Literal[False]`. `Literal[True]` is a subtype of
`AlwaysTruthy` and `Literal[False]` is a subtype of `AlwaysFalsy`; it therefore stands to reason
that `bool` is a subtype of `AlwaysTruthy | AlwaysFalsy`.

```py
from knot_extensions import AlwaysTruthy, AlwaysFalsy, is_subtype_of, static_assert, Not, is_disjoint_from
from typing_extensions import Literal

static_assert(is_subtype_of(bool, AlwaysTruthy | AlwaysFalsy))

# the inverse also applies -- TODO: this should pass!
# See the TODO comments in the `Type::Intersection` branch of `Type::is_disjoint_from()`.
static_assert(is_disjoint_from(bool, Not[AlwaysTruthy | AlwaysFalsy])) # error: [static-assert-error]

# `Type::is_subtype_of` delegates many questions of `bool` subtyping to `int`,
# but set-theoretic types like intersections and unions are still handled differently to `int`
static_assert(is_subtype_of(Literal[True], Not[Literal[2]]))
static_assert(is_subtype_of(bool, Not[Literal[2]]))
static_assert(is_subtype_of(Literal[True], bool | None))
static_assert(is_subtype_of(bool, bool | None))

static_assert(not is_subtype_of(int, Not[Literal[2]]))
```

[special case for float and complex]: https://typing.readthedocs.io/en/latest/spec/special-types.html#special-cases-for-float-and-complex
[typing documentation]: https://typing.readthedocs.io/en/latest/spec/concepts.html#subtype-supertype-and-type-equivalence
79 changes: 72 additions & 7 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,29 @@ impl<'db> Type<'db> {
}
}

/// Normalize the type `bool` -> `Literal[True, False]`.
///
/// Using this method in various type-relational methods
/// ensures that the following invariants hold true:
///
/// - bool ≡ Literal[True, False]
/// - bool | T ≡ Literal[True, False] | T
/// - bool <: Literal[True, False]
/// - bool | T <: Literal[True, False] | T
/// - Literal[True, False] <: bool
/// - Literal[True, False] | T <: bool | T
#[must_use]
pub fn with_normalized_bools(self, db: &'db dyn Db) -> Self {
match self {
Type::Instance(InstanceType { class }) if class.is_known(db, KnownClass::Bool) => {
Type::normalized_bool(db)
}
// TODO: decompose `LiteralString` into `Literal[""] | TruthyLiteralString`?
// We'd need to rename this method... --Alex
_ => self,
}
}

/// Return a normalized version of `self` in which all unions and intersections are sorted
/// according to a canonical order, no matter how "deeply" a union/intersection may be nested.
#[must_use]
Expand Down Expand Up @@ -881,6 +904,13 @@ impl<'db> Type<'db> {
(Type::Never, _) => true,
(_, Type::Never) => false,

(Type::Instance(InstanceType { class }), _) if class.is_known(db, KnownClass::Bool) => {
Type::normalized_bool(db).is_subtype_of(db, target)
}
(_, Type::Instance(InstanceType { class })) if class.is_known(db, KnownClass::Bool) => {
self.is_subtype_of(db, Type::normalized_bool(db))
}

(Type::Union(union), _) => union
.elements(db)
.iter()
Expand Down Expand Up @@ -961,7 +991,7 @@ impl<'db> Type<'db> {
KnownClass::Str.to_instance(db).is_subtype_of(db, target)
}
(Type::BooleanLiteral(_), _) => {
KnownClass::Bool.to_instance(db).is_subtype_of(db, target)
KnownClass::Int.to_instance(db).is_subtype_of(db, target)
}
(Type::IntLiteral(_), _) => KnownClass::Int.to_instance(db).is_subtype_of(db, target),
(Type::BytesLiteral(_), _) => {
Expand Down Expand Up @@ -1093,6 +1123,13 @@ impl<'db> Type<'db> {
true
}

(Type::Instance(InstanceType { class }), _) if class.is_known(db, KnownClass::Bool) => {
Type::normalized_bool(db).is_assignable_to(db, target)
}
(_, Type::Instance(InstanceType { class })) if class.is_known(db, KnownClass::Bool) => {
self.is_assignable_to(db, Type::normalized_bool(db))
}

// A union is assignable to a type T iff every element of the union is assignable to T.
(Type::Union(union), ty) => union
.elements(db)
Expand Down Expand Up @@ -1183,6 +1220,12 @@ impl<'db> Type<'db> {
left.is_equivalent_to(db, right)
}
(Type::Tuple(left), Type::Tuple(right)) => left.is_equivalent_to(db, right),
(Type::Instance(InstanceType { class }), _) if class.is_known(db, KnownClass::Bool) => {
Type::normalized_bool(db).is_equivalent_to(db, other)
}
(_, Type::Instance(InstanceType { class })) if class.is_known(db, KnownClass::Bool) => {
self.is_equivalent_to(db, Type::normalized_bool(db))
}
_ => self == other && self.is_fully_static(db) && other.is_fully_static(db),
}
}
Expand Down Expand Up @@ -1241,6 +1284,13 @@ impl<'db> Type<'db> {
first.is_gradual_equivalent_to(db, second)
}

(Type::Instance(InstanceType { class }), _) if class.is_known(db, KnownClass::Bool) => {
Type::normalized_bool(db).is_gradual_equivalent_to(db, other)
}
(_, Type::Instance(InstanceType { class })) if class.is_known(db, KnownClass::Bool) => {
self.is_gradual_equivalent_to(db, Type::normalized_bool(db))
}

_ => false,
}
}
Expand All @@ -1255,6 +1305,13 @@ impl<'db> Type<'db> {

(Type::Dynamic(_), _) | (_, Type::Dynamic(_)) => false,

(Type::Instance(InstanceType { class }), ty)
| (ty, Type::Instance(InstanceType { class }))
if class.is_known(db, KnownClass::Bool) =>
{
Type::normalized_bool(db).is_disjoint_from(db, ty)
}

(Type::Union(union), other) | (other, Type::Union(union)) => union
.elements(db)
.iter()
Expand Down Expand Up @@ -2371,6 +2428,13 @@ impl<'db> Type<'db> {
KnownClass::NoneType.to_instance(db)
}

/// The type `Literal[True, False]`, which is exactly equivalent to `bool`
/// (and which `bool` is eagerly normalized to in several situations)
pub fn normalized_bool(db: &'db dyn Db) -> Type<'db> {
const LITERAL_BOOLS: [Type; 2] = [Type::BooleanLiteral(false), Type::BooleanLiteral(true)];
Type::Union(UnionType::new(db, Box::from(LITERAL_BOOLS)))
}

/// Return the type of `tuple(sys.version_info)`.
///
/// This is not exactly the type that `sys.version_info` has at runtime,
Expand Down Expand Up @@ -4642,18 +4706,19 @@ pub struct TupleType<'db> {
}

impl<'db> TupleType<'db> {
pub fn from_elements<T: Into<Type<'db>>>(
db: &'db dyn Db,
types: impl IntoIterator<Item = T>,
) -> Type<'db> {
pub fn from_elements<I, T>(db: &'db dyn Db, types: I) -> Type<'db>
where
I: IntoIterator<Item = T>,
T: Into<Type<'db>>,
{
let mut elements = vec![];

for ty in types {
let ty = ty.into();
let ty: Type<'db> = ty.into();
if ty.is_never() {
return Type::Never;
}
elements.push(ty);
elements.push(ty.with_normalized_bools(db));
}

Type::Tuple(Self::new(db, elements.into_boxed_slice()))
Expand Down
Loading
Loading