ruff
ruff copied to clipboard
[pylint] Implement global-variable-undefined (W0601)
Add pylint rule global-variable-undefined (W0601)
See https://github.com/astral-sh/ruff/issues/970 for rules
Test Plan: cargo test
ruff-ecosystem results
Linter (stable)
ℹ️ ecosystem check detected linter changes. (+16 -0 violations, +0 -0 fixes in 2 projects; 42 projects unchanged)
apache/airflow (+15 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --no-preview --select ALL
+ airflow/migrations/versions/0104_2_3_0_migrate_rtif_to_use_run_id_and_map_index.py:50:5: PLW0601 Global variable `dag_run` is undefined at the module + airflow/migrations/versions/0104_2_3_0_migrate_rtif_to_use_run_id_and_map_index.py:50:5: PLW0601 Global variable `rendered_task_instance_fields` is undefined at the module + airflow/migrations/versions/0104_2_3_0_migrate_rtif_to_use_run_id_and_map_index.py:50:5: PLW0601 Global variable `task_instance` is undefined at the module + airflow/migrations/versions/0105_2_3_0_add_map_index_to_taskfail.py:49:5: PLW0601 Global variable `dag_run` is undefined at the module + airflow/migrations/versions/0105_2_3_0_add_map_index_to_taskfail.py:49:5: PLW0601 Global variable `task_fail` is undefined at the module + airflow/migrations/versions/0105_2_3_0_add_map_index_to_taskfail.py:49:5: PLW0601 Global variable `task_instance` is undefined at the module + airflow/serialization/serialized_objects.py:1750:5: PLW0601 Global variable `HAS_KUBERNETES` is undefined at the module + airflow/settings.py:272:5: PLW0601 Global variable `Session` is undefined at the module + airflow/settings.py:273:5: PLW0601 Global variable `engine` is undefined at the module + airflow/settings.py:399:5: PLW0601 Global variable `engine` is undefined at the module + airflow/settings.py:400:5: PLW0601 Global variable `Session` is undefined at the module + airflow/settings.py:442:5: PLW0601 Global variable `engine` is undefined at the module + tests/executors/test_executor_loader.py:56:9: PLW0601 Global variable `ExecutorLoader` is undefined at the module + tests/listeners/class_listener.py:39:13: PLW0601 Global variable `stopped_component` is undefined at the module + tests/listeners/class_listener.py:71:13: PLW0601 Global variable `stopped_component` is undefined at the module
milvus-io/pymilvus (+1 -0 violations, +0 -0 fixes)
+ _version_helper.py:92:9: PLW0601 Global variable `version` is undefined at the module
Changes by rule (1 rules affected)
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PLW0601 | 16 | 16 | 0 | 0 | 0 |
Linter (preview)
ℹ️ ecosystem check detected linter changes. (+16 -0 violations, +0 -0 fixes in 2 projects; 42 projects unchanged)
apache/airflow (+15 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview --select ALL
+ airflow/migrations/versions/0104_2_3_0_migrate_rtif_to_use_run_id_and_map_index.py:50:5: PLW0601 Global variable `dag_run` is undefined at the module + airflow/migrations/versions/0104_2_3_0_migrate_rtif_to_use_run_id_and_map_index.py:50:5: PLW0601 Global variable `rendered_task_instance_fields` is undefined at the module + airflow/migrations/versions/0104_2_3_0_migrate_rtif_to_use_run_id_and_map_index.py:50:5: PLW0601 Global variable `task_instance` is undefined at the module + airflow/migrations/versions/0105_2_3_0_add_map_index_to_taskfail.py:49:5: PLW0601 Global variable `dag_run` is undefined at the module + airflow/migrations/versions/0105_2_3_0_add_map_index_to_taskfail.py:49:5: PLW0601 Global variable `task_fail` is undefined at the module + airflow/migrations/versions/0105_2_3_0_add_map_index_to_taskfail.py:49:5: PLW0601 Global variable `task_instance` is undefined at the module + airflow/serialization/serialized_objects.py:1750:5: PLW0601 Global variable `HAS_KUBERNETES` is undefined at the module + airflow/settings.py:272:5: PLW0601 Global variable `Session` is undefined at the module + airflow/settings.py:273:5: PLW0601 Global variable `engine` is undefined at the module + airflow/settings.py:399:5: PLW0601 Global variable `engine` is undefined at the module + airflow/settings.py:400:5: PLW0601 Global variable `Session` is undefined at the module + airflow/settings.py:442:5: PLW0601 Global variable `engine` is undefined at the module + tests/executors/test_executor_loader.py:56:9: PLW0601 Global variable `ExecutorLoader` is undefined at the module + tests/listeners/class_listener.py:39:13: PLW0601 Global variable `stopped_component` is undefined at the module + tests/listeners/class_listener.py:71:13: PLW0601 Global variable `stopped_component` is undefined at the module
milvus-io/pymilvus (+1 -0 violations, +0 -0 fixes)
ruff check --no-cache --exit-zero --ignore RUF9 --output-format concise --preview
+ _version_helper.py:92:9: PLW0601 Global variable `version` is undefined at the module
Changes by rule (1 rules affected)
| code | total | + violation | - violation | + fix | - fix |
|---|---|---|---|---|---|
| PLW0601 | 16 | 16 | 0 | 0 | 0 |
Does this break on wildcard imports?
from os import *
def foo():
global system # W0601 should not be raised
system = lambda _: None
Does this break on wildcard imports?
from os import * def foo(): global system # W0601 should not be raised system = lambda _: None
Yes, it does. I have not found a function in ruff which resolves star-imports, e.g. there is F403 and F405 which are raised for star-imports and warn about "...unable to detect..." and "...may be undefined...".
I think there's a way to detect if there's a star import in which case you could avoid creating any diagnostics (to avoid false-positives).
The main open question to me are
- how this fits into ruff long term when Ruff catches more static analyzable errors (undefined variables in general)
- What's the motivation that this rule is specific to globals and doesn't apply to all variables.
@MichaReiser
Regarding star imports: when I do from os import *, pylint resolves this and e.g. has system in the namespace. Ruff is not able to do this (yet). For now, I simply left this out in this PR. What is possible though is to add something similar to F403/F405, i.e. if there is a start import, then raise "global variable undefined but maybe imported via star import". What's your preference?
@tibor-reiss my preference would be that we don't flag any variables if a file contains a star import. I think there's already precedence for this in Ruff but I would need to search it to. @charliermarsh should know where to find it.
@MichaReiser thanks for feedback, I added the check for star imports together with a new test file.