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

Accept non-aliased (but correct) import in unconventional-import-alias #10729

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def unconventional_aliases():
import tkinter as tkr
import networkx as nxy


def conventional_aliases():
import altair as alt
import matplotlib.pyplot as plt
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def no_alias():
from django.conf import settings


def conventional_alias():
from django.conf import settings as settings


def unconventional_alias():
from django.conf import settings as s
28 changes: 25 additions & 3 deletions crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mod tests {

#[test]
fn custom() -> Result<()> {
let mut aliases = super::settings::default_aliases();
let mut aliases = default_aliases();
aliases.extend(FxHashMap::from_iter([
("dask.array".to_string(), "da".to_string()),
("dask.dataframe".to_string(), "dd".to_string()),
Expand Down Expand Up @@ -126,7 +126,7 @@ mod tests {

#[test]
fn override_defaults() -> Result<()> {
let mut aliases = super::settings::default_aliases();
let mut aliases = default_aliases();
aliases.extend(FxHashMap::from_iter([(
"numpy".to_string(),
"nmp".to_string(),
Expand All @@ -149,7 +149,7 @@ mod tests {

#[test]
fn from_imports() -> Result<()> {
let mut aliases = super::settings::default_aliases();
let mut aliases = default_aliases();
aliases.extend(FxHashMap::from_iter([
("xml.dom.minidom".to_string(), "md".to_string()),
(
Expand Down Expand Up @@ -182,4 +182,26 @@ mod tests {
assert_messages!(diagnostics);
Ok(())
}

#[test]
fn same_name() -> Result<()> {
let mut aliases = default_aliases();
aliases.extend(FxHashMap::from_iter([(
"django.conf.settings".to_string(),
"settings".to_string(),
)]));
let diagnostics = test_path(
Path::new("flake8_import_conventions/same_name.py"),
&LinterSettings {
flake8_import_conventions: super::settings::Settings {
aliases,
banned_aliases: FxHashMap::default(),
banned_from: FxHashSet::default(),
},
..LinterSettings::for_rule(Rule::UnconventionalImportAlias)
},
)?;
assert_messages!(diagnostics);
Ok(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub(crate) fn unconventional_import_alias(
let expected_alias = conventions.get(qualified_name.as_str())?;

let name = binding.name(checker.locator());
if binding.is_alias() && name == expected_alias {
if name == expected_alias {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(This is the fix.)

return None;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,16 +256,14 @@ defaults.py:21:23: ICN001 [*] `tkinter` should be imported as `tk`
21 |+ import tkinter as tk
22 22 | import networkx as nxy
23 23 |
24 24 | def conventional_aliases():
24 24 |

defaults.py:22:24: ICN001 [*] `networkx` should be imported as `nx`
|
20 | import seaborn as sbrn
21 | import tkinter as tkr
22 | import networkx as nxy
| ^^^ ICN001
23 |
24 | def conventional_aliases():
|
= help: Alias `networkx` to `nx`

Expand All @@ -276,7 +274,5 @@ defaults.py:22:24: ICN001 [*] `networkx` should be imported as `nx`
22 |- import networkx as nxy
22 |+ import networkx as nx
23 23 |
24 24 | def conventional_aliases():
25 25 | import altair as alt


24 24 |
25 25 | def conventional_aliases():
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
source: crates/ruff_linter/src/rules/flake8_import_conventions/mod.rs
---
same_name.py:10:41: ICN001 [*] `django.conf.settings` should be imported as `settings`
|
9 | def unconventional_alias():
10 | from django.conf import settings as s
| ^ ICN001
|
= help: Alias `django.conf.settings` to `settings`

ℹ Unsafe fix
7 7 |
8 8 |
9 9 | def unconventional_alias():
10 |- from django.conf import settings as s
10 |+ from django.conf import settings as settings