Skip to content

Commit d7926bb

Browse files
committed
Migrate namespace module resolver tests to mdtest
1 parent 1ba56b4 commit d7926bb

File tree

2 files changed

+98
-95
lines changed

2 files changed

+98
-95
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Namespace package
2+
3+
## Basic namespace package
4+
5+
```toml
6+
[environment]
7+
python = "/.venv"
8+
```
9+
10+
`parent/child/one.py`:
11+
12+
```py
13+
one = 1
14+
```
15+
16+
`/.venv/<path-to-site-packages>/parent/child/two.py`:
17+
18+
```py
19+
two = 2
20+
```
21+
22+
`main.py`:
23+
24+
```py
25+
import parent.child.one
26+
import parent.child.two
27+
```
28+
29+
## Regular package in namespace package
30+
31+
```toml
32+
[environment]
33+
python = "/.venv"
34+
```
35+
36+
Adopted test case from the
37+
[PEP420 examples](https://peps.python.org/pep-0420/#nested-namespace-packages). The
38+
`src/parent/child` package is a regular package. Therefore, `site_packages/parent/child/two.py`
39+
should not be resolved.
40+
41+
```ignore
42+
src
43+
parent
44+
child
45+
__init__.py
46+
one.py
47+
.venv/site_packages
48+
parent
49+
child
50+
two.py
51+
```
52+
53+
`parent/child/__init__.py`:
54+
55+
```py
56+
```
57+
58+
`parent/child/one.py`:
59+
60+
```py
61+
one = 1
62+
```
63+
64+
`/.venv/<path-to-site-packages>/parent/child/two.py`:
65+
66+
```py
67+
two = 2
68+
```
69+
70+
`main.py`:
71+
72+
```py
73+
import parent.child.one
74+
75+
import parent.child.two # error: [unresolved-import]
76+
```
77+
78+
## Priority between file and identically named namespace package
79+
80+
If there's a namespace package with the same name as a module, the module takes precedence.
81+
82+
`foo.py`:
83+
84+
```py
85+
x = "module"
86+
```
87+
88+
`foo/bar.py`:
89+
90+
```py
91+
x = "namespace"
92+
```
93+
94+
```py
95+
from foo import x
96+
97+
reveal_type(x) # revealed: Unknown | Literal["module"]
98+
```

crates/ty_python_semantic/src/module_resolver/resolver.rs

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,25 +1288,6 @@ mod tests {
12881288
);
12891289
}
12901290

1291-
#[test]
1292-
fn single_file_takes_priority_over_namespace_package() {
1293-
//const SRC: &[FileSpec] = &[("foo.py", "x = 1")];
1294-
const SRC: &[FileSpec] = &[("foo.py", "x = 1"), ("foo/bar.py", "x = 2")];
1295-
1296-
let TestCase { db, src, .. } = TestCaseBuilder::new().with_src_files(SRC).build();
1297-
1298-
let foo_module_name = ModuleName::new_static("foo").unwrap();
1299-
let foo_bar_module_name = ModuleName::new_static("foo.bar").unwrap();
1300-
1301-
// `foo.py` takes priority over the `foo` namespace package
1302-
let foo_module = resolve_module(&db, &foo_module_name).unwrap();
1303-
assert_eq!(foo_module.file().path(&db), &src.join("foo.py"));
1304-
1305-
// `foo.bar` isn't recognised as a module
1306-
let foo_bar_module = resolve_module(&db, &foo_bar_module_name);
1307-
assert_eq!(foo_bar_module, None);
1308-
}
1309-
13101291
#[test]
13111292
fn typing_stub_over_module() {
13121293
const SRC: &[FileSpec] = &[("foo.py", "print('Hello, world!')"), ("foo.pyi", "x: int")];
@@ -1349,82 +1330,6 @@ mod tests {
13491330
);
13501331
}
13511332

1352-
#[test]
1353-
fn namespace_package() {
1354-
// From [PEP420](https://peps.python.org/pep-0420/#nested-namespace-packages).
1355-
// But uses `src` for `project1` and `site-packages` for `project2`.
1356-
// ```
1357-
// src
1358-
// parent
1359-
// child
1360-
// one.py
1361-
// site_packages
1362-
// parent
1363-
// child
1364-
// two.py
1365-
// ```
1366-
let TestCase {
1367-
db,
1368-
src,
1369-
site_packages,
1370-
..
1371-
} = TestCaseBuilder::new()
1372-
.with_src_files(&[("parent/child/one.py", "print('Hello, world!')")])
1373-
.with_site_packages_files(&[("parent/child/two.py", "print('Hello, world!')")])
1374-
.build();
1375-
1376-
let one_module_name = ModuleName::new_static("parent.child.one").unwrap();
1377-
let one_module_path = FilePath::System(src.join("parent/child/one.py"));
1378-
assert_eq!(
1379-
resolve_module(&db, &one_module_name),
1380-
path_to_module(&db, &one_module_path)
1381-
);
1382-
1383-
let two_module_name = ModuleName::new_static("parent.child.two").unwrap();
1384-
let two_module_path = FilePath::System(site_packages.join("parent/child/two.py"));
1385-
assert_eq!(
1386-
resolve_module(&db, &two_module_name),
1387-
path_to_module(&db, &two_module_path)
1388-
);
1389-
}
1390-
1391-
#[test]
1392-
fn regular_package_in_namespace_package() {
1393-
// Adopted test case from the [PEP420 examples](https://peps.python.org/pep-0420/#nested-namespace-packages).
1394-
// The `src/parent/child` package is a regular package. Therefore, `site_packages/parent/child/two.py` should not be resolved.
1395-
// ```
1396-
// src
1397-
// parent
1398-
// child
1399-
// one.py
1400-
// site_packages
1401-
// parent
1402-
// child
1403-
// two.py
1404-
// ```
1405-
const SRC: &[FileSpec] = &[
1406-
("parent/child/__init__.py", "print('Hello, world!')"),
1407-
("parent/child/one.py", "print('Hello, world!')"),
1408-
];
1409-
1410-
const SITE_PACKAGES: &[FileSpec] = &[("parent/child/two.py", "print('Hello, world!')")];
1411-
1412-
let TestCase { db, src, .. } = TestCaseBuilder::new()
1413-
.with_src_files(SRC)
1414-
.with_site_packages_files(SITE_PACKAGES)
1415-
.build();
1416-
1417-
let one_module_path = FilePath::System(src.join("parent/child/one.py"));
1418-
let one_module_name =
1419-
resolve_module(&db, &ModuleName::new_static("parent.child.one").unwrap());
1420-
assert_eq!(one_module_name, path_to_module(&db, &one_module_path));
1421-
1422-
assert_eq!(
1423-
None,
1424-
resolve_module(&db, &ModuleName::new_static("parent.child.two").unwrap())
1425-
);
1426-
}
1427-
14281333
#[test]
14291334
fn module_search_path_priority() {
14301335
let TestCase {

0 commit comments

Comments
 (0)