Add hidden --extension to override inference of source type from file extension (#8373)

## Summary

This PR addresses the incompatibility with `jupyterlab-lsp` +
`python-lsp-ruff` arising from the inference of source type from file
extension, raised in #6847.

In particular it follows the suggestion in
https://github.com/astral-sh/ruff/issues/6847#issuecomment-1765724679 to
specify a mapping from file extension to source type.

The source types are

- python
- pyi
- ipynb

Usage:

```sh
ruff check --no-cache --stdin-filename Untitled.ipynb --extension ipynb:python
```

Unlike the original suggestion, `:` instead of `=` is used to associate
file extensions to language since that is what is used with
`--per-file-ignores` which is an existing option that accepts a mapping.

## Test Plan

2 tests added to `integration_test.rs` to ensure the override works as
expected

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
This commit is contained in:
Felix Williams 2023-11-08 02:32:40 +00:00 committed by GitHub
parent 71e93a9fa4
commit 7391f74cbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 296 additions and 34 deletions

View file

@ -25,8 +25,8 @@ use ruff_linter::rule_selector::{PreviewOptions, Specificity};
use ruff_linter::rules::pycodestyle;
use ruff_linter::settings::rule_table::RuleTable;
use ruff_linter::settings::types::{
FilePattern, FilePatternSet, PerFileIgnore, PreviewMode, PythonVersion, SerializationFormat,
UnsafeFixes, Version,
ExtensionMapping, FilePattern, FilePatternSet, PerFileIgnore, PreviewMode, PythonVersion,
SerializationFormat, UnsafeFixes, Version,
};
use ruff_linter::settings::{
resolve_per_file_ignores, LinterSettings, DEFAULT_SELECTORS, DUMMY_VARIABLE_RGX, TASK_TAGS,
@ -216,6 +216,7 @@ impl Configuration {
linter: LinterSettings {
rules: lint.as_rule_table(lint_preview),
exclude: FilePatternSet::try_from_iter(lint.exclude.unwrap_or_default())?,
extension: lint.extension.unwrap_or_default(),
preview: lint_preview,
target_version,
project_root: project_root.to_path_buf(),
@ -523,6 +524,7 @@ impl Configuration {
pub struct LintConfiguration {
pub exclude: Option<Vec<FilePattern>>,
pub preview: Option<PreviewMode>,
pub extension: Option<ExtensionMapping>,
// Rule selection
pub extend_per_file_ignores: Vec<PerFileIgnore>,
@ -589,6 +591,9 @@ impl LintConfiguration {
.chain(options.common.extend_unfixable.into_iter().flatten())
.collect();
Ok(LintConfiguration {
// `--extension` is a hidden command-line argument that isn't supported in configuration
// files at present.
extension: None,
exclude: options.exclude.map(|paths| {
paths
.into_iter()
@ -905,6 +910,7 @@ impl LintConfiguration {
Self {
exclude: self.exclude.or(config.exclude),
preview: self.preview.or(config.preview),
extension: self.extension.or(config.extension),
rule_selections: config
.rule_selections
.into_iter()