From 067a8ac574d3840d320de18a5e19fc9db381fb48 Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Thu, 8 May 2025 16:58:35 +0200 Subject: [PATCH] [ty] Default to latest supported python version (#17938) --- crates/ruff_db/src/lib.rs | 2 +- crates/ruff_python_ast/src/python_version.rs | 4 ++ crates/ty/tests/cli.rs | 75 ++++++++++++++++++++ crates/ty_ide/src/inlay_hints.rs | 2 +- crates/ty_ide/src/lib.rs | 2 +- crates/ty_project/src/metadata/options.rs | 2 +- crates/ty_project/src/metadata/pyproject.rs | 2 +- crates/ty_python_semantic/src/types/class.rs | 2 +- 8 files changed, 85 insertions(+), 6 deletions(-) diff --git a/crates/ruff_db/src/lib.rs b/crates/ruff_db/src/lib.rs index 38e55e5c2e..cdb3071553 100644 --- a/crates/ruff_db/src/lib.rs +++ b/crates/ruff_db/src/lib.rs @@ -136,7 +136,7 @@ mod tests { } fn python_version(&self) -> ruff_python_ast::PythonVersion { - ruff_python_ast::PythonVersion::latest() + ruff_python_ast::PythonVersion::latest_ty() } } diff --git a/crates/ruff_python_ast/src/python_version.rs b/crates/ruff_python_ast/src/python_version.rs index a9add9d5a4..86977cf00f 100644 --- a/crates/ruff_python_ast/src/python_version.rs +++ b/crates/ruff_python_ast/src/python_version.rs @@ -59,6 +59,10 @@ impl PythonVersion { Self::PY313 } + pub const fn latest_ty() -> Self { + Self::PY313 + } + pub const fn as_tuple(self) -> (u8, u8) { (self.major, self.minor) } diff --git a/crates/ty/tests/cli.rs b/crates/ty/tests/cli.rs index 5ae28517ca..7a2276de0d 100644 --- a/crates/ty/tests/cli.rs +++ b/crates/ty/tests/cli.rs @@ -1,6 +1,7 @@ use anyhow::Context; use insta::internals::SettingsBindDropGuard; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; +use ruff_python_ast::PythonVersion; use std::fmt::Write; use std::path::{Path, PathBuf}; use std::process::Command; @@ -1263,6 +1264,80 @@ fn can_handle_large_binop_expressions() -> anyhow::Result<()> { Ok(()) } +#[test] +fn defaults_to_a_new_python_version() -> anyhow::Result<()> { + let case = TestCase::with_files([ + ( + "ty.toml", + &*format!( + r#" + [environment] + python-version = "{}" + python-platform = "linux" + "#, + PythonVersion::default() + ), + ), + ( + "main.py", + r#" + import os + + os.grantpt(1) # only available on unix, Python 3.13 or newer + "#, + ), + ])?; + + assert_cmd_snapshot!(case.command(), @r" + success: false + exit_code: 1 + ----- stdout ----- + error: lint:unresolved-attribute: Type `` has no attribute `grantpt` + --> main.py:4:1 + | + 2 | import os + 3 | + 4 | os.grantpt(1) # only available on unix, Python 3.13 or newer + | ^^^^^^^^^^ + | + info: `lint:unresolved-attribute` is enabled by default + + Found 1 diagnostic + + ----- stderr ----- + "); + + // Use default (which should be latest supported) + let case = TestCase::with_files([ + ( + "ty.toml", + r#" + [environment] + python-platform = "linux" + "#, + ), + ( + "main.py", + r#" + import os + + os.grantpt(1) # only available on unix, Python 3.13 or newer + "#, + ), + ])?; + + assert_cmd_snapshot!(case.command(), @r" + success: true + exit_code: 0 + ----- stdout ----- + All checks passed! + + ----- stderr ----- + "); + + Ok(()) +} + struct TestCase { _temp_dir: TempDir, _settings_scope: SettingsBindDropGuard, diff --git a/crates/ty_ide/src/inlay_hints.rs b/crates/ty_ide/src/inlay_hints.rs index f3fc12acca..3af793ab5a 100644 --- a/crates/ty_ide/src/inlay_hints.rs +++ b/crates/ty_ide/src/inlay_hints.rs @@ -191,7 +191,7 @@ mod tests { Program::from_settings( &db, ProgramSettings { - python_version: PythonVersion::latest(), + python_version: PythonVersion::latest_ty(), python_platform: PythonPlatform::default(), search_paths: SearchPathSettings { extra_paths: vec![], diff --git a/crates/ty_ide/src/lib.rs b/crates/ty_ide/src/lib.rs index 4532b700c3..fa06058e5e 100644 --- a/crates/ty_ide/src/lib.rs +++ b/crates/ty_ide/src/lib.rs @@ -227,7 +227,7 @@ mod tests { Program::from_settings( &db, ProgramSettings { - python_version: PythonVersion::latest(), + python_version: PythonVersion::latest_ty(), python_platform: PythonPlatform::default(), search_paths: SearchPathSettings { extra_paths: vec![], diff --git a/crates/ty_project/src/metadata/options.rs b/crates/ty_project/src/metadata/options.rs index 796b8d5092..16f0ab2287 100644 --- a/crates/ty_project/src/metadata/options.rs +++ b/crates/ty_project/src/metadata/options.rs @@ -61,7 +61,7 @@ impl Options { .environment .as_ref() .and_then(|env| env.python_version.as_deref().copied()) - .unwrap_or_default(); + .unwrap_or(PythonVersion::latest_ty()); let python_platform = self .environment .as_ref() diff --git a/crates/ty_project/src/metadata/pyproject.rs b/crates/ty_project/src/metadata/pyproject.rs index a32c3b60d4..14264335b4 100644 --- a/crates/ty_project/src/metadata/pyproject.rs +++ b/crates/ty_project/src/metadata/pyproject.rs @@ -103,7 +103,7 @@ impl Project { let major = u8::try_from(major).map_err(|_| ResolveRequiresPythonError::TooLargeMajor(major))?; let minor = - u8::try_from(minor).map_err(|_| ResolveRequiresPythonError::TooLargeMajor(minor))?; + u8::try_from(minor).map_err(|_| ResolveRequiresPythonError::TooLargeMinor(minor))?; Ok(Some( requires_python diff --git a/crates/ty_python_semantic/src/types/class.rs b/crates/ty_python_semantic/src/types/class.rs index bc6b0dda99..ce13b86cab 100644 --- a/crates/ty_python_semantic/src/types/class.rs +++ b/crates/ty_python_semantic/src/types/class.rs @@ -2734,7 +2734,7 @@ mod tests { Program::get(&db) .set_python_version(&mut db) - .to(PythonVersion::latest()); + .to(PythonVersion::latest_ty()); for class in KnownClass::iter() { assert_ne!(