Skip to content

Commit 694d823

Browse files
committed
pretend the only two protocols are the ones in the typing module
1 parent 8551a32 commit 694d823

File tree

5 files changed

+234
-47
lines changed

5 files changed

+234
-47
lines changed

crates/red_knot_python_semantic/resources/mdtest/import/builtins.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ Builtin symbols can be explicitly imported:
77
```py
88
import builtins
99

10-
reveal_type(builtins.chr) # revealed: def chr(i: int | SupportsIndex, /) -> str
10+
reveal_type(builtins.chr) # revealed: def chr(i: SupportsIndex, /) -> str
1111
```
1212

1313
## Implicit use of builtin
1414

1515
Or used implicitly:
1616

1717
```py
18-
reveal_type(chr) # revealed: def chr(i: int | SupportsIndex, /) -> str
18+
reveal_type(chr) # revealed: def chr(i: SupportsIndex, /) -> str
1919
reveal_type(str) # revealed: Literal[str]
2020
```
2121

crates/red_knot_python_semantic/resources/mdtest/scopes/builtin.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if returns_bool():
1313
chr: int = 1
1414

1515
def f():
16-
reveal_type(chr) # revealed: int | (def chr(i: int | SupportsIndex, /) -> str)
16+
reveal_type(chr) # revealed: int | (def chr(i: SupportsIndex, /) -> str)
1717
```
1818

1919
## Conditionally global or builtin, with annotation
@@ -28,5 +28,5 @@ if returns_bool():
2828
chr: int = 1
2929

3030
def f():
31-
reveal_type(chr) # revealed: int | (def chr(i: int | SupportsIndex, /) -> str)
31+
reveal_type(chr) # revealed: int | (def chr(i: SupportsIndex, /) -> str)
3232
```

crates/red_knot_python_semantic/src/types.rs

-32
Original file line numberDiff line numberDiff line change
@@ -1448,38 +1448,6 @@ impl<'db> Type<'db> {
14481448
true
14491449
}
14501450

1451-
// TODO: This is a workaround to avoid false positives (e.g. when checking function calls
1452-
// with `SupportsIndex` parameters), which should be removed when we understand protocols.
1453-
(lhs, Type::NominalInstance(instance))
1454-
if instance.class().is_known(db, KnownClass::SupportsIndex) =>
1455-
{
1456-
match lhs {
1457-
Type::NominalInstance(instance)
1458-
if matches!(
1459-
instance.class().known(db),
1460-
Some(KnownClass::Int | KnownClass::SupportsIndex)
1461-
) =>
1462-
{
1463-
true
1464-
}
1465-
Type::IntLiteral(_) => true,
1466-
_ => false,
1467-
}
1468-
}
1469-
1470-
// TODO: ditto for avoiding false positives when checking function calls with `Sized` parameters.
1471-
(lhs, Type::NominalInstance(instance))
1472-
if instance.class().is_known(db, KnownClass::Sized) =>
1473-
{
1474-
matches!(
1475-
lhs.to_meta_type(db).member(db, "__len__"),
1476-
SymbolAndQualifiers {
1477-
symbol: Symbol::Type(..),
1478-
..
1479-
}
1480-
)
1481-
}
1482-
14831451
(Type::NominalInstance(self_instance), Type::NominalInstance(target_instance)) => {
14841452
self_instance.is_assignable_to(db, target_instance)
14851453
}

0 commit comments

Comments
 (0)