use insta_cmd::assert_cmd_snapshot; use ruff_python_ast::PythonVersion; use crate::CliTest; /// Specifying an option on the CLI should take precedence over the same setting in the /// project's configuration. Here, this is tested for the Python version. #[test] fn config_override_python_version() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python-version = "3.11" "#, ), ( "test.py", r#" import sys # Access `sys.last_exc` that was only added in Python 3.12 print(sys.last_exc) "#, ), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 1 ----- stdout ----- error[missing-attribute]: Module `sys` has no member `last_exc` --> test.py:5:7 | 5 | print(sys.last_exc) | ^^^^^^^^^^^^ info: The member may be available on other Python versions or platforms info: Python 3.11 was assumed when resolving the `last_exc` attribute --> pyproject.toml:3:18 | 3 | python-version = "3.11" | ^^^^^^ Python version configuration Found 1 diagnostic ----- stderr ----- "#); assert_cmd_snapshot!(case.command().arg("--python-version").arg("3.12"), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } /// Same as above, but for the Python platform. #[test] fn config_override_python_platform() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python-platform = "linux" "#, ), ( "test.py", r#" import sys from typing_extensions import reveal_type reveal_type(sys.platform) "#, ), ])?; assert_cmd_snapshot!(case.command(), @r#" success: true exit_code: 0 ----- stdout ----- info[revealed-type]: Revealed type --> test.py:5:13 | 5 | reveal_type(sys.platform) | ^^^^^^^^^^^^ `Literal["linux"]` Found 1 diagnostic ----- stderr ----- "#); assert_cmd_snapshot!(case.command().arg("--python-platform").arg("all"), @" success: true exit_code: 0 ----- stdout ----- info[revealed-type]: Revealed type --> test.py:5:13 | 5 | reveal_type(sys.platform) | ^^^^^^^^^^^^ `LiteralString` Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn config_file_annotation_showing_where_python_version_set_typing_error() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python-version = "3.12" "#, ), ( "test.py", r#" PythonFinalizationError "#, ), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 1 ----- stdout ----- error[unresolved-reference]: Name `PythonFinalizationError` used when not defined --> test.py:2:1 | 2 | PythonFinalizationError | ^^^^^^^^^^^^^^^^^^^^^^^ info: `PythonFinalizationError` was added as a builtin in Python 3.13 info: Python 3.12 was assumed when resolving types --> pyproject.toml:3:18 | 3 | python-version = "3.12" | ^^^^^^ Python version configuration Found 1 diagnostic ----- stderr ----- "#); assert_cmd_snapshot!(case.command().arg("--python-version=3.12"), @" success: false exit_code: 1 ----- stdout ----- error[unresolved-reference]: Name `PythonFinalizationError` used when not defined --> test.py:2:1 | 2 | PythonFinalizationError | ^^^^^^^^^^^^^^^^^^^^^^^ info: `PythonFinalizationError` was added as a builtin in Python 3.13 info: Python 3.12 was assumed when resolving types because it was specified on the command line Found 1 diagnostic ----- stderr ----- "); Ok(()) } /// If `.` and `./src` are both registered as first-party search paths, /// the `./src` directory should take precedence for module resolution, /// because it is relative to `.`. #[test] fn src_subdirectory_takes_precedence_over_repo_root() -> anyhow::Result<()> { let case = CliTest::with_files([( "src/package/__init__.py", "from . import nonexistent_submodule", )])?; // If `./src` didn't take priority over `.` here, we would report // "Module `src.package` has no member `nonexistent_submodule`" // instead of "Module `package` has no member `nonexistent_submodule`". assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package` has no member `nonexistent_submodule` --> src/package/__init__.py:1:15 | 1 | from . import nonexistent_submodule | ^^^^^^^^^^^^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); Ok(()) } /// If there is an `src/__init__.py(i)` file, don't add `./src` as a root. /// This is an antipattern (you're meant to have a top-level package directory /// *inside* `src/`), but it's a mistake that a surprising number of people make, /// and the diagnostic issued by ty is confusing if imports don't work. #[test] fn src_subdirectory_not_added_as_root_if_src_package_exists() -> anyhow::Result<()> { let case = CliTest::with_files([ ("src/__init__.py", ""), ("src/box.py", "class Box: ..."), ("src/test_box.py", "from .box import Box"), ])?; // The import is only resolvable because `src` was *not* added as a root. assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } /// This tests that, even if no Python *version* has been specified on the CLI or in a config file, /// ty is still able to infer the Python version from a `--python` argument on the CLI, /// *even if* the `--python` argument points to a system installation. /// /// We currently cannot infer the Python version from a system installation on Windows: /// on Windows, we can only infer the Python version from a virtual environment. /// This is because we use the layout of the Python installation to infer the Python version: /// on Unix, the `site-packages` directory of an installation will be located at /// `/lib/pythonX.Y/site-packages`. On Windows, however, the `site-packages` /// directory will be located at `/Lib/site-packages`, which doesn't give us the /// same information. #[cfg(not(windows))] #[test] fn python_version_inferred_from_system_installation() -> anyhow::Result<()> { let cpython_case = CliTest::with_files([ ("pythons/Python3.12/bin/python", ""), ("pythons/Python3.12/lib/python3.12/site-packages/foo.py", ""), ("test.py", "PythonFinalizationError"), ])?; assert_cmd_snapshot!(cpython_case.command().arg("--python").arg("pythons/Python3.12/bin/python"), @" success: false exit_code: 1 ----- stdout ----- error[unresolved-reference]: Name `PythonFinalizationError` used when not defined --> test.py:1:1 | 1 | PythonFinalizationError | ^^^^^^^^^^^^^^^^^^^^^^^ info: `PythonFinalizationError` was added as a builtin in Python 3.13 info: Python 3.12 was assumed when resolving types because of the layout of your Python installation info: The primary `site-packages` directory of your installation was found at `lib/python3.12/site-packages/` info: No Python version was specified on the command line or in a configuration file Found 1 diagnostic ----- stderr ----- "); let pypy_case = CliTest::with_files([ ("pythons/pypy3.12/bin/python", ""), ("pythons/pypy3.12/lib/pypy3.12/site-packages/foo.py", ""), ("test.py", "PythonFinalizationError"), ])?; assert_cmd_snapshot!(pypy_case.command().arg("--python").arg("pythons/pypy3.12/bin/python"), @" success: false exit_code: 1 ----- stdout ----- error[unresolved-reference]: Name `PythonFinalizationError` used when not defined --> test.py:1:1 | 1 | PythonFinalizationError | ^^^^^^^^^^^^^^^^^^^^^^^ info: `PythonFinalizationError` was added as a builtin in Python 3.13 info: Python 3.12 was assumed when resolving types because of the layout of your Python installation info: The primary `site-packages` directory of your installation was found at `lib/pypy3.12/site-packages/` info: No Python version was specified on the command line or in a configuration file Found 1 diagnostic ----- stderr ----- "); let free_threaded_case = CliTest::with_files([ ("pythons/Python3.13t/bin/python", ""), ( "pythons/Python3.13t/lib/python3.13t/site-packages/foo.py", "", ), ("test.py", "import string.templatelib"), ])?; assert_cmd_snapshot!(free_threaded_case.command().arg("--python").arg("pythons/Python3.13t/bin/python"), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `string.templatelib` --> test.py:1:8 | 1 | import string.templatelib | ^^^^^^^^^^^^^^^^^^ info: The stdlib module `string.templatelib` is only available on Python 3.14+ info: Python 3.13 was assumed when resolving modules because of the layout of your Python installation info: The primary `site-packages` directory of your installation was found at `lib/python3.13t/site-packages/` info: No Python version was specified on the command line or in a configuration file Found 1 diagnostic ----- stderr ----- "); Ok(()) } /// This attempts to simulate the tangled web of symlinks that a homebrew install has /// which can easily confuse us if we're ever told to use it. /// /// The main thing this is regression-testing is a panic in one *extremely* specific case /// that you have to try really hard to hit (but vscode, hilariously, did hit). #[cfg(unix)] #[test] fn python_argument_trapped_in_a_symlink_factory() -> anyhow::Result<()> { let case = CliTest::with_files([ // This is the real python binary. ( "opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/bin/python3.13", "", ), // There's a real site-packages here (although it's basically empty). ( "opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages/foo.py", "", ), // There's also a real site-packages here (although it's basically empty). ("opt/homebrew/lib/python3.13/site-packages/bar.py", ""), // This has the real stdlib, but the site-packages in this dir is a symlink. ( "opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/abc.py", "", ), // It's important that this our faux-homebrew not be in the same dir as our working directory // to reproduce the crash, don't ask me why. ( "project/test.py", "\ import foo import bar import colorama ", ), ])?; // many python symlinks pointing to a single real python (the longest path) case.write_symlink( "opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/bin/python3.13", "opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/bin/python3", )?; case.write_symlink( "opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/bin/python3", "opt/homebrew/Cellar/python@3.13/3.13.5/bin/python3", )?; case.write_symlink( "opt/homebrew/Cellar/python@3.13/3.13.5/bin/python3", "opt/homebrew/bin/python3", )?; // the "real" python's site-packages is a symlink to a different dir case.write_symlink( "opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages", "opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/lib/python3.13/site-packages", )?; // Try all 4 pythons with absolute paths to our fauxbrew install assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .arg("--python").arg(case.root().join("opt/homebrew/bin/python3")), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `foo` --> test.py:1:8 | 1 | import foo | ^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/lib/python3.13/site-packages (site-packages) 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 `colorama` --> test.py:3:8 | 3 | import colorama | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/lib/python3.13/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 2 diagnostics ----- stderr ----- "); assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .arg("--python").arg(case.root().join("opt/homebrew/Cellar/python@3.13/3.13.5/bin/python3")), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `bar` --> test.py:2:8 | 2 | import bar | ^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages (site-packages) 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 `colorama` --> test.py:3:8 | 3 | import colorama | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 2 diagnostics ----- stderr ----- "); assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .arg("--python").arg(case.root().join("opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/bin/python3")), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `bar` --> test.py:2:8 | 2 | import bar | ^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages (site-packages) 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 `colorama` --> test.py:3:8 | 3 | import colorama | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 2 diagnostics ----- stderr ----- "); assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .arg("--python").arg(case.root().join("opt/homebrew/Cellar/python@3.13/3.13.5/Frameworks/Python.framework/Versions/3.13/bin/python3.13")), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `bar` --> test.py:2:8 | 2 | import bar | ^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages (site-packages) 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 `colorama` --> test.py:3:8 | 3 | import colorama | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /opt/homebrew/Cellar/python@3.13/3.13.5/lib/python3.13/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 2 diagnostics ----- stderr ----- "); Ok(()) } /// On Unix systems, it's common for a Python installation at `.venv/bin/python` to only be a symlink /// to a system Python installation. We must be careful not to resolve the symlink too soon! /// If we do, we will incorrectly add the system installation's `site-packages` as a search path, /// when we should be adding the virtual environment's `site-packages` directory as a search path instead. #[cfg(unix)] #[test] fn python_argument_points_to_symlinked_executable() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "system-installation/lib/python3.13/site-packages/foo.py", "", ), ("system-installation/bin/python", ""), ( "strange-venv-location/lib/python3.13/site-packages/bar.py", "", ), ( "test.py", "\ import foo import bar", ), ])?; case.write_symlink( "system-installation/bin/python", "strange-venv-location/bin/python", )?; assert_cmd_snapshot!(case.command().arg("--python").arg("strange-venv-location/bin/python"), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `foo` --> test.py:1:8 | 1 | import foo | ^^^ info: Searched in the following paths during module resolution: info: 1. / (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /strange-venv-location/lib/python3.13/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 1 diagnostic ----- stderr ----- "); Ok(()) } /// On Unix systems, a virtual environment can come with multiple `site-packages` directories: /// one at `/lib/pythonX.Y/site-packages` and one at /// `/lib64/pythonX.Y/site-packages`. According to [the stdlib docs], the `lib64` /// is not *meant* to have any Python files in it (only C extensions and similar). Empirically, /// however, it sometimes does indeed have Python files in it: popular tools such as poetry /// appear to sometimes install Python packages into the `lib64` site-packages directory even /// though they probably shouldn't. We therefore check for both a `lib64` and a `lib` directory, /// and add them both as search paths if they both exist. /// /// See: /// - /// - . /// /// [the stdlib docs]: https://docs.python.org/3/library/sys.html#sys.platlibdir #[cfg(unix)] #[test] fn lib64_site_packages_directory_on_unix() -> anyhow::Result<()> { let case = CliTest::with_files([ (".venv/lib/python3.13/site-packages/foo.py", ""), (".venv/lib64/python3.13/site-packages/bar.py", ""), ("test.py", "import foo, bar, baz"), ])?; assert_cmd_snapshot!(case.command().arg("--python").arg(".venv"), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `baz` --> test.py:1:18 | 1 | import foo, bar, baz | ^^^ info: Searched in the following paths during module resolution: info: 1. / (first-party code) info: 2. vendored://stdlib (stdlib typeshed stubs vendored by ty) info: 3. /.venv/lib/python3.13/site-packages (site-packages) info: 4. /.venv/lib64/python3.13/site-packages (site-packages) info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn many_search_paths() -> anyhow::Result<()> { let case = CliTest::with_files([ ("extra1/foo1.py", ""), ("extra2/foo2.py", ""), ("extra3/foo3.py", ""), ("extra4/foo4.py", ""), ("extra5/foo5.py", ""), ("extra6/foo6.py", ""), ("test.py", "import foo1, baz"), ])?; assert_cmd_snapshot!( case.command() .arg("--python-platform").arg("linux") .arg("--extra-search-path").arg("extra1") .arg("--extra-search-path").arg("extra2") .arg("--extra-search-path").arg("extra3") .arg("--extra-search-path").arg("extra4"), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `baz` --> test.py:1:14 | 1 | import foo1, baz | ^^^ info: Searched in the following paths during module resolution: info: 1. /extra1 (extra search path specified on the CLI or in your config file) info: 2. /extra2 (extra search path specified on the CLI or in your config file) info: 3. /extra3 (extra search path specified on the CLI or in your config file) info: 4. /extra4 (extra search path specified on the CLI or in your config file) info: 5. / (first-party code) info: 6. 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 ----- "); assert_cmd_snapshot!( case.command() .arg("--python-platform").arg("linux") .arg("--extra-search-path").arg("extra1") .arg("--extra-search-path").arg("extra2") .arg("--extra-search-path").arg("extra3") .arg("--extra-search-path").arg("extra4") .arg("--extra-search-path").arg("extra5") .arg("--extra-search-path").arg("extra6"), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `baz` --> test.py:1:14 | 1 | import foo1, baz | ^^^ info: Searched in the following paths during module resolution: info: 1. /extra1 (extra search path specified on the CLI or in your config file) info: 2. /extra2 (extra search path specified on the CLI or in your config file) info: 3. /extra3 (extra search path specified on the CLI or in your config file) info: 4. /extra4 (extra search path specified on the CLI or in your config file) info: 5. /extra5 (extra search path specified on the CLI or in your config file) info: ... and 3 more paths. Run with `-v` to see all paths. info: make sure your Python environment is properly configured: https://docs.astral.sh/ty/modules/#python-environment Found 1 diagnostic ----- stderr ----- "); // Shows all with `-v` assert_cmd_snapshot!( case.command() .arg("--python-platform").arg("linux") .arg("--extra-search-path").arg("extra1") .arg("--extra-search-path").arg("extra2") .arg("--extra-search-path").arg("extra3") .arg("--extra-search-path").arg("extra4") .arg("--extra-search-path").arg("extra5") .arg("--extra-search-path").arg("extra6") .arg("-v"), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `baz` --> test.py:1:14 | 1 | import foo1, baz | ^^^ info: Searched in the following paths during module resolution: info: 1. /extra1 (extra search path specified on the CLI or in your config file) info: 2. /extra2 (extra search path specified on the CLI or in your config file) info: 3. /extra3 (extra search path specified on the CLI or in your config file) info: 4. /extra4 (extra search path specified on the CLI or in your config file) info: 5. /extra5 (extra search path specified on the CLI or in your config file) info: 6. /extra6 (extra search path specified on the CLI or in your config file) info: 7. / (first-party code) info: 8. 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 `import-not-found` is enabled by default Found 1 diagnostic ----- stderr ----- INFO Indexed 7 file(s) in 0.000s "); Ok(()) } #[test] fn pyvenv_cfg_file_annotation_showing_where_python_version_set() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python = "venv" "#, ), ( "venv/pyvenv.cfg", r#" version = 3.12 home = foo/bar/bin "#, ), if cfg!(target_os = "windows") { ("foo/bar/bin/python.exe", "") } else { ("foo/bar/bin/python", "") }, if cfg!(target_os = "windows") { ("venv/Lib/site-packages/foo.py", "") } else { ("venv/lib/python3.12/site-packages/foo.py", "") }, ("test.py", "PythonFinalizationError"), ])?; assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- error[unresolved-reference]: Name `PythonFinalizationError` used when not defined --> test.py:1:1 | 1 | PythonFinalizationError | ^^^^^^^^^^^^^^^^^^^^^^^ info: `PythonFinalizationError` was added as a builtin in Python 3.13 info: Python 3.12 was assumed when resolving types because of your virtual environment --> venv/pyvenv.cfg:2:11 | 2 | version = 3.12 | ^^^^ Virtual environment metadata info: No Python version was specified on the command line or in a configuration file Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn pyvenv_cfg_file_annotation_no_trailing_newline() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python = "venv" "#, ), ( "venv/pyvenv.cfg", r#"home = foo/bar/bin version = 3.12"#, ), if cfg!(target_os = "windows") { ("foo/bar/bin/python.exe", "") } else { ("foo/bar/bin/python", "") }, if cfg!(target_os = "windows") { ("venv/Lib/site-packages/foo.py", "") } else { ("venv/lib/python3.12/site-packages/foo.py", "") }, ("test.py", "PythonFinalizationError"), ])?; assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- error[unresolved-reference]: Name `PythonFinalizationError` used when not defined --> test.py:1:1 | 1 | PythonFinalizationError | ^^^^^^^^^^^^^^^^^^^^^^^ info: `PythonFinalizationError` was added as a builtin in Python 3.13 info: Python 3.12 was assumed when resolving types because of your virtual environment --> venv/pyvenv.cfg:3:23 | 3 | version = 3.12 | ^^^^ Virtual environment metadata info: No Python version was specified on the command line or in a configuration file Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn config_file_annotation_showing_where_python_version_set_syntax_error() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [project] requires-python = ">=3.8" "#, ), ( "test.py", r#" match object(): case int(): pass case _: pass "#, ), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 1 ----- stdout ----- error[invalid-syntax]: Cannot use `match` statement on Python 3.8 (syntax was added in Python 3.10) --> test.py:2:1 | 2 | match object(): | ^^^^^ info: Python 3.8 was assumed when parsing syntax --> pyproject.toml:3:19 | 3 | requires-python = ">=3.8" | ^^^^^^^ Python version configuration Found 1 diagnostic ----- stderr ----- "#); assert_cmd_snapshot!(case.command().arg("--python-version=3.9"), @" success: false exit_code: 1 ----- stdout ----- error[invalid-syntax]: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10) --> test.py:2:1 | 2 | match object(): | ^^^^^ info: Python 3.9 was assumed when parsing syntax because it was specified on the command line Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn python_cli_argument_virtual_environment() -> anyhow::Result<()> { let path_to_executable = if cfg!(windows) { "my-venv/Scripts/python.exe" } else { "my-venv/bin/python" }; let other_venv_path = "my-venv/foo/some_other_file.txt"; let case = CliTest::with_files([ ("test.py", ""), ( if cfg!(windows) { "my-venv/Lib/site-packages/foo.py" } else { "my-venv/lib/python3.13/site-packages/foo.py" }, "", ), (path_to_executable, ""), (other_venv_path, ""), ])?; // Passing a path to the installation works assert_cmd_snapshot!(case.command().arg("--python").arg("my-venv"), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); // And so does passing a path to the executable inside the installation assert_cmd_snapshot!(case.command().arg("--python").arg(path_to_executable), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); // But random other paths inside the installation are rejected assert_cmd_snapshot!(case.command().arg("--python").arg(other_venv_path), @" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: Invalid `--python` argument `/my-venv/foo/some_other_file.txt`: does not point to a Python executable or a directory on disk "); // And so are paths that do not exist on disk assert_cmd_snapshot!(case.command().arg("--python").arg("not-a-directory-or-executable"), @" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: Invalid `--python` argument `/not-a-directory-or-executable`: does not point to a Python executable or a directory on disk Cause: No such file or directory (os error 2) "); Ok(()) } #[test] fn python_cli_argument_system_installation() -> anyhow::Result<()> { let path_to_executable = if cfg!(windows) { "Python3.11/python.exe" } else { "Python3.11/bin/python" }; let case = CliTest::with_files([ ("test.py", ""), ( if cfg!(windows) { "Python3.11/Lib/site-packages/foo.py" } else { "Python3.11/lib/python3.11/site-packages/foo.py" }, "", ), (path_to_executable, ""), ])?; // Passing a path to the installation works assert_cmd_snapshot!(case.command().arg("--python").arg("Python3.11"), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); // And so does passing a path to the executable inside the installation assert_cmd_snapshot!(case.command().arg("--python").arg(path_to_executable), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } #[test] fn config_file_broken_python_setting() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [project] name = "test" version = "0.1.0" description = "Some description" readme = "README.md" requires-python = ">=3.13" dependencies = [] [tool.ty.environment] python = "not-a-directory-or-executable" "#, ), ("test.py", ""), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: Invalid `environment.python` setting --> Invalid setting in configuration file `/pyproject.toml` | 9 | 10 | [tool.ty.environment] 11 | python = "not-a-directory-or-executable" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ does not point to a Python executable or a directory on disk Cause: No such file or directory (os error 2) "#); Ok(()) } #[test] fn config_file_unsupported_python_version() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python-version = "2.7" "#, ), ("test.py", ""), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: /pyproject.toml is not a valid `pyproject.toml` Cause: TOML parse error at line 3, column 18 | 3 | python-version = "2.7" | ^^^^^ unknown variant `2.7`, expected one of `3.7`, `3.8`, `3.9`, `3.10`, `3.11`, `3.12`, `3.13`, `3.14`, `3.15` "#); Ok(()) } #[test] fn cli_unsupported_python_version() -> anyhow::Result<()> { let case = CliTest::with_file("test.py", "")?; assert_cmd_snapshot!(case.command().arg("--python-version=2.7"), @" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- error: invalid value '2.7' for '--python-version ' [possible values: 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15] tip: a similar value exists: '3.7' For more information, try '--help'. "); Ok(()) } #[test] fn config_file_python_setting_directory_with_no_site_packages() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python = "directory-but-no-site-packages" "#, ), ("directory-but-no-site-packages/lib/foo.py", ""), ("test.py", ""), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: Failed to discover the site-packages directory Cause: Invalid `environment.python` setting --> Invalid setting in configuration file `/pyproject.toml` | 1 | 2 | [tool.ty.environment] 3 | python = "directory-but-no-site-packages" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Could not find a `site-packages` directory for this Python installation/executable "#); Ok(()) } #[test] fn config_file_python_setting_directory_with_unsupported_python_version() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python = "venv" "#, ), ( "venv/pyvenv.cfg", r#" version_info = 3.16.0 home = base/bin "#, ), if cfg!(target_os = "windows") { ("base/bin/python.exe", "") } else { ("base/bin/python", "") }, if cfg!(target_os = "windows") { ("venv/Lib/site-packages/foo.py", "") } else { ("venv/lib/python3.16/site-packages/foo.py", "") }, ("test.py", ""), ])?; assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- warning[unsupported-python-version]: Ignoring unsupported inferred Python version `3.16`; ty will use Python 3.15 instead. --> venv/pyvenv.cfg:2:16 | 2 | version_info = 3.16.0 | ^^^^^^ info: Expected one of `3.7`, `3.8`, `3.9`, `3.10`, `3.11`, `3.12`, `3.13`, `3.14`, `3.15`. info: Set `environment.python-version` explicitly to override the inferred version. info: The version was inferred from your virtual environment metadata. Found 1 diagnostic ----- stderr ----- "); Ok(()) } // This error message is never emitted on Windows, because Windows installations have simpler layouts #[cfg(not(windows))] #[test] fn unix_system_installation_with_no_lib_directory() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [tool.ty.environment] python = "directory-but-no-site-packages" "#, ), ("directory-but-no-site-packages/foo.py", ""), ("test.py", ""), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: Failed to discover the site-packages directory Cause: Failed to iterate over the contents of the `lib`/`lib64` directories of the Python installation --> Invalid setting in configuration file `/pyproject.toml` | 1 | 2 | [tool.ty.environment] 3 | python = "directory-but-no-site-packages" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "#); Ok(()) } #[test] fn defaults_to_a_new_python_version() -> anyhow::Result<()> { let case = CliTest::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 from typing import TypeAliasType # added in Python 3.12 "#, ), ])?; assert_cmd_snapshot!(case.command(), @r#" success: false exit_code: 1 ----- stdout ----- error[missing-attribute]: Module `os` has no member `grantpt` --> main.py:4:1 | 4 | os.grantpt(1) # only available on unix, Python 3.13 or newer | ^^^^^^^^^^ info: The member may be available on other Python versions or platforms info: Python 3.11 was assumed when resolving the `grantpt` attribute --> ty.toml:3:18 | 3 | python-version = "3.11" | ^^^^^^ Python version configuration error[missing-module-member]: Module `typing` has no member `TypeAliasType` --> main.py:6:20 | 6 | from typing import TypeAliasType # added in Python 3.12 | ^^^^^^^^^^^^^ info: The member may be available on other Python versions or platforms info: Python 3.11 was assumed when resolving imports --> ty.toml:3:18 | 3 | python-version = "3.11" | ^^^^^^ Python version configuration Found 2 diagnostics ----- stderr ----- "#); // Use default (which should be latest supported) let case = CliTest::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 from typing import LiteralString # added in Python 3.11 "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } fn write_conda_interpreters(case: &CliTest) -> anyhow::Result<()> { let python = if cfg!(windows) { "python.exe" } else { "bin/python" }; for prefix in ["conda", "conda/envs/conda-env"] { case.write_file(format!("{prefix}/{python}"), "")?; } Ok(()) } /// The `site-packages` directory is used by ty for external imports. /// ty does the following checks to discover the `site-packages` directory in the order: /// 1) If the `VIRTUAL_ENV` environment variable is set to a non-empty value /// 2) If `CONDA_PREFIX` points to a child environment (e.g. its directory name matches /// `CONDA_DEFAULT_ENV`) /// 3) If a `.venv` directory exists at the project root /// 4) If `CONDA_PREFIX` points to the base environment (e.g. `_CONDA_ROOT` == `CONDA_PREFIX`) /// /// A Conda environment is considered only if it contains a Python interpreter. /// This test (and the next one) is aiming at validating the logic around these cases. /// /// To do this we create a program that has these 4 imports: /// /// ```python /// from package1 import ActiveVenv /// from package1 import ChildConda /// from package1 import WorkingVenv /// from package1 import BaseConda /// ``` /// /// We then create 4 different copies of package1. Each copy defines all of these /// classes... except the one that describes it. Therefore we know we got e.g. /// the working venv if we get a diagnostic like this: /// /// ```text /// Unresolved import /// 4 | from package1 import WorkingVenv /// | ^^^^^^^^^^^ /// ``` /// /// This test uses a directory structure as follows: /// /// ├── project /// │ ├── test.py /// │ └── .venv /// │ ├── pyvenv.cfg /// │ └── lib /// │ └── python3.13 /// │ └── site-packages /// │ └── package1 /// │ └── __init__.py /// ├── myvenv /// │ ├── pyvenv.cfg /// │ └── lib /// │ └── python3.13 /// │ └── site-packages /// │ └── package1 /// │ └── __init__.py /// └── conda /// ├── bin/python /// ├── lib /// │ └── python3.13 /// │ └── site-packages /// │ └── package1 /// │ └── __init__.py /// └── envs /// └── conda-env /// ├── bin/python /// └── lib /// └── python3.13 /// └── site-packages /// └── package1 /// └── __init__.py /// /// test.py imports package1 /// And the command is run in the `project` directory. #[test] fn check_venv_resolution_with_working_venv() -> anyhow::Result<()> { let child_conda_package1_path = if cfg!(windows) { "conda/envs/conda-env/Lib/site-packages/package1/__init__.py" } else { "conda/envs/conda-env/lib/python3.13/site-packages/package1/__init__.py" }; let base_conda_package1_path = if cfg!(windows) { "conda/Lib/site-packages/package1/__init__.py" } else { "conda//lib/python3.13/site-packages/package1/__init__.py" }; let working_venv_package1_path = if cfg!(windows) { "project/.venv/Lib/site-packages/package1/__init__.py" } else { "project/.venv/lib/python3.13/site-packages/package1/__init__.py" }; let active_venv_package1_path = if cfg!(windows) { "myvenv/Lib/site-packages/package1/__init__.py" } else { "myvenv/lib/python3.13/site-packages/package1/__init__.py" }; let case = CliTest::with_files([ ( "project/test.py", r#" from package1 import ActiveVenv from package1 import ChildConda from package1 import WorkingVenv from package1 import BaseConda "#, ), ( "project/.venv/pyvenv.cfg", r#" home = ./ "#, ), ( "myvenv/pyvenv.cfg", r#" home = ./ "#, ), ( active_venv_package1_path, r#" class ChildConda: ... class WorkingVenv: ... class BaseConda: ... "#, ), ( child_conda_package1_path, r#" class ActiveVenv: ... class WorkingVenv: ... class BaseConda: ... "#, ), ( working_venv_package1_path, r#" class ActiveVenv: ... class ChildConda: ... class BaseConda: ... "#, ), ( base_conda_package1_path, r#" class ActiveVenv: ... class ChildConda: ... class WorkingVenv: ... "#, ), ])?; write_conda_interpreters(&case)?; // Run with nothing set, should find the working venv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `WorkingVenv` --> test.py:4:22 | 4 | from package1 import WorkingVenv | ^^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // An empty VIRTUAL_ENV does not hide the working venv. assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("VIRTUAL_ENV", ""), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `WorkingVenv` --> test.py:4:22 | 4 | from package1 import WorkingVenv | ^^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // Run with VIRTUAL_ENV set, should find the active venv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("VIRTUAL_ENV", case.root().join("myvenv")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ActiveVenv` --> test.py:2:22 | 2 | from package1 import ActiveVenv | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX set, should find the child conda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ChildConda` --> test.py:3:22 | 3 | from package1 import ChildConda | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find working venv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("CONDA_DEFAULT_ENV", "base"), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `WorkingVenv` --> test.py:4:22 | 4 | from package1 import WorkingVenv | ^^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX and CONDA_DEFAULT_ENV (unequal) and VIRTUAL_ENV set, // should find child active venv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("CONDA_DEFAULT_ENV", "base") .env("VIRTUAL_ENV", case.root().join("myvenv")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ActiveVenv` --> test.py:2:22 | 2 | from package1 import ActiveVenv | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_DEFAULT_ENV set to the directory name of CONDA_PREFIX, should find ChildConda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")) .env("CONDA_DEFAULT_ENV", "conda-env"), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ChildConda` --> test.py:3:22 | 3 | from package1 import ChildConda | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with _CONDA_ROOT and CONDA_PREFIX (unequal!) set, should find ChildConda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")) .env("_CONDA_ROOT", case.root().join("conda")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ChildConda` --> test.py:3:22 | 3 | from package1 import ChildConda | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with _CONDA_ROOT and CONDA_PREFIX (equal!) set, should find WorkingVenv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("_CONDA_ROOT", case.root().join("conda")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `WorkingVenv` --> test.py:4:22 | 4 | from package1 import WorkingVenv | ^^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); Ok(()) } /// The exact same test as above, but without a working venv /// /// In this case the Base Conda should be a possible outcome. #[test] fn check_venv_resolution_without_working_venv() -> anyhow::Result<()> { let child_conda_package1_path = if cfg!(windows) { "conda/envs/conda-env/Lib/site-packages/package1/__init__.py" } else { "conda/envs/conda-env/lib/python3.13/site-packages/package1/__init__.py" }; let base_conda_package1_path = if cfg!(windows) { "conda/Lib/site-packages/package1/__init__.py" } else { "conda/lib/python3.13/site-packages/package1/__init__.py" }; let active_venv_package1_path = if cfg!(windows) { "myvenv/Lib/site-packages/package1/__init__.py" } else { "myvenv/lib/python3.13/site-packages/package1/__init__.py" }; let case = CliTest::with_files([ ( "project/test.py", r#" from package1 import ActiveVenv from package1 import ChildConda from package1 import WorkingVenv from package1 import BaseConda "#, ), ( "myvenv/pyvenv.cfg", r#" home = ./ "#, ), ( active_venv_package1_path, r#" class ChildConda: ... class WorkingVenv: ... class BaseConda: ... "#, ), ( child_conda_package1_path, r#" class ActiveVenv: ... class WorkingVenv: ... class BaseConda: ... "#, ), ( base_conda_package1_path, r#" class ActiveVenv: ... class ChildConda: ... class WorkingVenv: ... "#, ), ])?; write_conda_interpreters(&case)?; // Run with nothing set, should fail to find anything assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `package1` --> test.py:2:6 | 2 | from package1 import ActiveVenv | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. 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 `package1` --> test.py:3:6 | 3 | from package1 import ChildConda | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. 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 `package1` --> test.py:4:6 | 4 | from package1 import WorkingVenv | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. 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 `package1` --> test.py:5:6 | 5 | from package1 import BaseConda | ^^^^^^^^ info: Searched in the following paths during module resolution: info: 1. /project (first-party code) info: 2. 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 4 diagnostics ----- stderr ----- "); // Run with VIRTUAL_ENV set, should find the active venv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("VIRTUAL_ENV", case.root().join("myvenv")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ActiveVenv` --> test.py:2:22 | 2 | from package1 import ActiveVenv | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX set, should find the child conda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ChildConda` --> test.py:3:22 | 3 | from package1 import ChildConda | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find base conda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("CONDA_DEFAULT_ENV", "base"), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `BaseConda` --> test.py:5:22 | 5 | from package1 import BaseConda | ^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX and CONDA_DEFAULT_ENV (unequal) and VIRTUAL_ENV set, // should find child active venv assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("CONDA_DEFAULT_ENV", "base") .env("VIRTUAL_ENV", case.root().join("myvenv")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ActiveVenv` --> test.py:2:22 | 2 | from package1 import ActiveVenv | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with CONDA_PREFIX and CONDA_DEFAULT_ENV (unequal!) set, should find base conda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("CONDA_DEFAULT_ENV", "base"), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `BaseConda` --> test.py:5:22 | 5 | from package1 import BaseConda | ^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with _CONDA_ROOT and CONDA_PREFIX (unequal!) set, should find ChildConda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")) .env("_CONDA_ROOT", case.root().join("conda")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `ChildConda` --> test.py:3:22 | 3 | from package1 import ChildConda | ^^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); // run with _CONDA_ROOT and CONDA_PREFIX (equal!) set, should find BaseConda assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("CONDA_PREFIX", case.root().join("conda")) .env("_CONDA_ROOT", case.root().join("conda")), @" success: false exit_code: 1 ----- stdout ----- error[missing-module-member]: Module `package1` has no member `BaseConda` --> test.py:5:22 | 5 | from package1 import BaseConda | ^^^^^^^^^ Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn default_root_src_layout() -> anyhow::Result<()> { let case = CliTest::with_files([ ("src/foo.py", "foo = 10"), ("bar.py", "bar = 20"), ( "src/main.py", r#" from foo import foo from bar import bar print(f"{foo} {bar}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } #[test] fn default_root_project_name_folder() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [project] name = "psycopg" "#, ), ("psycopg/psycopg/foo.py", "foo = 10"), ("bar.py", "bar = 20"), ( "psycopg/psycopg/main.py", r#" from psycopg.foo import foo from bar import bar print(f"{foo} {bar}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } /// This is the "flat layout", but it looks *nearly* like the "src variant" layout. /// If `psycopg/__init__.py` didn't exist, we'd add `psycopg/` as a search path -- /// but because it *does*, we can see that this is clearly meant to be the flat layout. #[test] fn default_root_flat_layout_variant() -> anyhow::Result<()> { let case = CliTest::with_files([ ( "pyproject.toml", r#" [project] name = "psycopg" "#, ), ("psycopg/__init__.py", ""), ("psycopg/box.py", "class Box: ..."), // This import only resolves because of the fact that // `./psycopg` was *not* added as a search path. ("psycopg/psycopg/__init__.py", "from psycopg.box import Box"), ])?; assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } #[test] fn default_root_flat_layout() -> anyhow::Result<()> { let case = CliTest::with_files([ ("app/foo.py", "foo = 10"), ("bar.py", "bar = 20"), ( "app/main.py", r#" from app.foo import foo from bar import bar print(f"{foo} {bar}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } #[test] fn default_root_tests_folder() -> anyhow::Result<()> { let case = CliTest::with_files([ ("src/foo.py", "foo = 10"), ("tests/bar.py", "baz = 20"), ( "tests/test_bar.py", r#" from foo import foo from bar import baz print(f"{foo} {baz}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } /// If `tests/__init__.py` is present, it is considered a package and `tests` is not added to `sys.path`. #[test] fn default_root_tests_package() -> anyhow::Result<()> { let case = CliTest::with_files([ ("src/foo.py", "foo = 10"), ("tests/__init__.py", ""), ("tests/bar.py", "bar = 20"), ( "tests/test_bar.py", r#" from foo import foo from bar import bar # expected unresolved import print(f"{foo} {bar}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `bar` --> tests/test_bar.py:3:6 | 3 | from bar import bar # expected unresolved import | ^^^ info: Searched in the following paths during module resolution: info: 1. /src (first-party code) info: 2. / (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 Found 1 diagnostic ----- stderr ----- "); 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(), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); 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(), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `bar` --> python/test_bar.py:3:6 | 3 | from bar import bar # expected unresolved import | ^^^ info: Searched in the following paths during module resolution: info: 1. /src (first-party code) info: 2. / (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 Found 1 diagnostic ----- stderr ----- "); 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(), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `bar` --> python/test_bar.py:3:6 | 3 | from bar import bar # expected unresolved import | ^^^ info: Searched in the following paths during module resolution: info: 1. /src (first-party code) info: 2. / (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 Found 1 diagnostic ----- stderr ----- "); Ok(()) } #[test] fn pythonpath_is_respected() -> anyhow::Result<()> { let case = CliTest::with_files([ ("baz-dir/baz.py", "it = 42"), ( "src/foo.py", r#" import baz print(f"{baz.it}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `baz` --> src/foo.py:2:8 | 2 | import baz | ^^^ info: Searched in the following paths during module resolution: info: 1. /src (first-party code) info: 2. / (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 Found 1 diagnostic ----- stderr ----- "); assert_cmd_snapshot!(case.command() .env("PYTHONPATH", case.root().join("baz-dir")), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } #[test] fn pythonpath_multiple_dirs_is_respected() -> anyhow::Result<()> { let case = CliTest::with_files([ ("baz-dir/baz.py", "it = 42"), ("foo-dir/foo.py", "it = 42"), ( "src/main.py", r#" import baz import foo print(f"{baz.it}") print(f"{foo.it}") "#, ), ])?; assert_cmd_snapshot!(case.command(), @" success: false exit_code: 1 ----- stdout ----- error[import-not-found]: Cannot resolve imported module `baz` --> src/main.py:2:8 | 2 | import baz | ^^^ info: Searched in the following paths during module resolution: info: 1. /src (first-party code) info: 2. / (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 error[import-not-found]: Cannot resolve imported module `foo` --> src/main.py:3:8 | 3 | import foo | ^^^ info: Searched in the following paths during module resolution: info: 1. /src (first-party code) info: 2. / (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 Found 2 diagnostics ----- stderr ----- "); let pythonpath = std::env::join_paths([case.root().join("baz-dir"), case.root().join("foo-dir")])?; assert_cmd_snapshot!(case.command() .env("PYTHONPATH", pythonpath), @" success: true exit_code: 0 ----- stdout ----- All checks passed! ----- stderr ----- "); Ok(()) } /// Test behavior when `VIRTUAL_ENV` is set but points to a non-existent path. #[test] fn missing_virtual_env() -> anyhow::Result<()> { let working_venv_package1_path = if cfg!(windows) { "project/.venv/Lib/site-packages/package1/__init__.py" } else { "project/.venv/lib/python3.13/site-packages/package1/__init__.py" }; let case = CliTest::with_files([ ( "project/test.py", r#" from package1 import WorkingVenv "#, ), ( "project/.venv/pyvenv.cfg", r#" home = ./ "#, ), ( working_venv_package1_path, r#" class WorkingVenv: ... "#, ), ])?; assert_cmd_snapshot!(case.command() .current_dir(case.root().join("project")) .env("VIRTUAL_ENV", case.root().join("nonexistent-venv")), @" success: false exit_code: 2 ----- stdout ----- ----- stderr ----- ty failed Cause: Failed to discover local Python environment Cause: Invalid `VIRTUAL_ENV` environment variable `/nonexistent-venv`: does not point to a directory on disk Cause: No such file or directory (os error 2) "); Ok(()) }