-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for case-sensitive module resolution (#16517)
## Summary Python's module resolver is case sensitive. This PR adds mdtests that assert that our module resolution is case sensitive. The tests currently all pass because our in memory file system is case sensitive. I'll add support for using the real file system to the mdtest framework in a separate PR. This PR also adds support for specifying extra search paths to the mdtest framework. ## Test Plan The tests fail when running them using the real file system.
- Loading branch information
1 parent
ebd172e
commit 48f906e
Showing
4 changed files
with
156 additions
and
10 deletions.
There are no files selected for viewing
126 changes: 126 additions & 0 deletions
126
crates/red_knot_python_semantic/resources/mdtest/import/case_sensitive.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
# Case Sensitive Imports | ||
|
||
TODO: This test should use the real file system instead of the memory file system. | ||
|
||
Python's import system is case-sensitive even on case-insensitive file system. This means, importing | ||
a module `a` should fail if the file in the search paths is named `A.py`. See | ||
[PEP 235](https://peps.python.org/pep-0235/). | ||
|
||
## Correct casing | ||
|
||
Importing a module where the name matches the file name's casing should succeed. | ||
|
||
`a.py`: | ||
|
||
```py | ||
class Foo: | ||
x: int = 1 | ||
``` | ||
|
||
```python | ||
from a import Foo | ||
|
||
reveal_type(Foo().x) # revealed: int | ||
``` | ||
|
||
## Incorrect casing | ||
|
||
Importing a module where the name does not match the file name's casing should fail. | ||
|
||
`A.py`: | ||
|
||
```py | ||
class Foo: | ||
x: int = 1 | ||
``` | ||
|
||
```python | ||
# error: [unresolved-import] | ||
from a import Foo | ||
``` | ||
|
||
## Multiple search paths with different cased modules | ||
|
||
The resolved module is the first matching the file name's casing but Python falls back to later | ||
search paths if the file name's casing does not match. | ||
|
||
```toml | ||
[environment] | ||
extra-paths = ["/search-1", "/search-2"] | ||
``` | ||
|
||
`/search-1/A.py`: | ||
|
||
```py | ||
class Foo: | ||
x: int = 1 | ||
``` | ||
|
||
`/search-2/a.py`: | ||
|
||
```py | ||
class Bar: | ||
x: str = "test" | ||
``` | ||
|
||
```python | ||
from A import Foo | ||
from a import Bar | ||
|
||
reveal_type(Foo().x) # revealed: int | ||
reveal_type(Bar().x) # revealed: str | ||
``` | ||
|
||
## Intermediate segments | ||
|
||
`db/__init__.py`: | ||
|
||
```py | ||
``` | ||
|
||
`db/a.py`: | ||
|
||
```py | ||
class Foo: | ||
x: int = 1 | ||
``` | ||
|
||
`correctly_cased.py`: | ||
|
||
```python | ||
from db.a import Foo | ||
|
||
reveal_type(Foo().x) # revealed: int | ||
``` | ||
|
||
Imports where some segments are incorrectly cased should fail. | ||
|
||
`incorrectly_cased.py`: | ||
|
||
```python | ||
# error: [unresolved-import] | ||
from DB.a import Foo | ||
|
||
# error: [unresolved-import] | ||
from DB.A import Foo | ||
|
||
# error: [unresolved-import] | ||
from db.A import Foo | ||
``` | ||
|
||
## Incorrect extension casing | ||
|
||
The extension of imported python modules must be `.py` or `.pyi` but not `.PY` or `Py` or any | ||
variant where some characters are uppercase. | ||
|
||
`a.PY`: | ||
|
||
```py | ||
class Foo: | ||
x: int = 1 | ||
``` | ||
|
||
```python | ||
# error: [unresolved-import] | ||
from a import Foo | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters