-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Apply variable name regex to module-level names that are reassigned #3585 #10212
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
Changes from 4 commits
91c399d
cd354ae
854141e
5d113f2
babae7b
93b8b97
18609e0
45d80ed
dc7f7e7
b2b2afb
aff9dc5
2938d11
ba3f0f5
c4e7b2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -146,6 +146,7 @@ hmac | |
| html | ||
| idgeneratormixin | ||
| ifexpr | ||
| igetattr | ||
| importedname | ||
| importfrom | ||
| importnode | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| `invalid-name` now distinguishes module-level names that are assigned only once | ||
| from those that are reassigned and now applies `--variable-rgx` to the latter. | ||
|
|
||
| Also, `invalid-name` is triggered for module-level names for additional types | ||
| (e.g. lists and sets). | ||
|
|
||
| Closes #3585 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1865,6 +1865,18 @@ def is_sys_guard(node: nodes.If) -> bool: | |
| return False | ||
|
|
||
|
|
||
| def is_reassigned_before_current(node: nodes.NodeNG, varname: str) -> bool: | ||
| """Check if the given variable name is reassigned in the same scope before the | ||
| current node. | ||
| """ | ||
| return any( | ||
| a.name == varname and a.lineno < node.lineno | ||
| for a in node.scope().nodes_of_class( | ||
| (nodes.AssignName, nodes.ClassDef, nodes.FunctionDef) | ||
| ) | ||
| ) | ||
|
|
||
|
|
||
| def is_reassigned_after_current(node: nodes.NodeNG, varname: str) -> bool: | ||
| """Check if the given variable name is reassigned in the same scope after the | ||
| current node. | ||
|
|
@@ -2135,7 +2147,13 @@ def is_augmented_assign(node: nodes.Assign) -> tuple[bool, str]: | |
| binop.op in COMMUTATIVE_OPERATORS | ||
| and _is_target_name_in_binop_side(target, binop.right) | ||
| ): | ||
| inferred_left = safe_infer(binop.left) | ||
| if isinstance(binop.left, nodes.Const): | ||
| # This bizarrely became necessary after an unrelated call to igetattr(). | ||
| # Seems like a code smell uncovered in #10212. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure if I understand what the code smell is here, would you mind explaining a little more ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fact that I called I'd leave a more helpful comment if I could think of one, I'm definitely open to suggestions! Wanted to at least leave some kind of breadcrumb for the next developer. |
||
| # tuple(node.frame().igetattr(node.name)) | ||
| inferred_left = binop.left | ||
| else: | ||
| inferred_left = safe_infer(binop.left) | ||
| if isinstance(inferred_left, nodes.Const) and isinstance( | ||
| inferred_left.value, int | ||
| ): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be advertised as a breaking change for 4.0, it make sense to do it this way but pylint have been doing it a certain way for so long that "module level as constant" became a convention of a kind. (
LOGGERvsloggerwas not settled last time I checked, admittedly a long time ago).