Skip to content

Commit 4a3c360

Browse files
committed
add a test for annotations
1 parent a80a65a commit 4a3c360

File tree

3 files changed

+58
-4
lines changed

3 files changed

+58
-4
lines changed

crates/ty/tests/cli.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,61 @@ fn config_file_annotation_showing_where_python_version_set_typing_error() -> any
308308
Ok(())
309309
}
310310

311+
#[test]
312+
fn pyvenv_cfg_file_annotation_showing_where_python_version_set() -> anyhow::Result<()> {
313+
let case = TestCase::with_files([
314+
(
315+
"pyproject.toml",
316+
r#"
317+
[tool.ty.environment]
318+
python = ".venv"
319+
"#,
320+
),
321+
(
322+
".venv/pyvenv.cfg",
323+
r#"
324+
home = /usr
325+
version = 3.8
326+
"#,
327+
),
328+
if cfg!(target_os = "windows") {
329+
(".venv/Lib/site-packages/foo.py", "")
330+
} else {
331+
(".venv/lib/python3.8/site-packages/foo.py", "")
332+
},
333+
("test.py", "aiter"),
334+
])?;
335+
336+
assert_cmd_snapshot!(case.command(), @r"
337+
success: false
338+
exit_code: 1
339+
----- stdout -----
340+
error[unresolved-reference]: Name `aiter` used when not defined
341+
--> test.py:1:1
342+
|
343+
1 | aiter
344+
| ^^^^^
345+
|
346+
info: `aiter` was added as a builtin in Python 3.10
347+
info: Python 3.8 was assumed when resolving types because of your virtual environment
348+
--> .venv/pyvenv.cfg:3:11
349+
|
350+
2 | home = /usr
351+
3 | version = 3.8
352+
| ^^^ Python version inferred from virtual environment metadata file
353+
|
354+
info: No Python version was specified on the command line or in a configuration file
355+
info: rule `unresolved-reference` is enabled by default
356+
357+
Found 1 diagnostic
358+
359+
----- stderr -----
360+
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
361+
");
362+
363+
Ok(())
364+
}
365+
311366
#[test]
312367
fn config_file_annotation_showing_where_python_version_set_syntax_error() -> anyhow::Result<()> {
313368
let case = TestCase::with_files([

crates/ty_python_semantic/src/module_resolver/resolver.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,7 @@ impl SearchPaths {
251251
// than the one resolved in the program settings because it indicates
252252
// that the `target-version` is incorrectly configured or that the
253253
// venv is out of date.
254-
PythonEnvironment::new(sys_prefix.clone(), *origin, system)?
255-
.into_settings(system)?
254+
PythonEnvironment::new(sys_prefix, *origin, system)?.into_settings(system)?
256255
}
257256

258257
PythonPath::Resolve(target, origin) => {

crates/ty_python_semantic/src/site_packages.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl PythonEnvironment {
133133
system: &dyn System,
134134
) -> SitePackagesDiscoveryResult<SitePackagesPaths> {
135135
match self {
136-
Self::Virtual(venv) => venv.site_packages_directories(system),
136+
Self::Virtual(env) => env.site_packages_directories(system),
137137
Self::System(env) => env.site_packages_directories(system),
138138
}
139139
}
@@ -290,7 +290,7 @@ impl VirtualEnvironment {
290290
}
291291

292292
while !cursor.is_eof() {
293-
if cursor.eat_char('\n') || cursor.eat_char2('\r', '\n') {
293+
if cursor.eat_char('\n') {
294294
break;
295295
}
296296
cursor.bump();

0 commit comments

Comments
 (0)