Skip to content

Commit 9f5ae58

Browse files
committed
[ty] improve base conda distinction child conda
1 parent caf48f4 commit 9f5ae58

File tree

2 files changed

+40
-42
lines changed

2 files changed

+40
-42
lines changed

crates/ty/tests/cli/python_environment.rs

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -943,9 +943,9 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
943943
/// The `site-packages` directory is used by ty for external import.
944944
/// Ty does the following checks to discover the `site-packages` directory in the order:
945945
/// 1) If `VIRTUAL_ENV` environment variable is set
946-
/// 2) If `CONDA_PREFIX` environment variable is set (and .filename != `CONDA_DEFAULT_ENV`)
946+
/// 2) If `CONDA_PREFIX` environment variable is set (and .filename == `CONDA_DEFAULT_ENV`)
947947
/// 3) If a `.venv` directory exists at the project root
948-
/// 4) If `CONDA_PREFIX` environment variable is set (and .filename == `CONDA_DEFAULT_ENV`)
948+
/// 4) If `CONDA_PREFIX` environment variable is set (and .filename != `CONDA_DEFAULT_ENV`)
949949
///
950950
/// This test (and the next one) is aiming at validating the logic around these cases.
951951
///
@@ -986,15 +986,14 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
986986
/// │ └── site-packages
987987
/// │ └── package1
988988
/// │ └── __init__.py
989-
/// ├── conda-env
990-
/// │ └── lib
991-
/// │ └── python3.13
992-
/// │ └── site-packages
993-
/// │ └── package1
994-
/// │ └── __init__.py
995989
/// └── conda
990+
/// ├── lib
991+
/// │ └── python3.13
992+
/// │ └── site-packages
993+
/// │ └── package1
994+
/// │ └── __init__.py
996995
/// └── envs
997-
/// └── base
996+
/// └── conda-env
998997
/// └── lib
999998
/// └── python3.13
1000999
/// └── site-packages
@@ -1006,15 +1005,15 @@ fn defaults_to_a_new_python_version() -> anyhow::Result<()> {
10061005
#[test]
10071006
fn check_venv_resolution_with_working_venv() -> anyhow::Result<()> {
10081007
let child_conda_package1_path = if cfg!(windows) {
1009-
"conda-env/Lib/site-packages/package1/__init__.py"
1008+
"conda/envs/conda-env/Lib/site-packages/package1/__init__.py"
10101009
} else {
1011-
"conda-env/lib/python3.13/site-packages/package1/__init__.py"
1010+
"conda/envs/conda-env/lib/python3.13/site-packages/package1/__init__.py"
10121011
};
10131012

10141013
let base_conda_package1_path = if cfg!(windows) {
1015-
"conda/envs/base/Lib/site-packages/package1/__init__.py"
1014+
"conda/Lib/site-packages/package1/__init__.py"
10161015
} else {
1017-
"conda/envs/base/lib/python3.13/site-packages/package1/__init__.py"
1016+
"conda//lib/python3.13/site-packages/package1/__init__.py"
10181017
};
10191018

10201019
let working_venv_package1_path = if cfg!(windows) {
@@ -1136,7 +1135,7 @@ home = ./
11361135
// run with CONDA_PREFIX set, should find the child conda
11371136
assert_cmd_snapshot!(case.command()
11381137
.current_dir(case.root().join("project"))
1139-
.env("CONDA_PREFIX", case.root().join("conda-env")), @r"
1138+
.env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")), @r"
11401139
success: false
11411140
exit_code: 1
11421141
----- stdout -----
@@ -1157,21 +1156,21 @@ home = ./
11571156
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
11581157
");
11591158

1160-
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find child conda
1159+
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find working venv
11611160
assert_cmd_snapshot!(case.command()
11621161
.current_dir(case.root().join("project"))
1163-
.env("CONDA_PREFIX", case.root().join("conda-env"))
1162+
.env("CONDA_PREFIX", case.root().join("conda"))
11641163
.env("CONDA_DEFAULT_ENV", "base"), @r"
11651164
success: false
11661165
exit_code: 1
11671166
----- stdout -----
1168-
error[unresolved-import]: Module `package1` has no member `ChildConda`
1169-
--> test.py:3:22
1167+
error[unresolved-import]: Module `package1` has no member `WorkingVenv`
1168+
--> test.py:4:22
11701169
|
11711170
2 | from package1 import ActiveVenv
11721171
3 | from package1 import ChildConda
1173-
| ^^^^^^^^^^
11741172
4 | from package1 import WorkingVenv
1173+
| ^^^^^^^^^^^
11751174
5 | from package1 import BaseConda
11761175
|
11771176
info: rule `unresolved-import` is enabled by default
@@ -1186,7 +1185,7 @@ home = ./
11861185
// should find child active venv
11871186
assert_cmd_snapshot!(case.command()
11881187
.current_dir(case.root().join("project"))
1189-
.env("CONDA_PREFIX", case.root().join("conda-env"))
1188+
.env("CONDA_PREFIX", case.root().join("conda"))
11901189
.env("CONDA_DEFAULT_ENV", "base")
11911190
.env("VIRTUAL_ENV", case.root().join("myvenv")), @r"
11921191
success: false
@@ -1208,21 +1207,21 @@ home = ./
12081207
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
12091208
");
12101209

1211-
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (equal!) set, should find working venv
1210+
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (equal!) set, should find ChildConda
12121211
assert_cmd_snapshot!(case.command()
12131212
.current_dir(case.root().join("project"))
1214-
.env("CONDA_PREFIX", case.root().join("conda/envs/base"))
1215-
.env("CONDA_DEFAULT_ENV", "base"), @r"
1213+
.env("CONDA_PREFIX", case.root().join("conda/envs/conda-env"))
1214+
.env("CONDA_DEFAULT_ENV", "conda-env"), @r"
12161215
success: false
12171216
exit_code: 1
12181217
----- stdout -----
1219-
error[unresolved-import]: Module `package1` has no member `WorkingVenv`
1220-
--> test.py:4:22
1218+
error[unresolved-import]: Module `package1` has no member `ChildConda`
1219+
--> test.py:3:22
12211220
|
12221221
2 | from package1 import ActiveVenv
12231222
3 | from package1 import ChildConda
1223+
| ^^^^^^^^^^
12241224
4 | from package1 import WorkingVenv
1225-
| ^^^^^^^^^^^
12261225
5 | from package1 import BaseConda
12271226
|
12281227
info: rule `unresolved-import` is enabled by default
@@ -1242,15 +1241,15 @@ home = ./
12421241
#[test]
12431242
fn check_venv_resolution_without_working_venv() -> anyhow::Result<()> {
12441243
let child_conda_package1_path = if cfg!(windows) {
1245-
"conda-env/Lib/site-packages/package1/__init__.py"
1244+
"conda/envs/conda-env/Lib/site-packages/package1/__init__.py"
12461245
} else {
1247-
"conda-env/lib/python3.13/site-packages/package1/__init__.py"
1246+
"conda/envs/conda-env/lib/python3.13/site-packages/package1/__init__.py"
12481247
};
12491248

12501249
let base_conda_package1_path = if cfg!(windows) {
1251-
"conda/envs/base/Lib/site-packages/package1/__init__.py"
1250+
"conda/Lib/site-packages/package1/__init__.py"
12521251
} else {
1253-
"conda/envs/base/lib/python3.13/site-packages/package1/__init__.py"
1252+
"conda/lib/python3.13/site-packages/package1/__init__.py"
12541253
};
12551254

12561255
let active_venv_package1_path = if cfg!(windows) {
@@ -1398,7 +1397,7 @@ home = ./
13981397
// run with CONDA_PREFIX set, should find the child conda
13991398
assert_cmd_snapshot!(case.command()
14001399
.current_dir(case.root().join("project"))
1401-
.env("CONDA_PREFIX", case.root().join("conda-env")), @r"
1400+
.env("CONDA_PREFIX", case.root().join("conda/envs/conda-env")), @r"
14021401
success: false
14031402
exit_code: 1
14041403
----- stdout -----
@@ -1419,22 +1418,21 @@ home = ./
14191418
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
14201419
");
14211420

1422-
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find child conda
1421+
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV set (unequal), should find base conda
14231422
assert_cmd_snapshot!(case.command()
14241423
.current_dir(case.root().join("project"))
1425-
.env("CONDA_PREFIX", case.root().join("conda-env"))
1424+
.env("CONDA_PREFIX", case.root().join("conda"))
14261425
.env("CONDA_DEFAULT_ENV", "base"), @r"
14271426
success: false
14281427
exit_code: 1
14291428
----- stdout -----
1430-
error[unresolved-import]: Module `package1` has no member `ChildConda`
1431-
--> test.py:3:22
1429+
error[unresolved-import]: Module `package1` has no member `BaseConda`
1430+
--> test.py:5:22
14321431
|
1433-
2 | from package1 import ActiveVenv
14341432
3 | from package1 import ChildConda
1435-
| ^^^^^^^^^^
14361433
4 | from package1 import WorkingVenv
14371434
5 | from package1 import BaseConda
1435+
| ^^^^^^^^^
14381436
|
14391437
info: rule `unresolved-import` is enabled by default
14401438
@@ -1448,7 +1446,7 @@ home = ./
14481446
// should find child active venv
14491447
assert_cmd_snapshot!(case.command()
14501448
.current_dir(case.root().join("project"))
1451-
.env("CONDA_PREFIX", case.root().join("conda-env"))
1449+
.env("CONDA_PREFIX", case.root().join("conda"))
14521450
.env("CONDA_DEFAULT_ENV", "base")
14531451
.env("VIRTUAL_ENV", case.root().join("myvenv")), @r"
14541452
success: false
@@ -1470,10 +1468,10 @@ home = ./
14701468
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
14711469
");
14721470

1473-
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (equal!) set, should find base conda
1471+
// run with CONDA_PREFIX and CONDA_DEFAULT_ENV (unequal!) set, should find base conda
14741472
assert_cmd_snapshot!(case.command()
14751473
.current_dir(case.root().join("project"))
1476-
.env("CONDA_PREFIX", case.root().join("conda/envs/base"))
1474+
.env("CONDA_PREFIX", case.root().join("conda"))
14771475
.env("CONDA_DEFAULT_ENV", "base"), @r"
14781476
success: false
14791477
exit_code: 1

crates/ty_python_semantic/src/site_packages.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -633,9 +633,9 @@ impl CondaEnvironmentKind {
633633
};
634634

635635
if name == default_env {
636-
CondaEnvironmentKind::Base
637-
} else {
638636
CondaEnvironmentKind::Child
637+
} else {
638+
CondaEnvironmentKind::Base
639639
}
640640
}
641641
}

0 commit comments

Comments
 (0)