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

PyLint false positive PLW0602 when del a global #10397

Closed
ilius opened this issue Mar 13, 2024 · 2 comments · Fixed by #10415
Closed

PyLint false positive PLW0602 when del a global #10397

ilius opened this issue Mar 13, 2024 · 2 comments · Fixed by #10415
Assignees
Labels
bug Something isn't working

Comments

@ilius
Copy link

ilius commented Mar 13, 2024

Sample code:

test_global_var = None


def delete_test_global_var():
	global test_global_var
	if "test_global_var" in globals():
		del test_global_var

Command: ruff check --preview --isolated --select PL del-global.py
Output:

del-global.py:5:9: PLW0603 Using the global statement to update `test_global_var` is discouraged
  |
4 | def delete_test_global_var():
5 |     global test_global_var
  |            ^^^^^^^^^^^^^^^ PLW0603
6 |     if "test_global_var" in globals():
7 |         del test_global_var
  |

del-global.py:5:9: PLW0602 Using global for `test_global_var` but no assignment is done
  |
4 | def delete_test_global_var():
5 |     global test_global_var
  |            ^^^^^^^^^^^^^^^ PLW0602
6 |     if "test_global_var" in globals():
7 |         del test_global_var
  |

Found 2 errors.

Version:

$ ruff version
ruff 0.3.2
@charliermarsh charliermarsh added the bug Something isn't working label Mar 14, 2024
@charliermarsh
Copy link
Member

Thanks!

@charliermarsh
Copy link
Member

This only occurs when the del is in a conditional.

@charliermarsh charliermarsh self-assigned this Mar 14, 2024
charliermarsh added a commit that referenced this issue Mar 15, 2024
## Summary

Given `del X`, we'll typically add a `BindingKind::Deletion` to `X` to
shadow the current binding. However, if the deletion is inside of a
conditional operation, we _won't_, as in:

```python
def f():
    global X

    if X > 0:
        del X
```

We will, however, track it as a reference to the binding. This PR adds
the expression context to those resolved references, so that we can
detect that the `X` in `global X` was "assigned to".

Closes #10397.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants