Skip to content

Commit a3f8ea9

Browse files
committed
pass ParseOptions::target_version in red-knot
1 parent 4dba857 commit a3f8ea9

14 files changed

Lines changed: 72 additions & 31 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/red_knot_project/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use files::{Index, Indexed, IndexedFiles};
66
use metadata::settings::Settings;
77
pub use metadata::{ProjectDiscoveryError, ProjectMetadata};
88
use red_knot_python_semantic::lint::{LintRegistry, LintRegistryBuilder, RuleSelection};
9-
use red_knot_python_semantic::register_lints;
109
use red_knot_python_semantic::syntax::SyntaxDiagnostic;
1110
use red_knot_python_semantic::types::check_types;
11+
use red_knot_python_semantic::{python_version, 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);
337+
let parsed = parsed_module(db.upcast(), file, python_version(db.upcast()));
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::{HasType, SemanticModel};
3+
use red_knot_python_semantic::{python_version, 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);
168+
let ast = parsed_module(db, file, python_version(db));
169169

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

crates/red_knot_python_semantic/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,12 @@ 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: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ 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;
89
use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet};
910
use salsa::plumbing::AsId;
1011
use salsa::Update;
@@ -45,7 +46,13 @@ type SymbolMap = hashbrown::HashMap<ScopedSymbolId, (), FxBuildHasher>;
4546
pub(crate) fn semantic_index(db: &dyn Db, file: File) -> SemanticIndex<'_> {
4647
let _span = tracing::trace_span!("semantic_index", file = %file.path(db)).entered();
4748

48-
let parsed = parsed_module(db.upcast(), file);
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());
4956

5057
SemanticIndexBuilder::new(db, file, parsed).build()
5158
}
@@ -409,7 +416,7 @@ mod tests {
409416
use ruff_db::files::{system_path_to_file, File};
410417
use ruff_db::parsed::parsed_module;
411418
use ruff_db::system::DbWithTestSystem;
412-
use ruff_python_ast as ast;
419+
use ruff_python_ast::{self as ast, PythonVersion};
413420
use ruff_text_size::{Ranged, TextRange};
414421

415422
use crate::db::tests::TestDb;
@@ -830,7 +837,7 @@ def f(a: str, /, b: str, c: int = 1, *args, d: int = 2, **kwargs):
830837

831838
let use_def = index.use_def_map(comprehension_scope_id);
832839

833-
let module = parsed_module(&db, file).syntax();
840+
let module = parsed_module(&db, file, PythonVersion::default()).syntax();
834841
let element = module.body[0]
835842
.as_expr_stmt()
836843
.unwrap()
@@ -1079,7 +1086,7 @@ class C[T]:
10791086
#[test]
10801087
fn reachability_trivial() {
10811088
let TestCase { db, file } = test_case("x = 1; x");
1082-
let parsed = parsed_module(&db, file);
1089+
let parsed = parsed_module(&db, file, PythonVersion::default());
10831090
let scope = global_scope(&db, file);
10841091
let ast = parsed.syntax();
10851092
let ast::Stmt::Expr(ast::StmtExpr {
@@ -1112,7 +1119,7 @@ class C[T]:
11121119
let TestCase { db, file } = test_case("x = 1;\ndef test():\n y = 4");
11131120

11141121
let index = semantic_index(&db, file);
1115-
let parsed = parsed_module(&db, file);
1122+
let parsed = parsed_module(&db, file, PythonVersion::default());
11161123
let ast = parsed.syntax();
11171124

11181125
let x_stmt = ast.body[0].as_assign_stmt().unwrap();

crates/red_knot_python_semantic/src/semantic_model.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl_binding_has_ty!(ast::ParameterWithDefault);
166166
mod tests {
167167
use ruff_db::files::system_path_to_file;
168168
use ruff_db::parsed::parsed_module;
169+
use ruff_python_ast::PythonVersion;
169170

170171
use crate::db::tests::TestDbBuilder;
171172
use crate::{HasType, SemanticModel};
@@ -178,7 +179,7 @@ mod tests {
178179

179180
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
180181

181-
let ast = parsed_module(&db, foo);
182+
let ast = parsed_module(&db, foo, PythonVersion::default());
182183

183184
let function = ast.suite()[0].as_function_def_stmt().unwrap();
184185
let model = SemanticModel::new(&db, foo);
@@ -197,7 +198,7 @@ mod tests {
197198

198199
let foo = system_path_to_file(&db, "/src/foo.py").unwrap();
199200

200-
let ast = parsed_module(&db, foo);
201+
let ast = parsed_module(&db, foo, PythonVersion::default());
201202

202203
let class = ast.suite()[0].as_class_def_stmt().unwrap();
203204
let model = SemanticModel::new(&db, foo);
@@ -217,7 +218,7 @@ mod tests {
217218

218219
let bar = system_path_to_file(&db, "/src/bar.py").unwrap();
219220

220-
let ast = parsed_module(&db, bar);
221+
let ast = parsed_module(&db, bar, PythonVersion::default());
221222

222223
let import = ast.suite()[0].as_import_from_stmt().unwrap();
223224
let alias = &import.names[0];

crates/red_knot_python_semantic/src/suppression.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::lint::{GetLintError, Level, LintMetadata, LintRegistry, LintStatus};
2+
use crate::python_version;
23
use crate::types::{TypeCheckDiagnostic, TypeCheckDiagnostics};
34
use crate::{declare_lint, lint::LintId, Db};
45
use ruff_db::diagnostic::DiagnosticId;
@@ -88,7 +89,7 @@ declare_lint! {
8889

8990
#[salsa::tracked(return_ref)]
9091
pub(crate) fn suppressions(db: &dyn Db, file: File) -> Suppressions {
91-
let parsed = parsed_module(db.upcast(), file);
92+
let parsed = parsed_module(db.upcast(), file, python_version(db));
9293
let source = source_text(db.upcast(), file);
9394

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

crates/red_knot_python_semantic/src/types.rs

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

4620-
let call = &*parsed_module(&db, bar).syntax().body[1]
4620+
let call = &*parsed_module(&db, bar, PythonVersion::default())
4621+
.syntax()
4622+
.body[1]
46214623
.as_assign_stmt()
46224624
.unwrap()
46234625
.value;

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 10 additions & 3 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};
35+
use ruff_python_ast::{self as ast, AnyNodeRef, ExprContext, PythonVersion};
3636
use ruff_text_size::Ranged;
3737
use rustc_hash::{FxHashMap, FxHashSet};
3838
use salsa;
@@ -510,7 +510,14 @@ impl<'db> TypeInferenceBuilder<'db> {
510510
let node = scope.node(self.db());
511511
match node {
512512
NodeWithScopeKind::Module => {
513-
let parsed = parsed_module(self.db().upcast(), self.file());
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());
514521
self.infer_module(parsed.syntax());
515522
}
516523
NodeWithScopeKind::Function(function) => self.infer_function_body(function.node()),
@@ -6547,7 +6554,7 @@ mod tests {
65476554
fn dependency_implicit_instance_attribute() -> anyhow::Result<()> {
65486555
fn x_rhs_expression(db: &TestDb) -> Expression<'_> {
65496556
let file_main = system_path_to_file(db, "/src/main.py").unwrap();
6550-
let ast = parsed_module(db, file_main);
6557+
let ast = parsed_module(db, file_main, PythonVersion::default());
65516558
// Get the second statement in `main.py` (x = …) and extract the expression
65526559
// node on the right-hand side:
65536560
let x_rhs_node = &ast.syntax().body[1].as_assign_stmt().unwrap().value;

crates/red_knot_test/src/assertion.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
//! ```
3636
3737
use crate::db::Db;
38+
use red_knot_python_semantic::python_version;
3839
use regex::Regex;
3940
use ruff_db::files::File;
4041
use ruff_db::parsed::parsed_module;
@@ -58,7 +59,7 @@ impl InlineFileAssertions {
5859
pub(crate) fn from_file(db: &Db, file: File) -> Self {
5960
let source = source_text(db, file);
6061
let lines = line_index(db, file);
61-
let parsed = parsed_module(db, file);
62+
let parsed = parsed_module(db, file, python_version(db));
6263
let comment_ranges = CommentRanges::from(parsed.tokens());
6364
Self {
6465
comment_ranges,

0 commit comments

Comments
 (0)