Skip to content

Commit

Permalink
Enable autofix for unconventional imports rule
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Jun 16, 2023
1 parent 8c3b1e1 commit b61cf0d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use rustc_hash::FxHashMap;

use ruff_diagnostics::{Diagnostic, Violation};
use ruff_diagnostics::{AutofixKind, Diagnostic, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_semantic::Scope;

use crate::checkers::ast::Checker;
use crate::registry::AsRule;
use crate::renamer::Renamer;

/// ## What it does
/// Checks for imports that are typically imported using a common convention,
Expand Down Expand Up @@ -34,11 +36,18 @@ pub struct UnconventionalImportAlias {
}

impl Violation for UnconventionalImportAlias {
const AUTOFIX: AutofixKind = AutofixKind::Sometimes;

#[derive_message_formats]
fn message(&self) -> String {
let UnconventionalImportAlias { name, asname } = self;
format!("`{name}` should be imported as `{asname}`")
}

fn autofix_title(&self) -> Option<String> {
let UnconventionalImportAlias { name, asname } = self;
Some(format!("Alias `{name}` to `{asname}`"))
}
}

/// ICN001
Expand All @@ -63,13 +72,23 @@ pub(crate) fn unconventional_import_alias(
continue;
}

diagnostics.push(Diagnostic::new(
let mut diagnostic = Diagnostic::new(
UnconventionalImportAlias {
name: qualified_name.to_string(),
asname: expected_alias.to_string(),
},
binding.range,
));
);
if checker.patch(diagnostic.kind.rule()) {
if checker.semantic().is_available(expected_alias) {
diagnostic.try_set_fix(|| {
let (edit, rest) =
Renamer::rename(name, expected_alias, scope, checker.semantic())?;
Ok(Fix::suggested_edits(edit, rest))
});
}
}
diagnostics.push(diagnostic);
}
None
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ custom.py:3:8: ICN001 `altair` should be imported as `alt`
4 | import dask.array # unconventional
5 | import dask.dataframe # unconventional
|
= help: Alias `altair` to `alt`

custom.py:4:8: ICN001 `dask.array` should be imported as `da`
|
Expand All @@ -19,6 +20,7 @@ custom.py:4:8: ICN001 `dask.array` should be imported as `da`
5 | import dask.dataframe # unconventional
6 | import matplotlib.pyplot # unconventional
|
= help: Alias `dask.array` to `da`

custom.py:5:8: ICN001 `dask.dataframe` should be imported as `dd`
|
Expand All @@ -29,6 +31,7 @@ custom.py:5:8: ICN001 `dask.dataframe` should be imported as `dd`
6 | import matplotlib.pyplot # unconventional
7 | import numpy # unconventional
|
= help: Alias `dask.dataframe` to `dd`

custom.py:6:8: ICN001 `matplotlib.pyplot` should be imported as `plt`
|
Expand All @@ -39,6 +42,7 @@ custom.py:6:8: ICN001 `matplotlib.pyplot` should be imported as `plt`
7 | import numpy # unconventional
8 | import pandas # unconventional
|
= help: Alias `matplotlib.pyplot` to `plt`

custom.py:7:8: ICN001 `numpy` should be imported as `np`
|
Expand All @@ -49,6 +53,7 @@ custom.py:7:8: ICN001 `numpy` should be imported as `np`
8 | import pandas # unconventional
9 | import seaborn # unconventional
|
= help: Alias `numpy` to `np`

custom.py:8:8: ICN001 `pandas` should be imported as `pd`
|
Expand All @@ -59,6 +64,7 @@ custom.py:8:8: ICN001 `pandas` should be imported as `pd`
9 | import seaborn # unconventional
10 | import tensorflow # unconventional
|
= help: Alias `pandas` to `pd`

custom.py:9:8: ICN001 `seaborn` should be imported as `sns`
|
Expand All @@ -69,6 +75,7 @@ custom.py:9:8: ICN001 `seaborn` should be imported as `sns`
10 | import tensorflow # unconventional
11 | import holoviews # unconventional
|
= help: Alias `seaborn` to `sns`

custom.py:10:8: ICN001 `tensorflow` should be imported as `tf`
|
Expand All @@ -79,6 +86,7 @@ custom.py:10:8: ICN001 `tensorflow` should be imported as `tf`
11 | import holoviews # unconventional
12 | import panel # unconventional
|
= help: Alias `tensorflow` to `tf`

custom.py:11:8: ICN001 `holoviews` should be imported as `hv`
|
Expand All @@ -89,6 +97,7 @@ custom.py:11:8: ICN001 `holoviews` should be imported as `hv`
12 | import panel # unconventional
13 | import plotly.express # unconventional
|
= help: Alias `holoviews` to `hv`

custom.py:12:8: ICN001 `panel` should be imported as `pn`
|
Expand All @@ -99,6 +108,7 @@ custom.py:12:8: ICN001 `panel` should be imported as `pn`
13 | import plotly.express # unconventional
14 | import matplotlib # unconventional
|
= help: Alias `panel` to `pn`

custom.py:13:8: ICN001 `plotly.express` should be imported as `px`
|
Expand All @@ -109,6 +119,7 @@ custom.py:13:8: ICN001 `plotly.express` should be imported as `px`
14 | import matplotlib # unconventional
15 | import polars # unconventional
|
= help: Alias `plotly.express` to `px`

custom.py:14:8: ICN001 `matplotlib` should be imported as `mpl`
|
Expand All @@ -119,6 +130,7 @@ custom.py:14:8: ICN001 `matplotlib` should be imported as `mpl`
15 | import polars # unconventional
16 | import pyarrow # unconventional
|
= help: Alias `matplotlib` to `mpl`

custom.py:15:8: ICN001 `polars` should be imported as `pl`
|
Expand All @@ -128,6 +140,7 @@ custom.py:15:8: ICN001 `polars` should be imported as `pl`
| ^^^^^^ ICN001
16 | import pyarrow # unconventional
|
= help: Alias `polars` to `pl`

custom.py:16:8: ICN001 `pyarrow` should be imported as `pa`
|
Expand All @@ -138,6 +151,7 @@ custom.py:16:8: ICN001 `pyarrow` should be imported as `pa`
17 |
18 | import altair as altr # unconventional
|
= help: Alias `pyarrow` to `pa`

custom.py:18:18: ICN001 `altair` should be imported as `alt`
|
Expand All @@ -148,6 +162,7 @@ custom.py:18:18: ICN001 `altair` should be imported as `alt`
19 | import matplotlib.pyplot as plot # unconventional
20 | import dask.array as darray # unconventional
|
= help: Alias `altair` to `alt`

custom.py:19:29: ICN001 `matplotlib.pyplot` should be imported as `plt`
|
Expand All @@ -157,6 +172,7 @@ custom.py:19:29: ICN001 `matplotlib.pyplot` should be imported as `plt`
20 | import dask.array as darray # unconventional
21 | import dask.dataframe as ddf # unconventional
|
= help: Alias `matplotlib.pyplot` to `plt`

custom.py:20:22: ICN001 `dask.array` should be imported as `da`
|
Expand All @@ -167,6 +183,7 @@ custom.py:20:22: ICN001 `dask.array` should be imported as `da`
21 | import dask.dataframe as ddf # unconventional
22 | import numpy as nmp # unconventional
|
= help: Alias `dask.array` to `da`

custom.py:21:26: ICN001 `dask.dataframe` should be imported as `dd`
|
Expand All @@ -177,6 +194,7 @@ custom.py:21:26: ICN001 `dask.dataframe` should be imported as `dd`
22 | import numpy as nmp # unconventional
23 | import pandas as pdas # unconventional
|
= help: Alias `dask.dataframe` to `dd`

custom.py:22:17: ICN001 `numpy` should be imported as `np`
|
Expand All @@ -187,6 +205,7 @@ custom.py:22:17: ICN001 `numpy` should be imported as `np`
23 | import pandas as pdas # unconventional
24 | import seaborn as sbrn # unconventional
|
= help: Alias `numpy` to `np`

custom.py:23:18: ICN001 `pandas` should be imported as `pd`
|
Expand All @@ -197,6 +216,7 @@ custom.py:23:18: ICN001 `pandas` should be imported as `pd`
24 | import seaborn as sbrn # unconventional
25 | import tensorflow as tfz # unconventional
|
= help: Alias `pandas` to `pd`

custom.py:24:19: ICN001 `seaborn` should be imported as `sns`
|
Expand All @@ -207,6 +227,7 @@ custom.py:24:19: ICN001 `seaborn` should be imported as `sns`
25 | import tensorflow as tfz # unconventional
26 | import holoviews as hsv # unconventional
|
= help: Alias `seaborn` to `sns`

custom.py:25:22: ICN001 `tensorflow` should be imported as `tf`
|
Expand All @@ -217,6 +238,7 @@ custom.py:25:22: ICN001 `tensorflow` should be imported as `tf`
26 | import holoviews as hsv # unconventional
27 | import panel as pns # unconventional
|
= help: Alias `tensorflow` to `tf`

custom.py:26:21: ICN001 `holoviews` should be imported as `hv`
|
Expand All @@ -227,6 +249,7 @@ custom.py:26:21: ICN001 `holoviews` should be imported as `hv`
27 | import panel as pns # unconventional
28 | import plotly.express as pltx # unconventional
|
= help: Alias `holoviews` to `hv`

custom.py:27:17: ICN001 `panel` should be imported as `pn`
|
Expand All @@ -237,6 +260,7 @@ custom.py:27:17: ICN001 `panel` should be imported as `pn`
28 | import plotly.express as pltx # unconventional
29 | import matplotlib as ml # unconventional
|
= help: Alias `panel` to `pn`

custom.py:28:26: ICN001 `plotly.express` should be imported as `px`
|
Expand All @@ -247,6 +271,7 @@ custom.py:28:26: ICN001 `plotly.express` should be imported as `px`
29 | import matplotlib as ml # unconventional
30 | import polars as ps # unconventional
|
= help: Alias `plotly.express` to `px`

custom.py:29:22: ICN001 `matplotlib` should be imported as `mpl`
|
Expand All @@ -257,6 +282,7 @@ custom.py:29:22: ICN001 `matplotlib` should be imported as `mpl`
30 | import polars as ps # unconventional
31 | import pyarrow as arr # unconventional
|
= help: Alias `matplotlib` to `mpl`

custom.py:30:18: ICN001 `polars` should be imported as `pl`
|
Expand All @@ -266,6 +292,7 @@ custom.py:30:18: ICN001 `polars` should be imported as `pl`
| ^^ ICN001
31 | import pyarrow as arr # unconventional
|
= help: Alias `polars` to `pl`

custom.py:31:19: ICN001 `pyarrow` should be imported as `pa`
|
Expand All @@ -276,5 +303,6 @@ custom.py:31:19: ICN001 `pyarrow` should be imported as `pa`
32 |
33 | import altair as alt # conventional
|
= help: Alias `pyarrow` to `pa`


Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ defaults.py:3:8: ICN001 `altair` should be imported as `alt`
4 | import matplotlib.pyplot # unconventional
5 | import numpy # unconventional
|
= help: Alias `altair` to `alt`

defaults.py:4:8: ICN001 `matplotlib.pyplot` should be imported as `plt`
|
Expand All @@ -19,6 +20,7 @@ defaults.py:4:8: ICN001 `matplotlib.pyplot` should be imported as `plt`
5 | import numpy # unconventional
6 | import pandas # unconventional
|
= help: Alias `matplotlib.pyplot` to `plt`

defaults.py:5:8: ICN001 `numpy` should be imported as `np`
|
Expand All @@ -29,6 +31,7 @@ defaults.py:5:8: ICN001 `numpy` should be imported as `np`
6 | import pandas # unconventional
7 | import seaborn # unconventional
|
= help: Alias `numpy` to `np`

defaults.py:6:8: ICN001 `pandas` should be imported as `pd`
|
Expand All @@ -38,6 +41,7 @@ defaults.py:6:8: ICN001 `pandas` should be imported as `pd`
| ^^^^^^ ICN001
7 | import seaborn # unconventional
|
= help: Alias `pandas` to `pd`

defaults.py:7:8: ICN001 `seaborn` should be imported as `sns`
|
Expand All @@ -48,6 +52,7 @@ defaults.py:7:8: ICN001 `seaborn` should be imported as `sns`
8 |
9 | import altair as altr # unconventional
|
= help: Alias `seaborn` to `sns`

defaults.py:9:18: ICN001 `altair` should be imported as `alt`
|
Expand All @@ -58,6 +63,7 @@ defaults.py:9:18: ICN001 `altair` should be imported as `alt`
10 | import matplotlib.pyplot as plot # unconventional
11 | import numpy as nmp # unconventional
|
= help: Alias `altair` to `alt`

defaults.py:10:29: ICN001 `matplotlib.pyplot` should be imported as `plt`
|
Expand All @@ -67,6 +73,7 @@ defaults.py:10:29: ICN001 `matplotlib.pyplot` should be imported as `plt`
11 | import numpy as nmp # unconventional
12 | import pandas as pdas # unconventional
|
= help: Alias `matplotlib.pyplot` to `plt`

defaults.py:11:17: ICN001 `numpy` should be imported as `np`
|
Expand All @@ -77,6 +84,7 @@ defaults.py:11:17: ICN001 `numpy` should be imported as `np`
12 | import pandas as pdas # unconventional
13 | import seaborn as sbrn # unconventional
|
= help: Alias `numpy` to `np`

defaults.py:12:18: ICN001 `pandas` should be imported as `pd`
|
Expand All @@ -86,6 +94,7 @@ defaults.py:12:18: ICN001 `pandas` should be imported as `pd`
| ^^^^ ICN001
13 | import seaborn as sbrn # unconventional
|
= help: Alias `pandas` to `pd`

defaults.py:13:19: ICN001 `seaborn` should be imported as `sns`
|
Expand All @@ -96,5 +105,6 @@ defaults.py:13:19: ICN001 `seaborn` should be imported as `sns`
14 |
15 | import altair as alt # conventional
|
= help: Alias `seaborn` to `sns`


Loading

0 comments on commit b61cf0d

Please sign in to comment.