mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-28 02:39:59 +00:00
[ty] improve base conda distinction from child conda (#20675)
<!-- Thank you for contributing to Ruff/ty! To help us out with reviewing, please consider the following: - Does this pull request include a summary of the change? (See below.) - Does this pull request include a descriptive title? (Please prefix with `[ty]` for ty pull requests.) - Does this pull request include references to any relevant issues? --> ## Summary #19990 didn't completely fix the base vs. child conda environment distinction, since it detected slightly different behavior than what I usually see in conda. E.g., I see something like the following: ``` (didn't yet activate conda, but base is active) ➜ printenv | grep CONDA CONDA_PYTHON_EXE=/opt/anaconda3/bin/python CONDA_PREFIX=/opt/anaconda3 CONDA_DEFAULT_ENV=base CONDA_EXE=/opt/anaconda3/bin/conda CONDA_SHLVL=1 CONDA_PROMPT_MODIFIER=(base) (activating conda) ➜ conda activate test (test is an active conda environment) ❯ printenv | grep CONDA CONDA_PREFIX=/opt/anaconda3/envs/test CONDA_PYTHON_EXE=/opt/anaconda3/bin/python CONDA_SHLVL=2 CONDA_PREFIX_1=/opt/anaconda3 CONDA_DEFAULT_ENV=test CONDA_PROMPT_MODIFIER=(test) CONDA_EXE=/opt/anaconda3/bin/conda ``` But the current behavior looks for `CONDA_DEFAULT_ENV = basename(CONDA_PREFIX)` for the base environment instead of the child environment, where we actually see this equality. This pull request fixes that and updates the tests correspondingly. ## Test Plan I updated the existing tests with the new behavior. Let me know if you want more tests. Note: It shouldn't be necessary to test for the case where we have `conda/envs/base`, since one should not be able to create such an environment (one with the name of `CONDA_DEFAULT_ENV`). --------- Co-authored-by: Aria Desires <aria.desires@gmail.com>
This commit is contained in:
parent
ebfb33c30b
commit
f73ead11cb
4 changed files with 221 additions and 101 deletions
|
|
@ -943,9 +943,10 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
|
|||
/// The `site-packages` directory is used by ty for external import.
|
||||
/// Ty does the following checks to discover the `site-packages` directory in the order:
|
||||
/// 1) If `VIRTUAL_ENV` environment variable is set
|
||||
/// 2) If `CONDA_PREFIX` environment variable is set (and .filename != `CONDA_DEFAULT_ENV`)
|
||||
/// 2) If `CONDA_PREFIX` environment variable is set (and .filename == `CONDA_DEFAULT_ENV`)
|
||||
/// 3) If a `.venv` directory exists at the project root
|
||||
/// 4) If `CONDA_PREFIX` environment variable is set (and .filename == `CONDA_DEFAULT_ENV`)
|
||||
/// 4) If `CONDA_PREFIX` environment variable is set (and .filename != `CONDA_DEFAULT_ENV`)
|
||||
/// or if `_CONDA_ROOT` is set (and `_CONDA_ROOT` == `CONDA_PREFIX`)
|
||||
///
|
||||
/// This test (and the next one) is aiming at validating the logic around these cases.
|
||||
///
|
||||
|
|
@ -986,15 +987,14 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
|
|||
/// │ └── site-packages
|
||||
/// │ └── package1
|
||||
/// │ └── __init__.py
|
||||
/// ├── conda-env
|
||||
/// │ └── lib
|
||||
/// │ └── python3.13
|
||||
/// │ └── site-packages
|
||||
/// │ └── package1
|
||||
/// │ └── __init__.py
|
||||
/// └── conda
|
||||
/// ├── lib
|
||||
/// │ └── python3.13
|
||||
/// │ └── site-packages
|
||||
/// │ └── package1
|
||||
/// │ └── __init__.py
|
||||
/// └── envs
|
||||
/// └── base
|
||||
/// └── conda-env
|
||||
/// └── lib
|
||||
/// └── python3.13
|
||||
/// └── site-packages
|
||||
|
|
@ -1006,15 +1006,15 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
|
|||
#[test]
|
||||
fn check_venv_resolution_with_working_venv() -> anyhow::Result<()> {
|
||||
let child_conda_package1_path = if cfg!(windows) {
|
||||
"conda-env/Lib/site-packages/package1/__init__.py"
|
||||
"conda/envs/conda-env/Lib/site-packages/package1/__init__.py"
|
||||
} else {
|
||||
"conda-env/lib/python3.13/site-packages/package1/__init__.py"
|
||||
"conda/envs/conda-env/lib/python3.13/site-packages/package1/__init__.py"
|
||||
};
|
||||
|
||||
let base_conda_package1_path = if cfg!(windows) {
|
||||
"conda/envs/base/Lib/site-packages/package1/__init__.py"
|
||||
"conda/Lib/site-packages/package1/__init__.py"
|
||||
} else {
|
||||
"conda/envs/base/lib/python3.13/site-packages/package1/__init__.py"
|
||||
"conda//lib/python3.13/site-packages/package1/__init__.py"
|
||||
};
|
||||
|
||||
let working_venv_package1_path = if cfg!(windows) {
|
||||
|
|
@ -1136,7 +1136,7 @@ home = ./
|
|||
// 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-env")), @r"
|
||||
.env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
|
|
@ -1157,61 +1157,10 @@ home = ./
|
|||
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
|
||||
");
|
||||
|
||||
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find child conda
|
||||
// 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"))
|
||||
.env("CONDA_DEFAULT_ENV", "base"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ChildConda`
|
||||
--> test.py:3:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
3 | from package1 import ChildConda
|
||||
| ^^^^^^^^^^
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
|
|
||||
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.
|
||||
");
|
||||
|
||||
// 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"))
|
||||
.env("CONDA_DEFAULT_ENV", "base")
|
||||
.env("VIRTUAL_ENV", case.root().join("myvenv")), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ActiveVenv`
|
||||
--> test.py:2:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
| ^^^^^^^^^^
|
||||
3 | from package1 import ChildConda
|
||||
4 | from package1 import WorkingVenv
|
||||
|
|
||||
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.
|
||||
");
|
||||
|
||||
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (equal!) set, should find working venv
|
||||
assert_cmd_snapshot!(case.command()
|
||||
.current_dir(case.root().join("project"))
|
||||
.env("CONDA_PREFIX", case.root().join("conda/envs/base"))
|
||||
.env("CONDA_PREFIX", case.root().join("conda"))
|
||||
.env("CONDA_DEFAULT_ENV", "base"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
|
|
@ -1233,6 +1182,106 @@ home = ./
|
|||
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
|
||||
");
|
||||
|
||||
// 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")), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ActiveVenv`
|
||||
--> test.py:2:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
| ^^^^^^^^^^
|
||||
3 | from package1 import ChildConda
|
||||
4 | from package1 import WorkingVenv
|
||||
|
|
||||
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.
|
||||
");
|
||||
|
||||
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (equal!) 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_DEFAULT_ENV", "conda-env"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ChildConda`
|
||||
--> test.py:3:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
3 | from package1 import ChildConda
|
||||
| ^^^^^^^^^^
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
|
|
||||
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.
|
||||
");
|
||||
|
||||
// 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", "conda"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ChildConda`
|
||||
--> test.py:3:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
3 | from package1 import ChildConda
|
||||
| ^^^^^^^^^^
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
|
|
||||
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.
|
||||
");
|
||||
|
||||
// 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", "conda"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `BaseConda`
|
||||
--> test.py:5:22
|
||||
|
|
||||
3 | from package1 import ChildConda
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
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(())
|
||||
}
|
||||
|
||||
|
|
@ -1242,15 +1291,15 @@ home = ./
|
|||
#[test]
|
||||
fn check_venv_resolution_without_working_venv() -> anyhow::Result<()> {
|
||||
let child_conda_package1_path = if cfg!(windows) {
|
||||
"conda-env/Lib/site-packages/package1/__init__.py"
|
||||
"conda/envs/conda-env/Lib/site-packages/package1/__init__.py"
|
||||
} else {
|
||||
"conda-env/lib/python3.13/site-packages/package1/__init__.py"
|
||||
"conda/envs/conda-env/lib/python3.13/site-packages/package1/__init__.py"
|
||||
};
|
||||
|
||||
let base_conda_package1_path = if cfg!(windows) {
|
||||
"conda/envs/base/Lib/site-packages/package1/__init__.py"
|
||||
"conda/Lib/site-packages/package1/__init__.py"
|
||||
} else {
|
||||
"conda/envs/base/lib/python3.13/site-packages/package1/__init__.py"
|
||||
"conda/lib/python3.13/site-packages/package1/__init__.py"
|
||||
};
|
||||
|
||||
let active_venv_package1_path = if cfg!(windows) {
|
||||
|
|
@ -1398,7 +1447,7 @@ home = ./
|
|||
// 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-env")), @r"
|
||||
.env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
|
|
@ -1419,22 +1468,21 @@ home = ./
|
|||
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
|
||||
");
|
||||
|
||||
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find child conda
|
||||
// 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"))
|
||||
.env("CONDA_PREFIX", case.root().join("conda"))
|
||||
.env("CONDA_DEFAULT_ENV", "base"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ChildConda`
|
||||
--> test.py:3:22
|
||||
error[unresolved-import]: Module `package1` has no member `BaseConda`
|
||||
--> test.py:5:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
3 | from package1 import ChildConda
|
||||
| ^^^^^^^^^^
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
info: rule `unresolved-import` is enabled by default
|
||||
|
||||
|
|
@ -1448,7 +1496,7 @@ home = ./
|
|||
// should find child active venv
|
||||
assert_cmd_snapshot!(case.command()
|
||||
.current_dir(case.root().join("project"))
|
||||
.env("CONDA_PREFIX", case.root().join("conda-env"))
|
||||
.env("CONDA_PREFIX", case.root().join("conda"))
|
||||
.env("CONDA_DEFAULT_ENV", "base")
|
||||
.env("VIRTUAL_ENV", case.root().join("myvenv")), @r"
|
||||
success: false
|
||||
|
|
@ -1470,10 +1518,10 @@ home = ./
|
|||
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
|
||||
");
|
||||
|
||||
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (equal!) set, should find base conda
|
||||
// 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/envs/base"))
|
||||
.env("CONDA_PREFIX", case.root().join("conda"))
|
||||
.env("CONDA_DEFAULT_ENV", "base"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
|
|
@ -1494,6 +1542,55 @@ home = ./
|
|||
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
|
||||
");
|
||||
|
||||
// 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", "conda"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `ChildConda`
|
||||
--> test.py:3:22
|
||||
|
|
||||
2 | from package1 import ActiveVenv
|
||||
3 | from package1 import ChildConda
|
||||
| ^^^^^^^^^^
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
|
|
||||
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.
|
||||
");
|
||||
|
||||
// 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", "conda"), @r"
|
||||
success: false
|
||||
exit_code: 1
|
||||
----- stdout -----
|
||||
error[unresolved-import]: Module `package1` has no member `BaseConda`
|
||||
--> test.py:5:22
|
||||
|
|
||||
3 | from package1 import ChildConda
|
||||
4 | from package1 import WorkingVenv
|
||||
5 | from package1 import BaseConda
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
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(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue