mirror of
https://github.com/astral-sh/ruff.git
synced 2026-11-23 01:50:49 +08:00
## Summary This does some polishing on the former `uv-metadata` diagnostic: ### Rename It renames the diagnostic from `uv-metadata` to `project-manager`. This is to make it less uv focused (we could support other project managers) and also make it less technical. `uv-metadata` is an implementation detail. For as far as users are concerned, ty failed to run the project manager to set up the Python environment or to load the project metadata. **Before** ``` warning[uv-metadata]: uv 0.12.2 is too old; upgrade `./old-uv` to uv 0.12.3 or newer --> pyproject.toml:1:1 Found 1 diagnostic ``` **After** ``` warning[project-manager]: Failed to load project metadata from uv: Found uv 0.12.2, but uv 0.12.3 or newer is required --> pyproject.toml:1:1 help: Upgrade `./old-uv` to uv 0.12.3 or newer Found 1 diagnostic ``` ### Setting subdiagnostic We now also attach a sub diagnostic that explains why ty uses the project manager when ty failed to find a supported uv version. This will become more important once we make the uv integration for scripts opt out. **Before** ``` error[uv-metadata]: Failed to invoke `uv workspace metadata`: failed to resolve uv executable: cannot find binary path --> script.py:2:1 Found 1 diagnostic ``` **After** ``` error[project-manager]: Setting up the Python environment with uv failed: could not find `uv` on `PATH` --> script.py:2:1 info: ty uses uv to install this script's dependencies because `project-manager.script` is set to `"uv"` --> script.py:5:28 | 5 | # project-manager.script = "uv" | ^^^^ configured here info: To install uv, see https://docs.astral.sh/uv/ Found 1 diagnostic ``` ### uv error message I changed how we show uv's error message when it fails to resolve dependencies: **Before** ``` error[uv-metadata]: `uv workspace metadata` failed with `exit status: 1` --> script.py:2:1 info: No solution found when resolving dependencies cause: Because missing-script-dependency was not found in the cache and you require missing-script-dependency==99.0.0, we can conclude that your requirements are unsatisfiable. hint: Packages were unavailable because the network was disabled. When the network is disabled, registry packages may only be read from the cache. Found 1 diagnostic ``` **After** ``` error[project-manager]: Setting up the Python environment with uv failed --> script.py:2:1 error: No solution found when resolving dependencies cause: Because missing-script-dependency was not found in the cache and you require missing-script-dependency==99.0.0, we can conclude that your requirements are unsatisfiable. hint: Packages were unavailable because the network was disabled. When the network is disabled, registry packages may only be read from the cache. error: `uv workspace metadata` exited with status 1 Found 1 diagnostic ``` I don't love it, but formatting this one is a bit tricky, because uv doesn't provide us with structured output and we need to preserve any whitespace as is, to not mess up uv provided code frames. And concise ``` script.py:2:1: error[project-manager] Setting up the Python environment with uv failed: No solution found when resolving dependencies Found 1 diagnostic ``` ## Notes to reviewers Stacked on #30527. Verified the diagnostics in full and concise CLI output and in the language server.
2208 lines
54 KiB
Rust
2208 lines
54 KiB
Rust
use insta_cmd::assert_cmd_snapshot;
|
|
use ty_static::EnvVars;
|
|
|
|
use crate::CliTest;
|
|
|
|
#[test]
|
|
fn inline_settings_replace_project_settings_and_overrides() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.rules]
|
|
unresolved-reference = "ignore"
|
|
|
|
[[tool.ty.overrides]]
|
|
include = ["script.py"]
|
|
|
|
[tool.ty.overrides.rules]
|
|
unresolved-reference = "error"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "warn"
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:7:7
|
|
|
|
|
7 | print(missing)
|
|
| ^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn verbose_rule_diagnostics_identify_script_metadata() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "warn"
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--verbose"), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:7:7
|
|
|
|
|
7 | print(missing)
|
|
| ^^^^^^^
|
|
info: rule `unresolved-reference` was selected in script metadata
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
INFO Indexed 1 file(s) in 0.000s
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_rule_diagnostics_point_to_script_metadata() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unknown-script-rule = "warn"
|
|
# ///
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unknown-rule]: Unknown rule `unknown-script-rule`
|
|
--> script.py:4:3
|
|
|
|
|
4 | # unknown-script-rule = "warn"
|
|
| ^^^^^^^^^^^^^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn shared_config_override_warns_once_across_scripts() -> anyhow::Result<()> {
|
|
let script = "
|
|
# /// script
|
|
# [tool.ty]
|
|
# ///
|
|
";
|
|
let case = CliTest::with_files([
|
|
(
|
|
"ty.toml",
|
|
r#"
|
|
[[overrides]]
|
|
[overrides.rules]
|
|
division-by-zero = "warn"
|
|
"#,
|
|
),
|
|
("a.py", script),
|
|
("b.py", script),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().args([
|
|
"--config-file", "ty.toml", "--output-format", "concise", "a.py", "b.py"
|
|
]), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
ty.toml:2:1: warning[unnecessary-overrides-section] Unnecessary `overrides` section: This overrides section applies to all files
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn renamed_rule_in_script_metadata() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-import = "ignore"
|
|
# ///
|
|
|
|
import missing
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[renamed-rule]: Rule `unresolved-import` has been renamed to `import-not-found`
|
|
--> script.py:4:3
|
|
|
|
|
4 | # unresolved-import = "ignore"
|
|
| ^^^^^^^^^^^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn script_inherits_project_python_version() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
("ty.toml", "environment.python-version = '3.14'"),
|
|
(
|
|
"script.py",
|
|
"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
",
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--output-format=concise"), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
script.py:9:13: info[revealed-type] Revealed type: `tuple[Literal[3], Literal[14]]`
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn script_python_requirement_overrides_project_version() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
("ty.toml", "environment.python-version = '3.14'"),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.13"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--output-format=concise"), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
script.py:9:13: info[revealed-type] Revealed type: `tuple[Literal[3], Literal[13]]`
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn metadata_without_tool_ty_inherits_project_settings_and_overrides() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.rules]
|
|
unresolved-reference = "ignore"
|
|
|
|
[tool.ty.analysis]
|
|
respect-type-ignore-comments = false
|
|
|
|
[[tool.ty.overrides]]
|
|
include = ["scripts/*.py"]
|
|
|
|
[tool.ty.overrides.rules]
|
|
invalid-assignment = "warn"
|
|
"#,
|
|
),
|
|
(
|
|
"scripts/inherit.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# [tool.uv]
|
|
# ///
|
|
|
|
print(missing)
|
|
value: int = "not an int" # type: ignore
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().args(["--output-format", "concise"]), @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
scripts/inherit.py:8:14: warning[invalid-assignment] Object of type `Literal["not an int"]` is not assignable to `int`
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn empty_tool_ty_disables_project_settings_and_overrides() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.rules]
|
|
unresolved-reference = "ignore"
|
|
|
|
[[tool.ty.overrides]]
|
|
include = ["script.py"]
|
|
|
|
[tool.ty.overrides.rules]
|
|
unresolved-reference = "warn"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
"
|
|
# /// script
|
|
# [tool.ty]
|
|
# ///
|
|
|
|
print(missing)
|
|
",
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--output-format=concise"), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
script.py:6:7: error[unresolved-reference] Name `missing` used when not defined
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn environment_options() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.13"
|
|
#
|
|
# [tool.ty.environment]
|
|
# python-version = "3.11"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:12:13
|
|
|
|
|
12 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[11]]`
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn inline_overrides_are_ignored() -> anyhow::Result<()> {
|
|
// TODO: Emit a diagnostic for options that are not allowed within scripts.
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "warn"
|
|
#
|
|
# [[tool.ty.overrides]]
|
|
# include = ["script.py"]
|
|
#
|
|
# [tool.ty.overrides.rules]
|
|
# unresolved-reference = "ignore"
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:13:7
|
|
|
|
|
13 | print(missing)
|
|
| ^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn inline_terminal_settings_do_not_apply() -> anyhow::Result<()> {
|
|
// TODO: Either support (when calling `ty check <script>`), or raise a diagnostic that this option is not supported
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "warn"
|
|
#
|
|
# [tool.ty.terminal]
|
|
# error-on-warning = false
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:10:7
|
|
|
|
|
10 | print(missing)
|
|
| ^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn inline_settings_override_user_configuration() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "error"
|
|
#
|
|
# [tool.ty.analysis]
|
|
# respect-type-ignore-comments = false
|
|
# ///
|
|
|
|
print(missing) # type: ignore
|
|
"#,
|
|
)?;
|
|
case.write_file(
|
|
case.user_config_directory().join("ty/ty.toml"),
|
|
r#"
|
|
[rules]
|
|
unresolved-reference = "ignore"
|
|
|
|
[analysis]
|
|
respect-type-ignore-comments = true
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:10:7
|
|
|
|
|
10 | print(missing) # type: ignore
|
|
| ^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn user_configuration_applies() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
print(missing)
|
|
print(suppressed) # type: ignore
|
|
"#,
|
|
)?;
|
|
case.write_file(
|
|
case.user_config_directory().join("ty/ty.toml"),
|
|
r#"
|
|
[rules]
|
|
unresolved-reference = "warn"
|
|
|
|
[analysis]
|
|
respect-type-ignore-comments = false
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:6:7
|
|
|
|
|
6 | print(missing)
|
|
| ^^^^^^^
|
|
|
|
warning[unresolved-reference]: Name `suppressed` used when not defined
|
|
--> script.py:7:7
|
|
|
|
|
7 | print(suppressed) # type: ignore
|
|
| ^^^^^^^^^^
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn cli_arguments_override_script_options() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "ignore"
|
|
#
|
|
# [tool.ty.analysis]
|
|
# respect-type-ignore-comments = false
|
|
# ///
|
|
|
|
print(missing)
|
|
print(suppressed) # type: ignore
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(
|
|
case.command()
|
|
.arg("--warn")
|
|
.arg("unresolved-reference")
|
|
.arg("--config")
|
|
.arg("analysis.respect-type-ignore-comments=true"),
|
|
@"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:10:7
|
|
|
|
|
10 | print(missing)
|
|
| ^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_config_replaces_inline_metadata() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"explicit.toml",
|
|
r#"
|
|
[rules]
|
|
unresolved-reference = "warn"
|
|
|
|
[analysis]
|
|
respect-type-ignore-comments = false
|
|
|
|
[[overrides]]
|
|
include = ["script.py"]
|
|
[overrides.analysis]
|
|
respect-type-ignore-comments = true
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "ignore"
|
|
#
|
|
# [tool.ty.analysis]
|
|
# respect-type-ignore-comments = false
|
|
# ///
|
|
|
|
print(missing)
|
|
print(suppressed) # type: ignore
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(
|
|
case.command().arg("--config-file").arg("explicit.toml"),
|
|
@"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
warning[unresolved-reference]: Name `missing` used when not defined
|
|
--> script.py:10:7
|
|
|
|
|
10 | print(missing)
|
|
| ^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn explicit_config_replaces_the_script_environment() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"explicit.toml",
|
|
r#"
|
|
[environment]
|
|
python-version = "3.12"
|
|
python-platform = "linux"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.13"
|
|
# [tool.ty.environment]
|
|
# python-platform = "win32"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
reveal_type(sys.platform)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--config-file").arg("explicit.toml"), @r#"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:11:13
|
|
|
|
|
11 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[12]]`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:12:13
|
|
|
|
|
12 | reveal_type(sys.platform)
|
|
| ^^^^^^^^^^^^ `Literal["linux"]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn cli_arguments_override_script_environment() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.13"
|
|
# [tool.ty.environment]
|
|
# python-platform = "win32"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
reveal_type(sys.platform)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(
|
|
case.command()
|
|
.arg("--python-version")
|
|
.arg("3.12")
|
|
.arg("--python-platform")
|
|
.arg("linux"),
|
|
@r#"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:11:13
|
|
|
|
|
11 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[12]]`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:12:13
|
|
|
|
|
12 | reveal_type(sys.platform)
|
|
| ^^^^^^^^^^^^ `Literal["linux"]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
"#
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn script_version_and_platform_are_isolated_from_project_configuration() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.environment]
|
|
python-version = "3.12"
|
|
python-platform = "linux"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.13"
|
|
# [tool.ty.environment]
|
|
# python-version = "3.11"
|
|
# python-platform = "win32"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
reveal_type(sys.platform)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:12:13
|
|
|
|
|
12 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[11]]`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:13:13
|
|
|
|
|
13 | reveal_type(sys.platform)
|
|
| ^^^^^^^^^^^^ `Literal["win32"]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn python_requirement_overrides_user_configuration() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.13"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
"#,
|
|
)?;
|
|
case.write_file(
|
|
case.user_config_directory().join("ty/ty.toml"),
|
|
r#"
|
|
[environment]
|
|
python-version = "3.12"
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:9:13
|
|
|
|
|
9 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[13]]`
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn scripts_have_no_implicit_first_party_roots() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
("shared.py", "value = 1\n"),
|
|
("src/layout_dependency.py", "value = 1\n"),
|
|
("scripts/local_dependency.py", "value = 1\n"),
|
|
(
|
|
"scripts/script.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
from layout_dependency import value as layout_value
|
|
from local_dependency import value as local_value
|
|
from shared import value
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[import-not-found]: Cannot resolve imported module `layout_dependency`
|
|
--> scripts/script.py:6:6
|
|
|
|
|
6 | from layout_dependency import value as layout_value
|
|
| ^^^^^^^^^^^^^^^^^
|
|
info: Searched in the following paths during module resolution:
|
|
info: 1. vendored://stdlib (stdlib typeshed stubs vendored by ty)
|
|
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
|
|
|
|
error[import-not-found]: Cannot resolve imported module `local_dependency`
|
|
--> scripts/script.py:7:6
|
|
|
|
|
7 | from local_dependency import value as local_value
|
|
| ^^^^^^^^^^^^^^^^
|
|
info: Searched in the following paths during module resolution:
|
|
info: 1. vendored://stdlib (stdlib typeshed stubs vendored by ty)
|
|
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
|
|
|
|
error[import-not-found]: Cannot resolve imported module `shared`
|
|
--> scripts/script.py:8:6
|
|
|
|
|
8 | from shared import value
|
|
| ^^^^^^
|
|
info: Searched in the following paths during module resolution:
|
|
info: 1. vendored://stdlib (stdlib typeshed stubs vendored by ty)
|
|
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
|
|
|
|
Found 3 diagnostics
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn configured_source_roots_and_extra_paths_are_relative_to_the_script() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
("scripts/source/first_party.py", "value = 1\n"),
|
|
("scripts/extra/dependency.py", "value = 1\n"),
|
|
(
|
|
"scripts/script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.environment]
|
|
# root = ["./source"]
|
|
# extra-paths = ["./extra"]
|
|
# ///
|
|
|
|
from dependency import value as dependency
|
|
from first_party import value as first_party
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn scripts_use_their_own_roots() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"ty.toml",
|
|
r#"
|
|
[environment]
|
|
root = ["./project-source"]
|
|
extra-paths = ["./project-extra"]
|
|
"#,
|
|
),
|
|
("project-source/project_only.py", "value = 1\n"),
|
|
("project-extra/extra_only.py", "value = 1\n"),
|
|
(
|
|
"ordinary.py",
|
|
"
|
|
from extra_only import value as extra
|
|
from project_only import value as project
|
|
",
|
|
),
|
|
(
|
|
"scripts/script.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
from extra_only import value as extra
|
|
from project_only import value as project
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().args(["--output-format", "concise"]), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
scripts/script.py:7:6: error[import-not-found] Cannot resolve imported module `project_only`
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
assert_cmd_snapshot!(case.command().args(["--config-file", "ty.toml"]), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn shared_imports_use_each_scripts_platform() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"shared.py",
|
|
r#"
|
|
import sys
|
|
|
|
if sys.platform == "win32":
|
|
value = "windows"
|
|
else:
|
|
value = "other"
|
|
"#,
|
|
),
|
|
(
|
|
"windows.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.environment]
|
|
# extra-paths = ["."]
|
|
# python-platform = "win32"
|
|
# ///
|
|
|
|
from shared import value
|
|
from typing import reveal_type
|
|
|
|
reveal_type(value)
|
|
"#,
|
|
),
|
|
(
|
|
"linux.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.environment]
|
|
# extra-paths = ["."]
|
|
# python-platform = "linux"
|
|
# ///
|
|
|
|
from shared import value
|
|
from typing import reveal_type
|
|
|
|
reveal_type(value)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> linux.py:11:13
|
|
|
|
|
11 | reveal_type(value)
|
|
| ^^^^^ `Literal["other"]`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> windows.py:11:13
|
|
|
|
|
11 | reveal_type(value)
|
|
| ^^^^^ `Literal["windows"]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn user_configuration_paths_are_relative_to_the_active_configuration_root() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
("user-extra/project_dependency.py", "value = 1\n"),
|
|
("scripts/user-extra/user_dependency.py", "value = 1\n"),
|
|
("cli-extra/cli_dependency.py", "value = 1\n"),
|
|
(
|
|
"scripts/inline.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty]
|
|
# ///
|
|
|
|
from user_dependency import value as user_value
|
|
from cli_dependency import value as cli_value
|
|
"#,
|
|
),
|
|
(
|
|
"scripts/inherit.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
from project_dependency import value as project_value
|
|
from cli_dependency import value as cli_value
|
|
"#,
|
|
),
|
|
])?;
|
|
case.write_file(
|
|
case.user_config_directory().join("ty/ty.toml"),
|
|
r#"
|
|
[environment]
|
|
extra-paths = ["./user-extra"]
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--extra-search-path").arg("./cli-extra"), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn scripts_do_not_use_an_inactive_project_environment() -> anyhow::Result<()> {
|
|
let dependency = if cfg!(windows) {
|
|
".venv/Lib/site-packages/project_dependency.py"
|
|
} else {
|
|
".venv/lib/python3.13/site-packages/project_dependency.py"
|
|
};
|
|
|
|
let case = CliTest::with_files([
|
|
(".venv/pyvenv.cfg", "home = ./\nversion = 3.13\n"),
|
|
(dependency, "value = 1\n"),
|
|
(
|
|
"scripts/script.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
from project_dependency import value
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[import-not-found]: Cannot resolve imported module `project_dependency`
|
|
--> scripts/script.py:6:6
|
|
|
|
|
6 | from project_dependency import value
|
|
| ^^^^^^^^^^^^^^^^^^
|
|
info: Searched in the following paths during module resolution:
|
|
info: 1. vendored://stdlib (stdlib typeshed stubs vendored by ty)
|
|
info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn scripts_use_an_activated_virtual_environment() -> anyhow::Result<()> {
|
|
let dependency = if cfg!(windows) {
|
|
".venv/Lib/site-packages/project_dependency.py"
|
|
} else {
|
|
".venv/lib/python3.13/site-packages/project_dependency.py"
|
|
};
|
|
|
|
let case = CliTest::with_files([
|
|
(".venv/pyvenv.cfg", "home = ./\nversion = 3.13\n"),
|
|
(dependency, "value = 1\n"),
|
|
(
|
|
"scripts/script.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# ///
|
|
|
|
from project_dependency import value
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command().env("VIRTUAL_ENV", case.root().join(".venv")), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_toml_reports_configuration_error() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python =
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[invalid-script-metadata]: string values must be quoted, expected literal string
|
|
--> script.py:3:20
|
|
|
|
|
3 | # requires-python =
|
|
| ^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
assert_cmd_snapshot!(case.command().arg("--output-format").arg("concise"), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
script.py:3:20: error[invalid-script-metadata] string values must be quoted, expected literal string
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_metadata_options_report_configuration_error() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.environment]
|
|
# python-version = true
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[invalid-script-metadata]: wanted string or table
|
|
--> script.py:4:20
|
|
|
|
|
4 | # python-version = true
|
|
| ^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_python_requirement_reports_configuration_error() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.environment]
|
|
python-platform = "linux"
|
|
|
|
[tool.ty.rules]
|
|
unresolved-reference = "error"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = "<3.12"
|
|
# [tool.ty.environment]
|
|
# python-platform = "win32"
|
|
# [tool.ty.rules]
|
|
# unresolved-reference = "warn"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.platform)
|
|
print(missing)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[invalid-script-metadata]: value `<3.12` does not contain a lower bound
|
|
--> script.py:3:21
|
|
|
|
|
3 | # requires-python = "<3.12"
|
|
| ^^^^^^^
|
|
info: Add a lower bound to indicate the minimum compatible Python version (e.g., `>=3.13`) or specify a version in `environment.python-version`.
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
assert_cmd_snapshot!(case.command().arg("--output-format").arg("concise"), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
script.py:3:21: error[invalid-script-metadata] value `<3.12` does not contain a lower bound
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
// TODO: Consider warning that the entire `.src` table is unused.
|
|
#[test]
|
|
fn invalid_inline_src_glob_does_not_prevent_checking_script() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.environment]
|
|
python-platform = "linux"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.src]
|
|
# include = ["src/**test/"]
|
|
# [tool.ty.environment]
|
|
# python-platform = "win32"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.platform)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:12:13
|
|
|
|
|
12 | reveal_type(sys.platform)
|
|
| ^^^^^^^^^^^^ `Literal["win32"]`
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_glob_in_script_analysis_override_is_ignored() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [[tool.ty.overrides]]
|
|
# include = ["script.py"]
|
|
# [tool.ty.overrides.analysis]
|
|
# allowed-unresolved-imports = ["foo**"]
|
|
# ///
|
|
|
|
from typing_extensions import reveal_type
|
|
|
|
reveal_type(1)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command().arg("--output-format=concise"), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
script.py:11:13: info[revealed-type] Revealed type: `Literal[1]`
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_glob_in_top_level_script_analysis_is_an_error() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.analysis]
|
|
# allowed-unresolved-imports = ["foo**"]
|
|
# ///
|
|
|
|
from typing_extensions import reveal_type
|
|
|
|
reveal_type(1)
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(case.command().args(["--exit-zero-on-warning", "--output-format=concise"]), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
script.py:4:33: error[invalid-glob] Invalid glob pattern ``**` can only appear as a complete component (e.g., `foo.**` or `**.bar`), not combined with other text like `foo**``
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_script_environment_reports_configuration_error() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[tool.ty.environment]
|
|
python-version = "3.13"
|
|
python-platform = "linux"
|
|
"#,
|
|
),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.environment]
|
|
# python = "./missing-environment"
|
|
# python-version = "3.12"
|
|
# python-platform = "win32"
|
|
# ///
|
|
|
|
import sys
|
|
from typing import reveal_type
|
|
|
|
reveal_type(sys.version_info[:2])
|
|
reveal_type(sys.platform)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[invalid-script-metadata]: Invalid `environment.python` setting in script metadata `<temp_dir>/missing-environment`: does not point to a Python executable or a directory on disk
|
|
--> script.py:4:12
|
|
|
|
|
4 | # python = "./missing-environment"
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_script_search_paths_do_not_blame_python_environment() -> anyhow::Result<()> {
|
|
let dependency = if cfg!(windows) {
|
|
"environment/Lib/site-packages/dependency.py"
|
|
} else {
|
|
"environment/lib/python3.13/site-packages/dependency.py"
|
|
};
|
|
|
|
let case = CliTest::with_files([
|
|
("environment/pyvenv.cfg", "home = ./\nversion = 3.13\n"),
|
|
(dependency, "value = 1\n"),
|
|
(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# [tool.ty.environment]
|
|
# python = "./environment"
|
|
# typeshed = "./missing-typeshed"
|
|
# ///
|
|
|
|
print(missing)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(case.command(), @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[invalid-script-metadata]: Failed to read the custom typeshed versions file '<temp_dir>/missing-typeshed/stdlib/VERSIONS'
|
|
--> script.py:5:14
|
|
|
|
|
5 | # typeshed = "./missing-typeshed"
|
|
| ^^^^^^^^^^^^^^^^^^^^
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
assert_cmd_snapshot!(case.command().arg("--output-format").arg("concise"), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
script.py:5:14: error[invalid-script-metadata] Failed to read the custom typeshed versions file '<temp_dir>/missing-typeshed/stdlib/VERSIONS'
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn unavailable_uv_reports_metadata_error() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# dependencies = []
|
|
# [tool.ty]
|
|
# project-manager.script = "uv"
|
|
# ///
|
|
print(missing)
|
|
"#,
|
|
)?
|
|
.with_filter(
|
|
"program not found",
|
|
"No such file or directory (os error 2)",
|
|
);
|
|
|
|
let mut command = case.command();
|
|
command.arg("script.py").env("PATH", "");
|
|
assert_cmd_snapshot!(command, @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[project-manager]: Setting up the Python environment with uv failed: could not find `uv` on `PATH`
|
|
--> script.py:2:1
|
|
info: ty uses uv to install this script's dependencies because of your configuration
|
|
--> script.py:5:28
|
|
|
|
|
5 | # project-manager.script = "uv"
|
|
| ^^^^ uv specified here
|
|
info: To install uv, see https://docs.astral.sh/uv/
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
command.env(EnvVars::UV, "missing-uv-executable");
|
|
assert_cmd_snapshot!(command, @r#"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[project-manager]: Setting up the Python environment with uv failed: failed to run `missing-uv-executable`: No such file or directory (os error 2)
|
|
--> script.py:2:1
|
|
info: ty uses uv to install this script's dependencies because of your configuration
|
|
--> script.py:5:28
|
|
|
|
|
5 | # project-manager.script = "uv"
|
|
| ^^^^ uv specified here
|
|
info: The uv executable was specified by the `UV` environment variable
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"#);
|
|
|
|
assert_cmd_snapshot!(command.arg("--output-format=concise"), @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
script.py:2:1: error[project-manager] Setting up the Python environment with uv failed: failed to run `missing-uv-executable`: No such file or directory (os error 2)
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn ordinary_files_do_not_initialize_scripts() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
(
|
|
"script.py",
|
|
"# /// script\n# dependencies = []\n# ///\nvalue = 1\n",
|
|
),
|
|
("ordinary.py", "value = 1\n"),
|
|
])?;
|
|
assert_cmd_snapshot!(
|
|
case.command()
|
|
.arg("ordinary.py")
|
|
.env(EnvVars::TY_UV, "scripts")
|
|
.env(EnvVars::UV, "missing-uv-executable"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn disabled_integration_does_not_initialize_scripts() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
"# /// script\n# dependencies = []\n# ///\nvalue = 1\n",
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(
|
|
case.command()
|
|
.arg("script.py")
|
|
.env(EnvVars::UV, "missing-uv-executable"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn ty_uv_off_overrides_script_and_cli_settings() -> anyhow::Result<()> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r"
|
|
# /// script
|
|
# [tool.ty]
|
|
# project-manager.script = 'uv'
|
|
# ///
|
|
",
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(
|
|
case.command()
|
|
.arg("script.py")
|
|
.args(["--config", "project-manager.script='uv'"])
|
|
.env(EnvVars::TY_UV, "off")
|
|
.env(EnvVars::UV, "missing-uv-executable"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn excluded_scripts_do_not_initialize_their_environments() -> anyhow::Result<()> {
|
|
let case = CliTest::with_files([
|
|
("ty.toml", "[src]\nexclude-scripts = true\n"),
|
|
(
|
|
"script.py",
|
|
"# /// script\n# dependencies = []\n# ///\nvalue = 1\n",
|
|
),
|
|
("ordinary.py", "value = 1\n"),
|
|
])?;
|
|
assert_cmd_snapshot!(
|
|
case.command()
|
|
.arg(".")
|
|
.args(["--config-file", "ty.toml"])
|
|
.env(EnvVars::TY_UV, "scripts")
|
|
.env(EnvVars::UV, "missing-uv-executable"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(feature = "test-uv")]
|
|
mod uv_metadata {
|
|
use std::{fs, process::Command};
|
|
|
|
use insta_cmd::assert_cmd_snapshot;
|
|
use ty_project::uv_test_env_vars;
|
|
use ty_static::EnvVars;
|
|
|
|
use crate::CliTest;
|
|
use crate::uv_workspace::{uv_sync_command, write_dependency_wheel};
|
|
|
|
fn command_with_script_uv(case: &CliTest) -> Command {
|
|
let mut command = case.command();
|
|
command
|
|
.envs(uv_test_env_vars())
|
|
.env(EnvVars::TY_UV, "1")
|
|
.env(EnvVars::UV, "uv")
|
|
.env("UV_CACHE_DIR", case.root().join("cache"));
|
|
command
|
|
}
|
|
|
|
fn assert_uv_supports_script_metadata() -> anyhow::Result<()> {
|
|
let output = Command::new("uv")
|
|
.env_clear()
|
|
.envs(uv_test_env_vars())
|
|
.args(["workspace", "metadata", "--help"])
|
|
.output()?;
|
|
|
|
assert!(
|
|
output.status.success() && String::from_utf8_lossy(&output.stdout).contains("--script"),
|
|
"installed uv does not support script metadata"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn uses_uv_script_environment_and_python_version() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.12"
|
|
# dependencies = ["attrs==25.4.0"]
|
|
# [tool.ty.environment]
|
|
# python-version = "3.11"
|
|
# ///
|
|
|
|
import sys
|
|
from attrs import define
|
|
from typing import reveal_type
|
|
|
|
@define
|
|
class User:
|
|
value: int
|
|
|
|
reveal_type(User(1).value)
|
|
reveal_type(sys.version_info[:2])
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(command_with_script_uv(&case).arg("script.py"), @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:17:13
|
|
|
|
|
17 | reveal_type(User(1).value)
|
|
| ^^^^^^^^^^^^^ `int`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:18:13
|
|
|
|
|
18 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[11]]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.arg("script.py")
|
|
.args(["--python-version", "3.12"]),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:17:13
|
|
|
|
|
17 | reveal_type(User(1).value)
|
|
| ^^^^^^^^^^^^^ `int`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> script.py:18:13
|
|
|
|
|
18 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[12]]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn script_with_indirect_dependency() -> anyhow::Result<CliTest> {
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.8"
|
|
# dependencies = ["direct-dependency"]
|
|
# [tool.uv]
|
|
# no-index = true
|
|
# find-links = ["wheels"]
|
|
# ///
|
|
|
|
import direct_module
|
|
from indirect_module import value
|
|
import indirect_module
|
|
"#,
|
|
)?;
|
|
write_dependency_wheel(&case, "indirect-dependency", "indirect_module", &[])?;
|
|
write_dependency_wheel(
|
|
&case,
|
|
"direct-dependency",
|
|
"direct_module",
|
|
&["indirect-dependency"],
|
|
)?;
|
|
Ok(case)
|
|
}
|
|
|
|
#[test]
|
|
fn indirect_dependencies_use_script_declarations() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = script_with_indirect_dependency()?;
|
|
let mut command = command_with_script_uv(&case);
|
|
command
|
|
.arg("script.py")
|
|
.env("UV_OFFLINE", "1")
|
|
.env("UV_PYTHON_DOWNLOADS", "never");
|
|
|
|
assert_cmd_snapshot!(command, @"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
command.args(["--error", "missing-direct-dependency"]);
|
|
assert_cmd_snapshot!(command, @"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[missing-direct-dependency]: Import of `indirect_module` requires a direct dependency on `indirect-dependency`
|
|
--> script.py:11:6
|
|
|
|
|
11 | from indirect_module import value
|
|
| ^^^^^^^^^^^^^^^
|
|
help: Declare `indirect-dependency` in the script's inline `dependencies` metadata
|
|
info: See https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies
|
|
|
|
error[missing-direct-dependency]: Import of `indirect_module` requires a direct dependency on `indirect-dependency`
|
|
--> script.py:12:8
|
|
|
|
|
12 | import indirect_module
|
|
| ^^^^^^^^^^^^^^^
|
|
help: Declare `indirect-dependency` in the script's inline `dependencies` metadata
|
|
info: See https://docs.astral.sh/uv/guides/scripts/#declaring-script-dependencies
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
");
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn workspace_dependencies_do_not_apply_to_scripts() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = script_with_indirect_dependency()?;
|
|
case.write_files([
|
|
(
|
|
"pyproject.toml",
|
|
r#"
|
|
[project]
|
|
name = "project"
|
|
version = "0.1.0"
|
|
requires-python = ">=3.8"
|
|
dependencies = ["indirect-dependency"]
|
|
|
|
[tool.uv]
|
|
no-index = true
|
|
find-links = ["wheels"]
|
|
"#,
|
|
),
|
|
(
|
|
"ordinary.py",
|
|
r#"
|
|
import indirect_module
|
|
from typing_extensions import reveal_type
|
|
|
|
reveal_type(indirect_module.value)
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
assert_cmd_snapshot!(
|
|
uv_sync_command(&case, None)?
|
|
.args(["--error", "missing-direct-dependency"]),
|
|
@"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
ordinary.py:5:13: info[revealed-type] Revealed type: `int`
|
|
script.py:11:6: error[missing-direct-dependency] Import of `indirect_module` requires a direct dependency on `indirect-dependency`
|
|
script.py:12:8: error[missing-direct-dependency] Import of `indirect_module` requires a direct dependency on `indirect-dependency`
|
|
Found 3 diagnostics
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn imported_script_environment() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = CliTest::with_files([
|
|
("a.py", "from b import foo\nprint(foo)\n"),
|
|
(
|
|
"b.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = "==3.12.*"
|
|
# dependencies = []
|
|
# [tool.ty.environment]
|
|
# python-version = "3.11"
|
|
# ///
|
|
import sys
|
|
from typing import Literal, assert_type
|
|
|
|
foo = 1
|
|
assert_type(sys.version_info[:2], tuple[Literal[3], Literal[11]])
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
// Same as for regular projects. An explicit `python-version` takes precedence
|
|
// over a `requires-python` constraint.
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.args(["a.py", "b.py"])
|
|
.env(EnvVars::TY_UV, "scripts")
|
|
.env(EnvVars::TY_MAX_PARALLELISM, "1"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn synchronizes_imported_script_with_one_worker() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = CliTest::with_files([
|
|
("a.py", "from b import foo\nprint(foo)\n"),
|
|
(
|
|
"b.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = "==3.12.*"
|
|
# dependencies = []
|
|
# ///
|
|
foo = 1
|
|
"#,
|
|
),
|
|
])?;
|
|
|
|
// Starting with the script must not run its importer on the same Rayon worker while
|
|
// initialization is waiting for uv. The importer would wait for that initialization.
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.args(["b.py", "a.py"])
|
|
.env(EnvVars::TY_UV, "scripts")
|
|
.env(EnvVars::TY_MAX_PARALLELISM, "1"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn synchronizes_multiple_scripts_with_one_worker() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let script = r#"
|
|
# /// script
|
|
# requires-python = ">=3.12"
|
|
# dependencies = ["attrs==25.4.0"]
|
|
# ///
|
|
from attrs import define
|
|
"#;
|
|
let case = CliTest::with_files([("first.py", script), ("second.py", script)])?;
|
|
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.args(["first.py", "second.py"])
|
|
.env(EnvVars::TY_UV, "scripts")
|
|
.env(EnvVars::TY_MAX_PARALLELISM, "1"),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
All checks passed!
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn cli_python_selects_script_interpreter_without_replacing_its_environment()
|
|
-> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = CliTest::with_file(
|
|
"scripts/script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.11"
|
|
# dependencies = ["attrs==25.4.0"]
|
|
# ///
|
|
|
|
import sys
|
|
from attrs import define
|
|
from typing import reveal_type
|
|
|
|
@define
|
|
class User:
|
|
value: int
|
|
|
|
reveal_type(User(1).value)
|
|
reveal_type(sys.version_info[:2])
|
|
"#,
|
|
)?;
|
|
|
|
// The CLI environment selects uv's interpreter, but the script's dependencies must still
|
|
// come from the separate environment that uv creates for the script.
|
|
// As for projects, `requires-python` takes precedence over the virtual environment because
|
|
// it defines the minimum Python version the script must support. This allows ty to detect
|
|
// accidental use of newer language features.
|
|
let environment = case.root().join(".venv");
|
|
let output = Command::new("uv")
|
|
.env_clear()
|
|
.envs(uv_test_env_vars())
|
|
.args(["venv", "--no-project", "--python", "3.12"])
|
|
.arg(&environment)
|
|
.env("UV_CACHE_DIR", case.root().join("cache"))
|
|
.output()?;
|
|
anyhow::ensure!(
|
|
output.status.success(),
|
|
"failed to create project environment: {}",
|
|
String::from_utf8_lossy(&output.stderr)
|
|
);
|
|
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.arg("scripts/script.py")
|
|
.args(["--python", ".venv"]),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
info[revealed-type]: Revealed type
|
|
--> scripts/script.py:15:13
|
|
|
|
|
15 | reveal_type(User(1).value)
|
|
| ^^^^^^^^^^^^^ `int`
|
|
|
|
info[revealed-type]: Revealed type
|
|
--> scripts/script.py:16:13
|
|
|
|
|
16 | reveal_type(sys.version_info[:2])
|
|
| ^^^^^^^^^^^^^^^^^^^^ `tuple[Literal[3], Literal[11]]`
|
|
|
|
Found 2 diagnostics
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn fixes_script_using_uv_environment() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
r#"
|
|
# /// script
|
|
# requires-python = ">=3.12"
|
|
# dependencies = ["attrs==25.4.0"]
|
|
# ///
|
|
|
|
from attrs import define
|
|
|
|
@define
|
|
class User:
|
|
value: int
|
|
|
|
User(1) # ty: ignore[unresolved-reference]
|
|
"#,
|
|
)?;
|
|
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.arg("script.py")
|
|
.arg("--fix")
|
|
.args(["--warn", "unused-ignore-comment"]),
|
|
@"
|
|
success: true
|
|
exit_code: 0
|
|
----- stdout -----
|
|
Found 1 diagnostic (1 fixed, 0 remaining).
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
let updated = fs::read_to_string(case.root().join("script.py"))?;
|
|
assert!(!updated.contains("ty: ignore"));
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn failed_uv_script_synchronization_reports_an_error() -> anyhow::Result<()> {
|
|
assert_uv_supports_script_metadata()?;
|
|
|
|
let case = CliTest::with_file(
|
|
"script.py",
|
|
"
|
|
# /// script
|
|
# requires-python = '>=3.8'
|
|
# dependencies = ['missing-script-dependency==99.0.0']
|
|
# ///
|
|
print(missing)
|
|
",
|
|
)?;
|
|
assert_cmd_snapshot!(
|
|
command_with_script_uv(&case)
|
|
.arg("script.py")
|
|
.env("UV_OFFLINE", "1"),
|
|
@"
|
|
success: false
|
|
exit_code: 1
|
|
----- stdout -----
|
|
error[project-manager]: Setting up the Python environment with uv failed
|
|
--> script.py:2:1
|
|
error: No solution found when resolving dependencies
|
|
cause: Because missing-script-dependency was not found in the cache and you require missing-script-dependency==99.0.0, we can conclude that your requirements are unsatisfiable.
|
|
|
|
hint: Packages were unavailable because the network was disabled. When the network is disabled, registry packages may only be read from the cache.
|
|
error: `uv workspace metadata` exited with status 1
|
|
|
|
Found 1 diagnostic
|
|
|
|
----- stderr -----
|
|
"
|
|
);
|
|
|
|
Ok(())
|
|
}
|
|
}
|