Commit f56e41a
authored
Fix undefined-variable false positive with metaclass in nested class (#10853)
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 673898f commit f56e41a
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 | |
|---|---|---|---|
| |||
3387 | 3387 | | |
3388 | 3388 | | |
3389 | 3389 | | |
3390 | | - | |
| 3390 | + | |
3391 | 3391 | | |
3392 | 3392 | | |
3393 | 3393 | | |
3394 | 3394 | | |
3395 | 3395 | | |
3396 | | - | |
3397 | | - | |
3398 | | - | |
3399 | | - | |
| 3396 | + | |
| 3397 | + | |
| 3398 | + | |
| 3399 | + | |
| 3400 | + | |
| 3401 | + | |
3400 | 3402 | | |
3401 | 3403 | | |
3402 | 3404 | | |
3403 | 3405 | | |
3404 | 3406 | | |
3405 | | - | |
| 3407 | + | |
3406 | 3408 | | |
3407 | 3409 | | |
3408 | 3410 | | |
3409 | 3411 | | |
3410 | | - | |
| 3412 | + | |
3411 | 3413 | | |
3412 | 3414 | | |
3413 | 3415 | | |
| |||
3433 | 3435 | | |
3434 | 3436 | | |
3435 | 3437 | | |
3436 | | - | |
| 3438 | + | |
3437 | 3439 | | |
3438 | 3440 | | |
3439 | 3441 | | |
| |||
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