Skip to content

Commit 51f068d

Browse files
committed
Red-knot no longer panics!
1 parent 3f43c82 commit 51f068d

File tree

10 files changed

+42
-53
lines changed

10 files changed

+42
-53
lines changed

crates/red_knot_project/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use metadata::{ProjectDiscoveryError, ProjectMetadata};
88
use red_knot_python_semantic::lint::{LintRegistry, LintRegistryBuilder, RuleSelection};
99
use red_knot_python_semantic::syntax::SyntaxDiagnostic;
1010
use red_knot_python_semantic::types::check_types;
11-
use red_knot_python_semantic::{python_version, register_lints};
11+
use red_knot_python_semantic::{Program, register_lints};
1212
use ruff_db::diagnostic::{Diagnostic, DiagnosticId, ParseDiagnostic, Severity, Span};
1313
use ruff_db::files::{system_path_to_file, File};
1414
use ruff_db::parsed::parsed_module;
@@ -334,7 +334,7 @@ fn check_file_impl(db: &dyn Db, file: File) -> Vec<Box<dyn Diagnostic>> {
334334
return diagnostics;
335335
}
336336

337-
let parsed = parsed_module(db.upcast(), file, python_version(db.upcast()));
337+
let parsed = parsed_module(db.upcast(), file, Program::get(db).python_version(db));
338338
diagnostics.extend(parsed.errors().iter().map(|error| {
339339
let diagnostic: Box<dyn Diagnostic> = Box::new(ParseDiagnostic::new(file, error.clone()));
340340
diagnostic

crates/red_knot_project/tests/check.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use anyhow::{anyhow, Context};
22
use red_knot_project::{ProjectDatabase, ProjectMetadata};
3-
use red_knot_python_semantic::{python_version, HasType, SemanticModel};
3+
use red_knot_python_semantic::{Program, 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};
@@ -165,7 +165,7 @@ fn run_corpus_tests(pattern: &str) -> anyhow::Result<()> {
165165
fn pull_types(db: &ProjectDatabase, file: File) {
166166
let mut visitor = PullTypesVisitor::new(db, file);
167167

168-
let ast = parsed_module(db, file, python_version(db));
168+
let ast = parsed_module(db, file, Program::get(db).python_version(db));
169169

170170
visitor.visit_body(ast.suite());
171171
}

crates/red_knot_python_semantic/src/lib.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,3 @@ pub fn register_lints(registry: &mut LintRegistryBuilder) {
5050
registry.register_lint(&UNKNOWN_RULE);
5151
registry.register_lint(&INVALID_IGNORE_COMMENT);
5252
}
53-
54-
// TODO(brent) remove this. It should just be `Program::get(db).python_version(db)`, but for some
55-
// reason `tests::check_file_skips_type_checking_when_file_cant_be_read` fails when I use `get`, so
56-
// I factored this out instead of inlining everywhere
57-
pub fn python_version(db: &dyn Db) -> ruff_python_ast::PythonVersion {
58-
Program::try_get(db)
59-
.map(|program| program.python_version(db))
60-
.unwrap_or_default()
61-
}

crates/red_knot_python_semantic/src/semantic_index.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use ruff_db::files::File;
55
use ruff_db::parsed::parsed_module;
66
use ruff_index::{IndexSlice, IndexVec};
77

8-
use ruff_python_ast::PythonVersion;
98
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
109
use salsa::plumbing::AsId;
1110
use salsa::Update;
@@ -21,7 +20,7 @@ use crate::semantic_index::symbol::{
2120
FileScopeId, NodeWithScopeKey, NodeWithScopeRef, Scope, ScopeId, ScopedSymbolId, SymbolTable,
2221
};
2322
use crate::semantic_index::use_def::{EagerBindingsKey, ScopedEagerBindingsId, UseDefMap};
24-
use crate::Db;
23+
use crate::{Db, Program};
2524

2625
pub mod ast_ids;
2726
pub mod attribute_assignment;
@@ -46,13 +45,7 @@ type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), FxBuildHasher>;
4645
pub(crate) fn semantic_index(db: &dyn Db, file: File) -> SemanticIndex<'_> {
4746
let _span = tracing::trace_span!("semantic_index", file = %file.path(db)).entered();
4847

49-
// TODO(brent) need to pass the real PythonVersion here, but tests fail when I change it from
50-
// PythonVersion::default()
51-
//
52-
// I've tried my hacky `python_version` helper function and also
53-
// `Program::get(db).python_version(db)`, and many tests fail in both cases (18 with
54-
// `python_version`, 48 with `Program::get`)
55-
let parsed = parsed_module(db.upcast(), file, PythonVersion::default());
48+
let parsed = parsed_module(db.upcast(), file, Program::get(db).python_version(db));
5649

5750
SemanticIndexBuilder::new(db, file, parsed).build()
5851
}
@@ -415,11 +408,10 @@ impl FusedIterator for ChildrenIter<'_> {}
415408
mod tests {
416409
use ruff_db::files::{system_path_to_file, File};
417410
use ruff_db::parsed::parsed_module;
418-
use ruff_db::system::DbWithTestSystem;
419411
use ruff_python_ast::{self as ast, PythonVersion};
420412
use ruff_text_size::{Ranged, TextRange};
421413

422-
use crate::db::tests::TestDb;
414+
use crate::db::tests::{TestDb, TestDbBuilder};
423415
use crate::semantic_index::ast_ids::{HasScopedUseId, ScopedUseId};
424416
use crate::semantic_index::definition::{Definition, DefinitionKind};
425417
use crate::semantic_index::symbol::{
@@ -447,10 +439,14 @@ mod tests {
447439
}
448440

449441
fn test_case(content: impl ToString) -> TestCase {
450-
let mut db = TestDb::new();
451-
db.write_file("test.py", content).unwrap();
442+
const FILENAME: &str = "test.py";
452443

453-
let file = system_path_to_file(&db, "test.py").unwrap();
444+
let db = TestDbBuilder::new()
445+
.with_file(FILENAME, &content.to_string())
446+
.build()
447+
.unwrap();
448+
449+
let file = system_path_to_file(&db, FILENAME).unwrap();
454450

455451
TestCase { db, file }
456452
}

crates/red_knot_python_semantic/src/suppression.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::lint::{GetLintError, Level, LintMetadata, LintRegistry, LintStatus};
2-
use crate::python_version;
32
use crate::types::{TypeCheckDiagnostic, TypeCheckDiagnostics};
3+
use crate::Program;
44
use crate::{declare_lint, lint::LintId, Db};
55
use ruff_db::diagnostic::DiagnosticId;
66
use ruff_db::{files::File, parsed::parsed_module, source::source_text};
@@ -89,7 +89,7 @@ declare_lint! {
8989

9090
#[salsa::tracked(return_ref)]
9191
pub(crate) fn suppressions(db: &dyn Db, file: File) -> Suppressions {
92-
let parsed = parsed_module(db.upcast(), file, python_version(db));
92+
let parsed = parsed_module(db.upcast(), file, Program::get(db).python_version(db));
9393
let source = source_text(db.upcast(), file);
9494

9595
let mut builder = SuppressionsBuilder::new(&source, db.lint_registry());

crates/red_knot_python_semantic/src/types.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4617,7 +4617,7 @@ pub(crate) mod tests {
46174617
);
46184618
let events = db.take_salsa_events();
46194619

4620-
let call = &*parsed_module(&db, bar, PythonVersion::default())
4620+
let call = &*parsed_module(&db, bar, Program::get(&db).python_version(&db))
46214621
.syntax()
46224622
.body[1]
46234623
.as_assign_stmt()

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use itertools::{Either, Itertools};
3232
use ruff_db::diagnostic::{DiagnosticId, Severity};
3333
use ruff_db::files::File;
3434
use ruff_db::parsed::parsed_module;
35-
use ruff_python_ast::{self as ast, AnyNodeRef, ExprContext, PythonVersion};
35+
use ruff_python_ast::{self as ast, AnyNodeRef, ExprContext};
3636
use ruff_text_size::Ranged;
3737
use rustc_hash::{FxHashMap, FxHashSet};
3838
use salsa;
@@ -76,7 +76,7 @@ use crate::types::{
7676
};
7777
use crate::unpack::Unpack;
7878
use crate::util::subscript::{PyIndex, PySlice};
79-
use crate::Db;
79+
use crate::{Db, Program};
8080

8181
use super::call::CallError;
8282
use super::context::{InNoTypeCheck, InferContext, WithDiagnostics};
@@ -507,17 +507,15 @@ impl<'db> TypeInferenceBuilder<'db> {
507507
}
508508

509509
fn infer_region_scope(&mut self, scope: ScopeId<'db>) {
510-
let node = scope.node(self.db());
510+
let db = self.db();
511+
let node = scope.node(db);
511512
match node {
512513
NodeWithScopeKind::Module => {
513-
// TODO(brent) need to pass the real PythonVersion here, but tests fail when I
514-
// change it from PythonVersion::default()
515-
//
516-
// I've tried my hacky `python_version` helper function and also
517-
// `Program::get(db).python_version(db)`, and many tests fail in both cases (18 with
518-
// `python_version`, 19 with `Program::get`)
519-
let parsed =
520-
parsed_module(self.db().upcast(), self.file(), PythonVersion::default());
514+
let parsed = parsed_module(
515+
db.upcast(),
516+
self.file(),
517+
Program::get(db).python_version(db),
518+
);
521519
self.infer_module(parsed.syntax());
522520
}
523521
NodeWithScopeKind::Function(function) => self.infer_function_body(function.node()),
@@ -552,7 +550,7 @@ impl<'db> TypeInferenceBuilder<'db> {
552550
// Infer the deferred types for the definitions here to consider the end-of-scope
553551
// semantics.
554552
for definition in std::mem::take(&mut self.types.deferred) {
555-
self.extend(infer_deferred_types(self.db(), definition));
553+
self.extend(infer_deferred_types(db, definition));
556554
}
557555
assert!(
558556
self.types.deferred.is_empty(),

crates/red_knot_test/src/assertion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
//! ```
3636
3737
use crate::db::Db;
38-
use red_knot_python_semantic::python_version;
38+
use red_knot_python_semantic::Program;
3939
use regex::Regex;
4040
use ruff_db::files::File;
4141
use ruff_db::parsed::parsed_module;
@@ -59,7 +59,7 @@ impl InlineFileAssertions {
5959
pub(crate) fn from_file(db: &Db, file: File) -> Self {
6060
let source = source_text(db, file);
6161
let lines = line_index(db, file);
62-
let parsed = parsed_module(db, file, python_version(db));
62+
let parsed = parsed_module(db, file, Program::get(db).python_version(db));
6363
let comment_ranges = CommentRanges::from(parsed.tokens());
6464
Self {
6565
comment_ranges,

crates/red_knot_test/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use camino::Utf8Path;
44
use colored::Colorize;
55
use parser as test_parser;
66
use red_knot_python_semantic::types::check_types;
7-
use red_knot_python_semantic::{
8-
python_version, Program, ProgramSettings, SearchPathSettings, SitePackages,
9-
};
7+
use red_knot_python_semantic::{Program, ProgramSettings, SearchPathSettings, SitePackages};
108
use ruff_db::diagnostic::{Diagnostic, DisplayDiagnosticConfig, ParseDiagnostic};
119
use ruff_db::files::{system_path_to_file, File, Files};
1210
use ruff_db::panic::catch_unwind;
@@ -195,7 +193,7 @@ fn run_test(
195193
let failures: Failures = test_files
196194
.into_iter()
197195
.filter_map(|test_file| {
198-
let parsed = parsed_module(db, test_file.file, python_version(db));
196+
let parsed = parsed_module(db, test_file.file, Program::get(db).python_version(db));
199197

200198
let mut diagnostics: Vec<Box<_>> = parsed
201199
.errors()

crates/red_knot_wasm/src/lib.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use std::any::Any;
22

33
use js_sys::Error;
4-
use red_knot_python_semantic::python_version;
54
use wasm_bindgen::prelude::*;
65

76
use red_knot_project::metadata::options::{EnvironmentOptions, Options};
87
use red_knot_project::metadata::value::RangedValue;
98
use red_knot_project::ProjectMetadata;
109
use red_knot_project::{Db, ProjectDatabase};
10+
use red_knot_python_semantic::Program;
1111
use ruff_db::diagnostic::{Diagnostic, DisplayDiagnosticConfig};
1212
use ruff_db::files::{system_path_to_file, File};
1313
use ruff_db::system::walk_directory::WalkDirectoryBuilder;
@@ -135,16 +135,22 @@ impl Workspace {
135135

136136
/// Returns the parsed AST for `path`
137137
pub fn parsed(&self, file_id: &FileHandle) -> Result<String, Error> {
138-
let parsed =
139-
ruff_db::parsed::parsed_module(&self.db, file_id.file, python_version(&self.db));
138+
let parsed = ruff_db::parsed::parsed_module(
139+
&self.db,
140+
file_id.file,
141+
Program::get(&self.db).python_version(&self.db),
142+
);
140143

141144
Ok(format!("{:#?}", parsed.syntax()))
142145
}
143146

144147
/// Returns the token stream for `path` serialized as a string.
145148
pub fn tokens(&self, file_id: &FileHandle) -> Result<String, Error> {
146-
let parsed =
147-
ruff_db::parsed::parsed_module(&self.db, file_id.file, python_version(&self.db));
149+
let parsed = ruff_db::parsed::parsed_module(
150+
&self.db,
151+
file_id.file,
152+
Program::get(&self.db).python_version(&self.db),
153+
);
148154

149155
Ok(format!("{:#?}", parsed.tokens()))
150156
}

0 commit comments

Comments
 (0)