Skip to content

Commit dadcd48

Browse files
committed
remove panicking behaviour
1 parent 7e43d92 commit dadcd48

File tree

4 files changed

+5
-101
lines changed

4 files changed

+5
-101
lines changed

crates/red_knot_project/tests/check.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
use anyhow::{anyhow, Context};
22
use red_knot_project::{ProjectDatabase, ProjectMetadata};
3-
use red_knot_python_semantic::{HasType, Program, SemanticModel};
3+
use red_knot_python_semantic::{HasType, SemanticModel};
44
use ruff_db::files::{system_path_to_file, File};
55
use ruff_db::parsed::parsed_module;
66
use ruff_db::system::{SystemPath, SystemPathBuf, TestSystem};
77
use ruff_python_ast::visitor::source_order;
88
use ruff_python_ast::visitor::source_order::SourceOrderVisitor;
9-
use ruff_python_ast::{
10-
self as ast, Alias, Expr, Parameter, ParameterWithDefault, PythonVersion, Stmt,
11-
};
12-
use salsa::Setter;
9+
use ruff_python_ast::{self as ast, Alias, Expr, Parameter, ParameterWithDefault, Stmt};
1310

1411
fn setup_db(project_root: &SystemPath, system: TestSystem) -> anyhow::Result<ProjectDatabase> {
1512
let project = ProjectMetadata::discover(project_root, &system)?;
@@ -87,14 +84,6 @@ fn run_corpus_tests(pattern: &str) -> anyhow::Result<()> {
8784

8885
let mut db = setup_db(&root, system.clone())?;
8986

90-
// Set the target Python version to the latest one supported by red-knot.
91-
// This enables us to parse syntax (and accurately infer types) for code that only works
92-
// on newer Python versions.
93-
// (Some of the corpus snippets use syntax that is only available on newer Python versions.)
94-
Program::get(&db)
95-
.set_python_version(&mut db)
96-
.to(PythonVersion::latest());
97-
9887
let workspace_root = get_cargo_workspace_root()?;
9988
let workspace_root = workspace_root.to_string();
10089

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,11 @@ typeshed = "/typeshed"
3232
`/typeshed/stdlib/builtins.pyi`:
3333

3434
```pyi
35-
class object: ...
3635
class Custom: ...
3736

3837
custom_builtin: Custom
3938
```
4039

41-
`/typeshed/stdlib/types.pyi`:
42-
43-
```pyi
44-
class ModuleType: ...
45-
```
46-
4740
`/typeshed/stdlib/typing_extensions.pyi`:
4841

4942
```pyi
@@ -74,12 +67,6 @@ foo = bar
7467
bar = 1
7568
```
7669

77-
`/typeshed/stdlib/types.pyi`:
78-
79-
```pyi
80-
class ModuleType: ...
81-
```
82-
8370
`/typeshed/stdlib/typing_extensions.pyi`:
8471

8572
```pyi

crates/red_knot_python_semantic/resources/mdtest/mdtest_custom_typeshed.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,11 @@ We can then place custom stub files in `/typeshed/stdlib`, for example:
2222
`/typeshed/stdlib/builtins.pyi`:
2323

2424
```pyi
25-
class object: ...
26-
class type: ...
27-
class ellipsis: ...
28-
2925
class BuiltinClass: ...
3026

3127
builtin_symbol: BuiltinClass
3228
```
3329

34-
`/typeshed/stdlib/types.pyi`:
35-
36-
```pyi
37-
class ModuleType: ...
38-
```
39-
4030
`/typeshed/stdlib/sys/__init__.pyi`:
4131

4232
```pyi
@@ -80,18 +70,11 @@ class OldClass: ...
8070
class NewClass: ...
8171
```
8272

83-
`/typeshed/stdlib/types.pyi`:
84-
85-
```pyi
86-
class ModuleType: ...
87-
```
88-
8973
`/typeshed/stdlib/VERSIONS`:
9074

9175
```text
9276
old_module: 3.0-
9377
new_module: 3.11-
94-
types: 3.9-
9578
```
9679

9780
```py
@@ -119,12 +102,6 @@ typeshed = "/typeshed"
119102
def reveal_type(obj, /): ...
120103
```
121104

122-
`/typeshed/stdlib/types.pyi`:
123-
124-
```pyi
125-
class ModuleType: ...
126-
```
127-
128105
```py
129106
reveal_type(()) # revealed: tuple[()]
130107
```

crates/red_knot_python_semantic/src/types/class.rs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -951,11 +951,7 @@ impl<'db> KnownClass {
951951
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`]
952952
/// representing all possible instances of the class.
953953
///
954-
/// ## Panics
955-
///
956-
/// This method will panic if the class cannot be found in typeshed
957-
/// and *either* the `test` feature *or* the `debug_assertions` feature is enabled.
958-
/// See [`KnownClass::to_class_literal`] for more details.
954+
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
959955
pub(crate) fn to_instance(self, db: &'db dyn Db) -> Type<'db> {
960956
self.to_class_literal(db)
961957
.into_class_literal()
@@ -986,42 +982,7 @@ impl<'db> KnownClass {
986982

987983
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`] representing that class-literal.
988984
///
989-
/// ## Invariants upheld by this method
990-
///
991-
/// When executed outside of tests, this method should never panic: if the symbol cannot
992-
/// be found in typeshed, the method simply returns [`Type::unknown()`].
993-
/// When executed in the context of tests, however, this method panics rather than falling
994-
/// back to `Unknown` if a symbol cannot be found.
995-
///
996-
/// This means that two desirable properties are upheld:
997-
/// 1. red-knot works on (nearly) arbitrary custom typesheds without panicking when run
998-
/// outside of our tests. This is a potentially useful feature for a variety of reasons.
999-
/// 2. Implicit behaviour is kept to a minimum in the context of our tests.
1000-
/// If [`KnownClass`] symbols silently fell back to `Unknown` in the context of a test
1001-
/// that used a custom typeshed directory, it might lead to hard-to-debug test failures
1002-
/// and confusing behaviour.
1003-
///
1004-
/// ## When this method panics
1005-
///
1006-
/// This method panics if both the following conditions are met:
1007-
/// 1. Either no symbol can be found for the class in typeshed,
1008-
/// OR the found symbol is not a class definition,
1009-
/// OR the found symbol is possibly unbound.
1010-
/// 2. Either the `test` feature is enabled,
1011-
/// OR the `debug_assertions` feature is enabled.
1012-
///
1013-
/// If condition (1) is met but condition (2) is not, we simply log a debug warning
1014-
/// and fall back to `Unknown`.
1015-
///
1016-
/// ## Why do we panic if *either* `test` *or* `debug_assertions` is enabled?
1017-
///
1018-
/// Some of our tests are not run in a context where the `test` feature is enabled
1019-
/// (for example, mdtests). Meanwhile, other tests are never run in our CI with
1020-
/// `debug_assertions` enabled, because running them with a debug build would take too
1021-
/// long (our property tests). For both of these tests, however, we want to avoid
1022-
/// special-cases symbols silently falling back to `Unknown`. Panicking if *either*
1023-
/// `test` is enabled *or* `debug_assertions` is enabled covers both cases, while still
1024-
/// avoiding panicking when red-knot is run on production code.
985+
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
1025986
pub(crate) fn to_class_literal(self, db: &'db dyn Db) -> Type<'db> {
1026987
// a cache of the `KnownClass`es that we have already failed to lookup in typeshed
1027988
// (and therefore that we've already logged a warning for)
@@ -1030,12 +991,6 @@ impl<'db> KnownClass {
1030991
self.try_to_class_literal(db)
1031992
.map(Type::ClassLiteral)
1032993
.unwrap_or_else(|lookup_error| {
1033-
assert!(
1034-
!cfg!(any(test, debug_assertions)),
1035-
"{}",
1036-
lookup_error.display(db, self)
1037-
);
1038-
1039994
if MESSAGES.lock().unwrap().insert(self) {
1040995
if matches!(
1041996
lookup_error,
@@ -1063,11 +1018,7 @@ impl<'db> KnownClass {
10631018
/// Lookup a [`KnownClass`] in typeshed and return a [`Type`]
10641019
/// representing that class and all possible subclasses of the class.
10651020
///
1066-
/// ## Panics
1067-
///
1068-
/// This method will panic if the class cannot be found in typeshed
1069-
/// and *either* the `test` feature *or* the `debug_assertions` feature is enabled.
1070-
/// See [`KnownClass::to_class_literal`] for more details.
1021+
/// If the class cannot be found in typeshed, a debug-level log message will be emitted stating this.
10711022
pub(crate) fn to_subclass_of(self, db: &'db dyn Db) -> Type<'db> {
10721023
self.to_class_literal(db)
10731024
.into_class_literal()

0 commit comments

Comments
 (0)