mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 13:33:50 +00:00
![]() ## Summary This PR fixes a small quirk in the semantic model. Typically, when we see an import, like `import foo`, we create a `BindingKind::Importation` for it. However, if `foo` has been declared as a `global`, then we propagate the kind forward. So given: ```python global foo import foo ``` We'd create two bindings for `foo`, both with type `global`. This was originally borrowed from Pyflakes, and it exists to help avoid false-positives like: ```python def f(): global foo # Don't mark `foo` as "assigned but unused"! It's a global! foo = 1 ``` This PR removes that behavior, and instead tracks "Does this binding refer to a global?" as a flag. This is much cleaner, since it means we don't "lose" the identity of various bindings. As a very strange example of why this matters, consider: ```python def foo(): global Member from module import Member x: Member = 1 ``` `Member` is only used in a typing context, so we should flag it and say "move it to a `TYPE_CHECKING` block". However, when we go to analyze `from module import Member`, it has `BindingKind::Global`. So we don't even know that it's an import! |
||
---|---|---|
.. | ||
flake8_to_ruff | ||
ruff | ||
ruff_benchmark | ||
ruff_cache | ||
ruff_cli | ||
ruff_dev | ||
ruff_diagnostics | ||
ruff_formatter | ||
ruff_index | ||
ruff_macros | ||
ruff_python_ast | ||
ruff_python_formatter | ||
ruff_python_semantic | ||
ruff_python_stdlib | ||
ruff_python_whitespace | ||
ruff_rustpython | ||
ruff_testing_macros | ||
ruff_textwrap | ||
ruff_wasm |