Commit 5dd2a04
committed
Fix undefined-variable false positive with metaclass in nested class
When an imported name was used as a `metaclass=` argument inside a
nested class within a method, pylint incorrectly flagged subsequent
uses of that name at module level as `undefined-variable`.
Root cause: `_check_classdef_metaclasses()` found the name in
enclosing scopes and `_check_metaclasses()` popped it directly from
`to_consume`, making it completely unresolvable. Subsequent references
to the same name triggered `undefined-variable`.
Fix: use `NamesConsumer.mark_as_consumed()` instead of raw dict pop.
This properly moves the name from `to_consume` to `consumed`, so:
1. The import is not flagged as unused (original intent)
2. Later references find the name in `consumed` and resolve correctly
Closes #108231 parent 4fb4079 commit 5dd2a04
3 files changed
Lines changed: 34 additions & 8 deletions
File tree
- doc/whatsnew/fragments
- pylint/checkers
- tests/functional/u/undefined
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3400 | 3400 | | |
3401 | 3401 | | |
3402 | 3402 | | |
3403 | | - | |
| 3403 | + | |
3404 | 3404 | | |
3405 | 3405 | | |
3406 | 3406 | | |
3407 | 3407 | | |
3408 | 3408 | | |
3409 | | - | |
3410 | | - | |
3411 | | - | |
3412 | | - | |
| 3409 | + | |
| 3410 | + | |
| 3411 | + | |
| 3412 | + | |
| 3413 | + | |
| 3414 | + | |
3413 | 3415 | | |
3414 | 3416 | | |
3415 | 3417 | | |
3416 | 3418 | | |
3417 | 3419 | | |
3418 | | - | |
| 3420 | + | |
3419 | 3421 | | |
3420 | 3422 | | |
3421 | 3423 | | |
3422 | 3424 | | |
3423 | | - | |
| 3425 | + | |
3424 | 3426 | | |
3425 | 3427 | | |
3426 | 3428 | | |
| |||
3446 | 3448 | | |
3447 | 3449 | | |
3448 | 3450 | | |
3449 | | - | |
| 3451 | + | |
3450 | 3452 | | |
3451 | 3453 | | |
3452 | 3454 | | |
| |||
Lines changed: 20 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
0 commit comments