Skip to content

Commit a77c852

Browse files
committed
fix panics and test failures
1 parent 21b36b0 commit a77c852

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

crates/red_knot_python_semantic/resources/mdtest/protocols.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1299,12 +1299,9 @@ class FalsyFooSubclass(FalsyFoo, Protocol):
12991299
y: str
13001300

13011301
def g(a: Truthy, b: FalsyFoo, c: FalsyFooSubclass):
1302-
# TODO should be `Literal[True]
1303-
reveal_type(bool(a)) # revealed: bool
1304-
# TODO should be `Literal[False]
1305-
reveal_type(bool(b)) # revealed: bool
1306-
# TODO should be `Literal[False]
1307-
reveal_type(bool(c)) # revealed: bool
1302+
reveal_type(bool(a)) # revealed: Literal[True]
1303+
reveal_type(bool(b)) # revealed: Literal[False]
1304+
reveal_type(bool(c)) # revealed: Literal[False]
13081305
```
13091306

13101307
It is not sufficient for a protocol to have a callable `__bool__` instance member that returns

crates/red_knot_python_semantic/src/types/class.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,16 @@ impl<'db> ClassType<'db> {
145145
}
146146

147147
pub(super) fn is_protocol(self, db: &'db dyn Db) -> bool {
148+
// We need to construct the types "instance of `str`" and "instance of `_version_info`"
149+
// very eagerly when type-checking a variety of things. That means we can't go through the normal
150+
// `ClassLiteral::is_protocol` path, because that would require us to evaluate the types of the bases
151+
// of these classes, which would cause Salsa cycles (or other, more exotic kinds of Salsa panics!).
152+
if matches!(
153+
self.known(db),
154+
Some(KnownClass::Str | KnownClass::VersionInfo)
155+
) {
156+
return false;
157+
}
148158
self.class_literal(db).0.is_protocol(db)
149159
}
150160

0 commit comments

Comments
 (0)