1
- use ruff_python_ast:: { Alias , Stmt } ;
2
-
3
1
use ruff_diagnostics:: { Diagnostic , Violation } ;
4
2
use ruff_macros:: { derive_message_formats, violation} ;
3
+ use ruff_python_ast:: { Alias , Stmt } ;
5
4
use ruff_python_stdlib:: str:: { self } ;
6
5
use ruff_text_size:: Ranged ;
7
6
7
+ use crate :: checkers:: ast:: Checker ;
8
8
use crate :: rules:: pep8_naming:: helpers;
9
- use crate :: rules:: pep8_naming:: settings:: IgnoreNames ;
10
9
11
10
/// ## What it does
12
11
/// Checks for `CamelCase` imports that are aliased as acronyms.
@@ -23,6 +22,10 @@ use crate::rules::pep8_naming::settings::IgnoreNames;
23
22
/// Note that this rule is distinct from `camelcase-imported-as-constant`
24
23
/// to accommodate selective enforcement.
25
24
///
25
+ /// Also note that import aliases following an import convention according to the
26
+ /// [`lint.flake8-boolean-trap.extend-allowed-calls`] option are allowed.
27
+ ///
28
+ ///
26
29
/// ## Example
27
30
/// ```python
28
31
/// from example import MyClassName as MCN
@@ -34,6 +37,9 @@ use crate::rules::pep8_naming::settings::IgnoreNames;
34
37
/// ```
35
38
///
36
39
/// [PEP 8]: https://peps.python.org/pep-0008/
40
+ ///
41
+ /// ## Options
42
+ /// - `lint.flake8-import-conventions.banned-aliases`
37
43
#[ violation]
38
44
pub struct CamelcaseImportedAsAcronym {
39
45
name : String ,
@@ -54,17 +60,32 @@ pub(crate) fn camelcase_imported_as_acronym(
54
60
asname : & str ,
55
61
alias : & Alias ,
56
62
stmt : & Stmt ,
57
- ignore_names : & IgnoreNames ,
63
+ checker : & Checker ,
58
64
) -> Option < Diagnostic > {
59
65
if helpers:: is_camelcase ( name)
60
66
&& !str:: is_cased_lowercase ( asname)
61
67
&& str:: is_cased_uppercase ( asname)
62
68
&& helpers:: is_acronym ( name, asname)
63
69
{
70
+ let ignore_names = & checker. settings . pep8_naming . ignore_names ;
71
+
64
72
// Ignore any explicitly-allowed names.
65
73
if ignore_names. matches ( name) || ignore_names. matches ( asname) {
66
74
return None ;
67
75
}
76
+
77
+ // Ignore names that follow a community-agreed import convention.
78
+ if checker
79
+ . settings
80
+ . flake8_import_conventions
81
+ . aliases
82
+ . get ( & * alias. name )
83
+ . map ( String :: as_str)
84
+ == Some ( asname)
85
+ {
86
+ return None ;
87
+ }
88
+
68
89
let mut diagnostic = Diagnostic :: new (
69
90
CamelcaseImportedAsAcronym {
70
91
name : name. to_string ( ) ,
0 commit comments