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 @@ -87,6 +87,31 @@ static_assert(is_disjoint_from(memoryview, Foo))
static_assert(is_disjoint_from(type[memoryview], type[Foo]))
```

## Specialized `@final` types

```toml
[environment]
python-version = "3.12"
```

```py
from typing import final
from ty_extensions import static_assert, is_disjoint_from

@final
class Foo[T]:
def get(self) -> T:
raise NotImplementedError

class A: ...
class B: ...

static_assert(not is_disjoint_from(Foo[A], Foo[B]))

# TODO: `int` and `str` are disjoint bases, so these should be disjoint.
static_assert(not is_disjoint_from(Foo[int], Foo[str]))
```

## "Disjoint base" builtin types

Most other builtins can be subclassed and can even be used in multiple inheritance. However, builtin
Expand Down
11 changes: 8 additions & 3 deletions crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,12 +637,17 @@ impl<'db> ClassType<'db> {
return true;
}

// Optimisation: if either class is `@final`, we only need to do one `is_subclass_of` call.
if self.is_final(db) {
return self.is_subclass_of(db, other);
return self
.iter_mro(db)
.filter_map(ClassBase::into_class)
.any(|class| class.class_literal(db).0 == other.class_literal(db).0);
}
if other.is_final(db) {
return other.is_subclass_of(db, self);
return other
.iter_mro(db)
.filter_map(ClassBase::into_class)
.any(|class| class.class_literal(db).0 == self.class_literal(db).0);
}

// Two disjoint bases can only coexist in an MRO if one is a subclass of the other.
Expand Down
Loading