Skip to content

Commit 3097f0b

Browse files
authored
Cut ScopeConsumer (#9995)
This class is only used by the NamesConsumer. It adds an extra level of indirection, but without providing any clear benefit.
1 parent 74c49b2 commit 3097f0b

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

pylint/checkers/variables.py

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from collections import defaultdict
1515
from enum import Enum
1616
from functools import cached_property
17-
from typing import TYPE_CHECKING, NamedTuple
17+
from typing import TYPE_CHECKING
1818

1919
import astroid
2020
import astroid.exceptions
@@ -33,7 +33,6 @@
3333

3434
if TYPE_CHECKING:
3535
from collections.abc import Generator, Iterable, Iterator
36-
from typing import Any
3736

3837
from astroid.nodes import _base_nodes
3938
from astroid.typing import InferenceResult
@@ -475,72 +474,49 @@ def _has_locals_call_after_node(stmt: nodes.NodeNG, scope: nodes.FunctionDef) ->
475474
}
476475

477476

478-
class ScopeConsumer(NamedTuple):
479-
"""Store nodes and their consumption states."""
477+
class NamesConsumer:
478+
"""A simple class to handle consumed, to consume and scope type info of node locals."""
479+
480+
node: nodes.NodeNG
481+
scope_type: str
480482

481483
to_consume: Consumption
482484
consumed: Consumption
483485
consumed_uncertain: Consumption
484-
scope_type: str
486+
"""Retrieves nodes filtered out by get_next_to_consume() that may not
487+
have executed.
485488
489+
These include nodes such as statements in except blocks, or statements
490+
in try blocks (when evaluating their corresponding except and finally
491+
blocks). Checkers that want to treat the statements as executed
492+
(e.g. for unused-variable) may need to add them back.
493+
"""
486494

487-
class NamesConsumer:
488-
"""A simple class to handle consumed, to consume and scope type info of node locals."""
489-
490-
def __init__(self, node: nodes.NodeNG, scope_type: str) -> None:
491-
self._atomic = ScopeConsumer(
492-
copy.copy(node.locals),
493-
{},
494-
defaultdict(list),
495-
scope_type,
496-
)
495+
def __init__(self, node: nodes.NodeNG, scope_type: str):
497496
self.node = node
497+
self.scope_type = scope_type
498+
499+
self.to_consume = copy.copy(node.locals)
500+
self.consumed = {}
501+
self.consumed_uncertain = defaultdict(list)
502+
498503
self.names_under_always_false_test: set[str] = set()
499504
self.names_defined_under_one_branch_only: set[str] = set()
500505

501506
def __repr__(self) -> str:
502-
_to_consumes = [f"{k}->{v}" for k, v in self._atomic.to_consume.items()]
503-
_consumed = [f"{k}->{v}" for k, v in self._atomic.consumed.items()]
504-
_consumed_uncertain = [
505-
f"{k}->{v}" for k, v in self._atomic.consumed_uncertain.items()
506-
]
507+
_to_consumes = [f"{k}->{v}" for k, v in self.to_consume.items()]
508+
_consumed = [f"{k}->{v}" for k, v in self.consumed.items()]
509+
_consumed_uncertain = [f"{k}->{v}" for k, v in self.consumed_uncertain.items()]
507510
to_consumes = ", ".join(_to_consumes)
508511
consumed = ", ".join(_consumed)
509512
consumed_uncertain = ", ".join(_consumed_uncertain)
510513
return f"""
511514
to_consume : {to_consumes}
512515
consumed : {consumed}
513516
consumed_uncertain: {consumed_uncertain}
514-
scope_type : {self._atomic.scope_type}
517+
scope_type : {self.scope_type}
515518
"""
516519

517-
def __iter__(self) -> Iterator[Any]:
518-
return iter(self._atomic)
519-
520-
@property
521-
def to_consume(self) -> Consumption:
522-
return self._atomic.to_consume
523-
524-
@property
525-
def consumed(self) -> Consumption:
526-
return self._atomic.consumed
527-
528-
@property
529-
def consumed_uncertain(self) -> Consumption:
530-
"""Retrieves nodes filtered out by get_next_to_consume() that may not
531-
have executed.
532-
533-
These include nodes such as statements in except blocks, or statements
534-
in try blocks (when evaluating their corresponding except and finally
535-
blocks). Checkers that want to treat the statements as executed
536-
(e.g. for unused-variable) may need to add them back.
537-
"""
538-
return self._atomic.consumed_uncertain
539-
540-
@property
541-
def scope_type(self) -> str:
542-
return self._atomic.scope_type
543-
544520
def mark_as_consumed(self, name: str, consumed_nodes: list[nodes.NodeNG]) -> None:
545521
"""Mark the given nodes as consumed for the name.
546522
@@ -3314,7 +3290,8 @@ def _check_classdef_metaclasses(
33143290
name = METACLASS_NAME_TRANSFORMS.get(name, name)
33153291
if name:
33163292
# check enclosing scopes starting from most local
3317-
for scope_locals, _, _, _ in self._to_consume[::-1]:
3293+
for to_consume in self._to_consume[::-1]:
3294+
scope_locals = to_consume.to_consume
33183295
found_nodes = scope_locals.get(name, [])
33193296
for found_node in found_nodes:
33203297
if found_node.lineno <= klass.lineno:

0 commit comments

Comments
 (0)