[ty] Include python folder in environment.root if it exists (#20263)

## Summary

I felt it was safer to add the `python` folder *in addition* to a
possibly-existing `src` folder, even though the `src` folder only
contains Rust code for `maturin`-based projects. There might be
non-maturin projects where a `python` folder exists for other reasons,
next to a normal `src` layout.

closes https://github.com/astral-sh/ty/issues/1120

## Test Plan

Tested locally on the egglog-python project.
This commit is contained in:
David Peter 2025-09-05 13:53:48 +02:00 committed by GitHub
parent 8ade6c4eaf
commit 7ee863b6d7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 144 additions and 3 deletions

View file

@ -1752,3 +1752,126 @@ fn default_root_tests_package() -> anyhow::Result<()> {
Ok(())
}
#[test]
fn default_root_python_folder() -> anyhow::Result<()> {
let case = CliTest::with_files([
("src/foo.py", "foo = 10"),
("python/bar.py", "bar = 20"),
(
"python/test_bar.py",
r#"
from foo import foo
from bar import bar
print(f"{foo} {bar}")
"#,
),
])?;
assert_cmd_snapshot!(case.command(), @r"
success: true
exit_code: 0
----- stdout -----
All checks passed!
----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
");
Ok(())
}
/// If `python/__init__.py` is present, it is considered a package and `python` is not added to search paths.
#[test]
fn default_root_python_package() -> anyhow::Result<()> {
let case = CliTest::with_files([
("src/foo.py", "foo = 10"),
("python/__init__.py", ""),
("python/bar.py", "bar = 20"),
(
"python/test_bar.py",
r#"
from foo import foo
from bar import bar # expected unresolved import
print(f"{foo} {bar}")
"#,
),
])?;
assert_cmd_snapshot!(case.command(), @r#"
success: false
exit_code: 1
----- stdout -----
error[unresolved-import]: Cannot resolve imported module `bar`
--> python/test_bar.py:3:6
|
2 | from foo import foo
3 | from bar import bar # expected unresolved import
| ^^^
4 |
5 | print(f"{foo} {bar}")
|
info: Searched in the following paths during module resolution:
info: 1. <temp_dir>/ (first-party code)
info: 2. <temp_dir>/src (first-party code)
info: 3. 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
info: rule `unresolved-import` is enabled by default
Found 1 diagnostic
----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
"#);
Ok(())
}
/// Similarly, if `python/__init__.pyi` is present, it is considered a package and `python` is not added to search paths.
#[test]
fn default_root_python_package_pyi() -> anyhow::Result<()> {
let case = CliTest::with_files([
("src/foo.py", "foo = 10"),
("python/__init__.pyi", ""),
("python/bar.py", "bar = 20"),
(
"python/test_bar.py",
r#"
from foo import foo
from bar import bar # expected unresolved import
print(f"{foo} {bar}")
"#,
),
])?;
assert_cmd_snapshot!(case.command(), @r#"
success: false
exit_code: 1
----- stdout -----
error[unresolved-import]: Cannot resolve imported module `bar`
--> python/test_bar.py:3:6
|
2 | from foo import foo
3 | from bar import bar # expected unresolved import
| ^^^
4 |
5 | print(f"{foo} {bar}")
|
info: Searched in the following paths during module resolution:
info: 1. <temp_dir>/ (first-party code)
info: 2. <temp_dir>/src (first-party code)
info: 3. 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
info: rule `unresolved-import` is enabled by default
Found 1 diagnostic
----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
"#);
Ok(())
}