Skip to content
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

Open
AlexWaygood opened this issue Oct 9, 2024 · 4 comments
Open

Audit how we determine whether a file is a "Python source file" #13691

AlexWaygood opened this issue Oct 9, 2024 · 4 comments
Labels
cli Related to the command-line interface
Milestone

Comments

@AlexWaygood
Copy link
Member

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:

} else if Path::new(url.path())
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case("ipynb"))

} else if Path::new(url.path())
.extension()
.map_or(false, |ext| ext.eq_ignore_ascii_case("ipynb"))
{
DocumentKey::Notebook(url)

Elsewhere, however, we mostly use case-sensitive matching:

/// Infers the source type from the file extension.
pub fn try_from_extension(extension: &str) -> Option<Self> {
let ty = match extension {
"py" => Self::Python,
"pyi" => Self::Stub,
"ipynb" => Self::Ipynb,
_ => return None,
};
Some(ty)
}

pub(crate) fn push(&mut self, component: &str) {
if let Some(component_extension) = camino::Utf8Path::new(component).extension() {
assert!(
self.relative_path.extension().is_none(),
"Cannot push part {component} to {self:?}, which already has an extension"
);
if self.is_standard_library() {
assert_eq!(
component_extension, "pyi",
"Extension must be `pyi`; got `{component_extension}`"
);
} else {
assert!(
matches!(component_extension, "pyi" | "py"),
"Extension must be `py` or `pyi`; got `{component_extension}`"
);
}
}
self.relative_path.push(component);
}

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 not foo.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.

@MichaReiser
Copy link
Member

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 not foo.PY

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 test.jpg or test.JPG.

@MichaReiser MichaReiser added the cli Related to the command-line interface label Oct 9, 2024
@AlexWaygood
Copy link
Member Author

AlexWaygood commented Oct 9, 2024

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:

/// N999
pub(crate) fn invalid_module_name(
path: &Path,
package: Option<&Path>,
ignore_names: &IgnoreNames,
) -> Option<Diagnostic> {
if !PySourceType::try_from_path(path).is_some_and(PySourceType::is_py_file_or_stub) {
return None;
}

If a FOO.PY module can't be imported due to the fact that it uses a .PY extension on a case-sensitive file system, then the name of the file isn't really relevant to any PEP-8 concerns, as it's not a module (even if it might be a Python file). The rule is invalid-module-name, not invalid-python-file-name.

@dhruvmanila
Copy link
Member

There's also this issue astral-sh/ruff-vscode#574 related to the server although not sure if this affects that.

@hauntsaninja
Copy link
Contributor

Fwiw mypy has some special handling in a few places to be case sensitive even on case insensitive file systems

@AlexWaygood AlexWaygood modified the milestones: v0.8, v0.9 Nov 18, 2024
@MichaReiser MichaReiser modified the milestones: v0.9, v.0.10 Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Related to the command-line interface
Projects
None yet
Development

No branches or pull requests

4 participants