Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[pyupgrade] Allow shadowing non-builtin bindings (UP029)#22749

Merged
ntBre merged 2 commits intomainfrom
brent/up029
Jan 20, 2026
Merged

[pyupgrade] Allow shadowing non-builtin bindings (UP029)#22749
ntBre merged 2 commits intomainfrom
brent/up029

Conversation

@ntBre
Copy link
Contributor

@ntBre ntBre commented Jan 20, 2026

Summary

I thought the fix unsafety example in the rule docs looked a bit suspicious
while I was going through more potential default rules today (playground):

def str(x):
    return x

from builtins import str

str(1)  # `"1"` with the import, `1` without

Changing the behavior in this way seemed to go beyond fix unsafety and into bug
territory. Sure enough, there was an existing bug report in #16182.

This PR fixes #16182 (and the fix safety example) by checking that the builtin
import that the rule flags is actually shadowing a builtin binding. I also left
an exception for from builtins import *, but we could consider ignoring that
case too.

I initially tried reusing SemanticModel::resolve_name and only_binding, but
they are specific to ExprNames because that seems to be all that is inserted
into the SemanticModel::resolved_names map.

Test Plan

New tests based on #16182 and the fix safety docs

Summary
--

I thought the fix unsafety example in the rule docs looked a bit suspicious
while I was going through more potential default rules today:

```py
def str(x):
    return x

from builtins import str

str(1)  # `"1"` with the import, `1` without
```

Changing the behavior in this way seemed to go beyond fix unsafety and into bug
territory. Sure enough, there was an existing bug report in #16182.

This PR fixes #16182 (and the fix safety example) by checking that the builtin
import that the rule flags is actually shadowing a builtin binding. I also left
an exception for `from builtins import *`, but we could consider ignoring that
case too.

I initially tried reusing `SemanticModel::resolve_name` and `only_binding`, but
they are specific to `ExprName`s because that seems to be all that is inserted
into the `SemanticModel::resolved_names` map.

Test Plan
--

New tests based on #16182 and the fix safety docs
@ntBre ntBre added bug Something isn't working rule Implementing or modifying a lint rule labels Jan 20, 2026
@ntBre ntBre requested a review from MichaReiser January 20, 2026 00:45
@ntBre ntBre marked this pull request as ready for review January 20, 2026 00:45
@astral-sh-bot
Copy link

astral-sh-bot bot commented Jan 20, 2026

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@ntBre ntBre merged commit 83ca286 into main Jan 20, 2026
41 checks passed
@ntBre ntBre deleted the brent/up029 branch January 20, 2026 13:52
carljm added a commit that referenced this pull request Jan 20, 2026
* main: (149 commits)
  Preserve required parentheses in lambda bodies (#22747)
  [`flake8-simplify`] Make fix unsafe if it deletes comments (`SIM911`) (#22661)
  [`refurb`] Make fix unsafe if it deletes comments (`FURB145`) (#22670)
  [`ruff`] Make fix unsafe if it deletes comments (`RUF020`) (#22664)
  [ty] Add README for `ty_completion_bench` CLI tool
  [ty] Update completion eval
  [ty] Collect completions into a max-heap
  [ty] Truncate imports and qualifications derived from completions
  [ty] Get rid of high-level completion deduplication
  [ty] Refactor how we sort completions
  [ty] Optimize collection of "all symbols"
  [ty] Speed up completions by tweaking sorts
  [ty] Add new `ty_completion_bench` for ad hoc benchmarking
  gitignore: ignore scratch directories in completion eval truth directory
  [`pyupgrade`] Allow shadowing non-builtin bindings (`UP029`) (#22749)
  [ty] Emit invalid type form for stringified annotations (#22752)
  [ty] Allow `if type(x) is Y` narrowing for types other than class-literal types (#22729)
  [ty] Add basic support for overloads in `ParamSpec` (#21946)
  [`ruff`] Make fix unsafe if it deletes comments (`RUF019`) (#22663)
  [`flake8-bugbear`] Make fix unsafe if it deletes comments (`B014`) (#22659)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working rule Implementing or modifying a lint rule

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UP029 triggers if import redefines previous import

2 participants