-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Audit how we determine whether a file is a "Python source file" #13691
Comments
Note: I think that depends on the file system's case sensitivity. Other than that. I think we should ignore casing when matching files because that's what most desktop environments to. They use the same application to open a |
Here, for example, we might want to continue to do case-sensitive matching, since the rule only applies to the names of Python modules that are intended to be importable by other Python modules: ruff/crates/ruff_linter/src/rules/pep8_naming/rules/invalid_module_name.rs Lines 51 to 59 in 5b4afd3
If a |
There's also this issue astral-sh/ruff-vscode#574 related to the server although not sure if this affects that. |
Fwiw mypy has some special handling in a few places to be case sensitive even on case insensitive file systems |
I think this is the relevant code in mypy: Both methods are called from within module resolution. @AlexWaygood and @carljm any chance you know how Python detects the real casing of a file in its module resolver? |
I think I found it: It seems Python indexes module on a per directory basis and uses It does seem that the extension casing is ignored, at least on windows Python assumes entire platforms as case sensitive or insensitive even though a mounted FAT partition on linux is case sensitive and macOs and both NTFS and macos can be configured to be case sensitive |
I don't think this is really true, or it's only sort of true. The platform checking code you linked does not automatically determine any default case handling. It is purely for determining whether the legacy The case insensitive suffix handling on Windows is a similar story. It was a legacy feature added on windows a long time ago and it is preserved on windows to avoid breakage there, but there is no desire to ever expand it to any other platform. So in both these cases, there aren't assumptions about case sensitivity made that would cause a bug if there's a case sensitive file systems on Windows or non case sensitive file systems on Linux. There are just certain case-related legacy features that are documented as only available on certain platform(s). I think if you ignore these two legacy features, the story for Python imports is pretty simple: imports are always case sensitive, based on needing to match what |
Oh right, there's an |
I noticed in #13682 that there's some inconsistency regarding how we determine whether a file is a "Python source file" currently. In the code for
ruff server
(and the red-knot port of the server), we take care to do case-insensitive matching when figuring out whether something is a notebook file or not:ruff/crates/ruff_server/src/session/index.rs
Lines 124 to 126 in 5b4afd3
ruff/crates/red_knot_server/src/session/index.rs
Lines 75 to 79 in 5b4afd3
Elsewhere, however, we mostly use case-sensitive matching:
ruff/crates/ruff_python_ast/src/lib.rs
Lines 91 to 101 in 5b4afd3
ruff/crates/red_knot_python_semantic/src/module_resolver/path.rs
Lines 41 to 60 in 5b4afd3
For places like the red-knot module resolver, it's likely correct to do case-sensitive matching (Python recognises
foo.py
as an importable module, but notfoo.PY
), but in other places it may not be. We should audit the code to make sure we're using case-sensitive matching and case-insensitive matching for file extensions in the correct places. We should also add comments to the places where there might be a subtle reason why case-(in)sensitive matching is required, rather than vice versa.The text was updated successfully, but these errors were encountered: