Skip to content

Commit

Permalink
Change back default python version
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Dec 3, 2024
1 parent 1b7ecdf commit caa4c88
Show file tree
Hide file tree
Showing 19 changed files with 52 additions and 41 deletions.
2 changes: 1 addition & 1 deletion crates/red_knot/src/target_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
pub enum TargetVersion {
Py37,
Py38,
#[default]
Py39,
Py310,
Py311,
Py312,
#[default]
Py313,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,3 @@ class C:

reveal_type(C.x) # revealed: int
```

## TODO

- declarations vs bindings => NoDefault: NoDefaultType
- conditional imports
- conditional class definitions
- compare with tests in if.md=>Statically known branches
- boundness
- TODO in `issubclass.md`
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ The fields of `sys.version_info` can be accessed by name:
import sys

reveal_type(sys.version_info.major >= 3) # revealed: Literal[True]
reveal_type(sys.version_info.minor >= 13) # revealed: Literal[True]
reveal_type(sys.version_info.minor >= 14) # revealed: Literal[False]
reveal_type(sys.version_info.minor >= 12) # revealed: Literal[True]
reveal_type(sys.version_info.minor >= 13) # revealed: Literal[False]
```

But the `micro`, `releaselevel` and `serial` fields are inferred as `@Todo` until we support
Expand All @@ -127,12 +127,12 @@ import sys
reveal_type(sys.version_info[0] < 3) # revealed: Literal[False]
reveal_type(sys.version_info[1] > 13) # revealed: Literal[False]

# revealed: tuple[Literal[3], Literal[13], int, Literal["alpha", "beta", "candidate", "final"], int]
# revealed: tuple[Literal[3], Literal[12], int, Literal["alpha", "beta", "candidate", "final"], int]
reveal_type(sys.version_info[:5])

reveal_type(sys.version_info[:2] >= (3, 13)) # revealed: Literal[True]
reveal_type(sys.version_info[0:2] >= (3, 14)) # revealed: Literal[False]
reveal_type(sys.version_info[:3] >= (3, 14, 1)) # revealed: Literal[False]
reveal_type(sys.version_info[:2] >= (3, 12)) # revealed: Literal[True]
reveal_type(sys.version_info[0:2] >= (3, 13)) # revealed: Literal[False]
reveal_type(sys.version_info[:3] >= (3, 13, 1)) # revealed: Literal[False]
reveal_type(sys.version_info[3] == "final") # revealed: bool
reveal_type(sys.version_info[3] == "finalllllll") # revealed: Literal[False]
```
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/python_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl PythonVersion {

impl Default for PythonVersion {
fn default() -> Self {
Self::PY313 // TODO: temporarily changed to 3.13 to activate all sys.version_info branches
Self::PY39
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/semantic_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ if True:

use_def.print(&db);

assert!(false);
panic!();
// let binding = use_def
// .first_public_binding(global_table.symbol_id_by_name(name).expect("symbol exists"))
// .expect("Expected with item definition for {name}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -950,7 +950,7 @@ where
.last()
.is_some_and(|case| case.guard.is_none() && case.pattern.is_wildcard())
{
self.flow_merge(after_subject, after_subject_cs.clone());
self.flow_merge(after_subject, after_subject_cs);
}
}
ast::Stmt::Try(ast::StmtTry {
Expand Down
11 changes: 6 additions & 5 deletions crates/red_knot_python_semantic/src/semantic_index/use_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ pub(crate) struct UseDefMap<'db> {

impl<'db> UseDefMap<'db> {
#[cfg(test)]
#[allow(clippy::print_stdout)]
pub(crate) fn print(&self, db: &dyn crate::db::Db) {
use crate::semantic_index::constraint::ConstraintNode;

Expand Down Expand Up @@ -305,14 +306,14 @@ impl<'db> UseDefMap<'db> {
println!("================");

for (id, bindings) in self.bindings_by_use.iter_enumerated() {
println!("{:?}:", id);
println!("{id:?}:");
for binding in bindings.iter() {
let definition = self.all_definitions[binding.definition];
let mut constraint_ids = binding.constraint_ids.peekable();
let mut active_constraint_ids =
binding.constraints_active_at_binding_ids.peekable();

println!(" * {:?}", definition);
println!(" * {definition:?}");

if constraint_ids.peek().is_some() {
println!(" Constraints:");
Expand Down Expand Up @@ -342,13 +343,13 @@ impl<'db> UseDefMap<'db> {
println!("================");

for (id, symbol) in self.public_symbols.iter_enumerated() {
println!("{:?}:", id);
println!("{id:?}:");
println!(" * Bindings:");
for binding in symbol.bindings().iter() {
let definition = self.all_definitions[binding.definition];
let mut constraint_ids = binding.constraint_ids.peekable();

println!(" {:?}", definition);
println!(" {definition:?}");

if constraint_ids.peek().is_some() {
println!(" Constraints:");
Expand All @@ -363,7 +364,7 @@ impl<'db> UseDefMap<'db> {
println!(" * Declarations:");
for (declaration, _) in symbol.declarations().iter() {
let definition = self.all_definitions[declaration];
println!(" {:?}", definition);
println!(" {definition:?}");
}

println!();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl<const B: usize> BitSet<B> {
}

/// Union in-place with another [`BitSet`].
#[allow(dead_code)]
pub(super) fn union(&mut self, other: &BitSet<B>) {
let mut max_len = self.blocks().len();
let other_len = other.blocks().len();
Expand Down
8 changes: 4 additions & 4 deletions crates/red_knot_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ impl<'db> Type<'db> {
}

/// Return true if this type is equivalent to type `other`.
pub(crate) fn is_equivalent_to(self, db: &'db dyn Db, other: Type<'db>) -> bool {
pub(crate) fn is_equivalent_to(self, _db: &'db dyn Db, other: Type<'db>) -> bool {
// TODO equivalent but not identical structural types, differently-ordered unions and
// intersections, other cases?

Expand Down Expand Up @@ -3186,7 +3186,7 @@ pub(crate) mod tests {
use ruff_python_ast as ast;
use test_case::test_case;

pub(crate) fn setup_db_with_python_version(python_version: PythonVersion) -> TestDb {
pub(crate) fn setup_db_with_python_version(target_version: PythonVersion) -> TestDb {
let db = TestDb::new();

let src_root = SystemPathBuf::from("/src");
Expand All @@ -3197,7 +3197,7 @@ pub(crate) mod tests {
Program::from_settings(
&db,
&ProgramSettings {
target_version: python_version,
target_version,
search_paths: SearchPathSettings::new(src_root),
},
)
Expand Down Expand Up @@ -3775,7 +3775,7 @@ pub(crate) mod tests {

#[test]
fn typing_vs_typeshed_no_default() {
let db = setup_db();
let db = setup_db_with_python_version(PythonVersion::PY313);

let typing_no_default = typing_symbol(&db, "NoDefault").expect_type();
let typing_extensions_no_default = typing_extensions_symbol(&db, "NoDefault").expect_type();
Expand Down
6 changes: 3 additions & 3 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5042,7 +5042,7 @@ mod tests {

use crate::db::tests::TestDb;
use crate::program::{Program, SearchPathSettings};
use crate::python_version::{self, PythonVersion};
use crate::python_version::PythonVersion;
use crate::semantic_index::definition::Definition;
use crate::semantic_index::symbol::FileScopeId;
use crate::semantic_index::{global_scope, semantic_index, symbol_table, use_def_map};
Expand All @@ -5056,7 +5056,7 @@ mod tests {

use super::*;

fn setup_db_with_python_version(python_version: PythonVersion) -> TestDb {
fn setup_db_with_python_version(target_version: PythonVersion) -> TestDb {
let db = TestDb::new();

let src_root = SystemPathBuf::from("/src");
Expand All @@ -5067,7 +5067,7 @@ mod tests {
Program::from_settings(
&db,
&ProgramSettings {
target_version: python_version,
target_version,
search_paths: SearchPathSettings::new(src_root),
},
)
Expand Down
12 changes: 10 additions & 2 deletions crates/red_knot_test/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ pub(crate) struct Db {
}

impl Db {
pub(crate) fn setup(workspace_root: SystemPathBuf) -> Self {
pub(crate) fn setup_with_python_version(
workspace_root: SystemPathBuf,
target_version: PythonVersion,
) -> Self {
let db = Self {
workspace_root,
storage: salsa::Storage::default(),
Expand All @@ -32,7 +35,7 @@ impl Db {
Program::from_settings(
&db,
&ProgramSettings {
target_version: PythonVersion::default(),
target_version,
search_paths: SearchPathSettings::new(db.workspace_root.clone()),
},
)
Expand All @@ -41,6 +44,11 @@ impl Db {
db
}

#[cfg(test)]
pub(crate) fn setup(workspace_root: SystemPathBuf) -> Self {
Self::setup_with_python_version(workspace_root, PythonVersion::default())
}

pub(crate) fn workspace_root(&self) -> &SystemPath {
&self.workspace_root
}
Expand Down
6 changes: 5 additions & 1 deletion crates/red_knot_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use camino::Utf8Path;
use colored::Colorize;
use parser as test_parser;
use red_knot_python_semantic::types::check_types;
use red_knot_python_semantic::PythonVersion;
use ruff_db::diagnostic::{Diagnostic, ParseDiagnostic};
use ruff_db::files::{system_path_to_file, File, Files};
use ruff_db::parsed::parsed_module;
Expand Down Expand Up @@ -30,7 +31,10 @@ pub fn run(path: &Utf8Path, long_title: &str, short_title: &str, test_name: &str
}
};

let mut db = db::Db::setup(SystemPathBuf::from("/src"));
// TODO: We currently run the tests with a target version of 3.12, as some tests rely
// on the presence of type aliases.
let mut db =
db::Db::setup_with_python_version(SystemPathBuf::from("/src"), PythonVersion::PY312);

let filter = std::env::var(MDTEST_TEST_FILTER).ok();
let mut any_failures = false;
Expand Down
2 changes: 1 addition & 1 deletion crates/red_knot_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ impl Settings {
pub enum TargetVersion {
Py37,
Py38,
#[default]
Py39,
Py310,
Py311,
Py312,
#[default]
Py313,
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/red_knot_workspace/src/workspace/metadata.rs
expression: "&workspace"
snapshot_kind: text
---
WorkspaceMetadata(
root: "/app",
Expand All @@ -23,7 +24,7 @@ WorkspaceMetadata(
program: ProgramSettings(
target_version: PythonVersion(
major: 3,
minor: 13,
minor: 9,
),
search_paths: SearchPathSettings(
extra_paths: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/red_knot_workspace/src/workspace/metadata.rs
expression: workspace
snapshot_kind: text
---
WorkspaceMetadata(
root: "/app",
Expand All @@ -23,7 +24,7 @@ WorkspaceMetadata(
program: ProgramSettings(
target_version: PythonVersion(
major: 3,
minor: 13,
minor: 9,
),
search_paths: SearchPathSettings(
extra_paths: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/red_knot_workspace/src/workspace/metadata.rs
expression: workspace
snapshot_kind: text
---
WorkspaceMetadata(
root: "/app",
Expand All @@ -23,7 +24,7 @@ WorkspaceMetadata(
program: ProgramSettings(
target_version: PythonVersion(
major: 3,
minor: 13,
minor: 9,
),
search_paths: SearchPathSettings(
extra_paths: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/red_knot_workspace/src/workspace/metadata.rs
expression: workspace
snapshot_kind: text
---
WorkspaceMetadata(
root: "/app",
Expand All @@ -23,7 +24,7 @@ WorkspaceMetadata(
program: ProgramSettings(
target_version: PythonVersion(
major: 3,
minor: 13,
minor: 9,
),
search_paths: SearchPathSettings(
extra_paths: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/red_knot_workspace/src/workspace/metadata.rs
expression: workspace
snapshot_kind: text
---
WorkspaceMetadata(
root: "/app",
Expand Down Expand Up @@ -36,7 +37,7 @@ WorkspaceMetadata(
program: ProgramSettings(
target_version: PythonVersion(
major: 3,
minor: 13,
minor: 9,
),
search_paths: SearchPathSettings(
extra_paths: [],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/red_knot_workspace/src/workspace/metadata.rs
expression: workspace
snapshot_kind: text
---
WorkspaceMetadata(
root: "/app",
Expand Down Expand Up @@ -49,7 +50,7 @@ WorkspaceMetadata(
program: ProgramSettings(
target_version: PythonVersion(
major: 3,
minor: 13,
minor: 9,
),
search_paths: SearchPathSettings(
extra_paths: [],
Expand Down

0 comments on commit caa4c88

Please sign in to comment.