From 615587b6bf9661a45241689a14c2b15ad4d1a93d Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sat, 23 Apr 2022 18:09:57 +0200 Subject: [PATCH] Rename TypeVars (#1527) --- astroid/decorators.py | 24 ++++++++-------- astroid/nodes/node_classes.py | 4 +-- astroid/nodes/node_ng.py | 32 +++++++++++----------- astroid/nodes/scoped_nodes/scoped_nodes.py | 10 +++---- astroid/rebuilder.py | 18 ++++++------ 5 files changed, 44 insertions(+), 44 deletions(-) diff --git a/astroid/decorators.py b/astroid/decorators.py index ec16feadff..de1b8d212c 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -21,8 +21,8 @@ else: from typing_extensions import ParamSpec -R = TypeVar("R") -P = ParamSpec("P") +_R = TypeVar("_R") +_P = ParamSpec("_P") @wrapt.decorator @@ -153,7 +153,7 @@ def raise_if_nothing_inferred(func, instance, args, kwargs): def deprecate_default_argument_values( astroid_version: str = "3.0", **arguments: str - ) -> Callable[[Callable[P, R]], Callable[P, R]]: + ) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: """Decorator which emits a DeprecationWarning if any arguments specified are None or not passed at all. @@ -167,11 +167,11 @@ def deprecate_default_argument_values( # Decorator for DeprecationWarning: https://stackoverflow.com/a/49802489 # Typing of stacked decorators: https://stackoverflow.com/a/68290080 - def deco(func: Callable[P, R]) -> Callable[P, R]: + def deco(func: Callable[_P, _R]) -> Callable[_P, _R]: """Decorator function.""" @functools.wraps(func) - def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: + def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: """Emit DeprecationWarnings if conditions are met.""" keys = list(inspect.signature(func).parameters.keys()) @@ -212,7 +212,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: def deprecate_arguments( astroid_version: str = "3.0", **arguments: str - ) -> Callable[[Callable[P, R]], Callable[P, R]]: + ) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: """Decorator which emits a DeprecationWarning if any arguments specified are passed. @@ -223,9 +223,9 @@ def deprecate_arguments( the default one are enabled. """ - def deco(func: Callable[P, R]) -> Callable[P, R]: + def deco(func: Callable[_P, _R]) -> Callable[_P, _R]: @functools.wraps(func) - def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: + def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R: keys = list(inspect.signature(func).parameters.keys()) for arg, note in arguments.items(): @@ -252,10 +252,10 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R: def deprecate_default_argument_values( astroid_version: str = "3.0", **arguments: str - ) -> Callable[[Callable[P, R]], Callable[P, R]]: + ) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: """Passthrough decorator to improve performance if DeprecationWarnings are disabled.""" - def deco(func: Callable[P, R]) -> Callable[P, R]: + def deco(func: Callable[_P, _R]) -> Callable[_P, _R]: """Decorator function.""" return func @@ -263,10 +263,10 @@ def deco(func: Callable[P, R]) -> Callable[P, R]: def deprecate_arguments( astroid_version: str = "3.0", **arguments: str - ) -> Callable[[Callable[P, R]], Callable[P, R]]: + ) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]: """Passthrough decorator to improve performance if DeprecationWarnings are disabled.""" - def deco(func: Callable[P, R]) -> Callable[P, R]: + def deco(func: Callable[_P, _R]) -> Callable[_P, _R]: """Decorator function.""" return func diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index f7f6231d5f..0538d4d169 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -56,12 +56,12 @@ def _is_const(value): return isinstance(value, tuple(CONST_CLS)) -T_Nodes = TypeVar("T_Nodes", bound=NodeNG) +_NodesT = TypeVar("_NodesT", bound=NodeNG) AssignedStmtsPossibleNode = Union["List", "Tuple", "AssignName", "AssignAttr", None] AssignedStmtsCall = Callable[ [ - T_Nodes, + _NodesT, AssignedStmtsPossibleNode, Optional[InferenceContext], Optional[typing.List[int]], diff --git a/astroid/nodes/node_ng.py b/astroid/nodes/node_ng.py index 007221437b..ccd825909c 100644 --- a/astroid/nodes/node_ng.py +++ b/astroid/nodes/node_ng.py @@ -49,9 +49,9 @@ from astroid.decorators import cachedproperty as cached_property # Types for 'NodeNG.nodes_of_class()' -T_Nodes = TypeVar("T_Nodes", bound="NodeNG") -T_Nodes2 = TypeVar("T_Nodes2", bound="NodeNG") -T_Nodes3 = TypeVar("T_Nodes3", bound="NodeNG") +_NodesT = TypeVar("_NodesT", bound="NodeNG") +_NodesT2 = TypeVar("_NodesT2", bound="NodeNG") +_NodesT3 = TypeVar("_NodesT3", bound="NodeNG") SkipKlassT = Union[None, Type["NodeNG"], Tuple[Type["NodeNG"], ...]] @@ -513,45 +513,45 @@ def set_local(self, name, stmt): @overload def nodes_of_class( self, - klass: Type[T_Nodes], + klass: Type[_NodesT], skip_klass: SkipKlassT = None, - ) -> Iterator[T_Nodes]: + ) -> Iterator[_NodesT]: ... @overload def nodes_of_class( self, - klass: Tuple[Type[T_Nodes], Type[T_Nodes2]], + klass: Tuple[Type[_NodesT], Type[_NodesT2]], skip_klass: SkipKlassT = None, - ) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2]]: + ) -> Union[Iterator[_NodesT], Iterator[_NodesT2]]: ... @overload def nodes_of_class( self, - klass: Tuple[Type[T_Nodes], Type[T_Nodes2], Type[T_Nodes3]], + klass: Tuple[Type[_NodesT], Type[_NodesT2], Type[_NodesT3]], skip_klass: SkipKlassT = None, - ) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2], Iterator[T_Nodes3]]: + ) -> Union[Iterator[_NodesT], Iterator[_NodesT2], Iterator[_NodesT3]]: ... @overload def nodes_of_class( self, - klass: Tuple[Type[T_Nodes], ...], + klass: Tuple[Type[_NodesT], ...], skip_klass: SkipKlassT = None, - ) -> Iterator[T_Nodes]: + ) -> Iterator[_NodesT]: ... def nodes_of_class( # type: ignore[misc] # mypy doesn't correctly recognize the overloads self, klass: Union[ - Type[T_Nodes], - Tuple[Type[T_Nodes], Type[T_Nodes2]], - Tuple[Type[T_Nodes], Type[T_Nodes2], Type[T_Nodes3]], - Tuple[Type[T_Nodes], ...], + Type[_NodesT], + Tuple[Type[_NodesT], Type[_NodesT2]], + Tuple[Type[_NodesT], Type[_NodesT2], Type[_NodesT3]], + Tuple[Type[_NodesT], ...], ], skip_klass: SkipKlassT = None, - ) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2], Iterator[T_Nodes3]]: + ) -> Union[Iterator[_NodesT], Iterator[_NodesT2], Iterator[_NodesT3]]: """Get the nodes (including this one or below) of the given types. :param klass: The types of node to search for. diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index 7182bcc34c..0c03f205f1 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -66,7 +66,7 @@ {"classmethod", "staticmethod", "builtins.classmethod", "builtins.staticmethod"} ) -T = TypeVar("T") +_T = TypeVar("_T") def _c3_merge(sequences, cls, context): @@ -647,7 +647,7 @@ def bool_value(self, context=None): def get_children(self): yield from self.body - def frame(self: T, *, future: Literal[None, True] = None) -> T: + def frame(self: _T, *, future: Literal[None, True] = None) -> _T: """The node's frame node. A frame node is a :class:`Module`, :class:`FunctionDef`, @@ -1254,7 +1254,7 @@ def get_children(self): yield self.args yield self.body - def frame(self: T, *, future: Literal[None, True] = None) -> T: + def frame(self: _T, *, future: Literal[None, True] = None) -> _T: """The node's frame node. A frame node is a :class:`Module`, :class:`FunctionDef`, @@ -1800,7 +1800,7 @@ def scope_lookup(self, node, name, offset=0): return self, [frame] return super().scope_lookup(node, name, offset) - def frame(self: T, *, future: Literal[None, True] = None) -> T: + def frame(self: _T, *, future: Literal[None, True] = None) -> _T: """The node's frame node. A frame node is a :class:`Module`, :class:`FunctionDef`, @@ -3102,7 +3102,7 @@ def _get_assign_nodes(self): ) return list(itertools.chain.from_iterable(children_assign_nodes)) - def frame(self: T, *, future: Literal[None, True] = None) -> T: + def frame(self: _T, *, future: Literal[None, True] = None) -> _T: """The node's frame node. A frame node is a :class:`Module`, :class:`FunctionDef`, diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 614778a65b..77ab10524a 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -57,9 +57,9 @@ "ast.ClassDef", Union["ast.FunctionDef", "ast.AsyncFunctionDef"], ) -T_Function = TypeVar("T_Function", nodes.FunctionDef, nodes.AsyncFunctionDef) -T_For = TypeVar("T_For", nodes.For, nodes.AsyncFor) -T_With = TypeVar("T_With", nodes.With, nodes.AsyncWith) +_FunctionT = TypeVar("_FunctionT", nodes.FunctionDef, nodes.AsyncFunctionDef) +_ForT = TypeVar("_ForT", nodes.For, nodes.AsyncFor) +_WithT = TypeVar("_WithT", nodes.With, nodes.AsyncWith) NodesWithDocsType = Union[nodes.Module, nodes.ClassDef, nodes.FunctionDef] @@ -1179,8 +1179,8 @@ def _visit_for( ... def _visit_for( - self, cls: Type[T_For], node: Union["ast.For", "ast.AsyncFor"], parent: NodeNG - ) -> T_For: + self, cls: Type[_ForT], node: Union["ast.For", "ast.AsyncFor"], parent: NodeNG + ) -> _ForT: """visit a For node by returning a fresh instance of it""" col_offset = node.col_offset if IS_PYPY and not PY39_PLUS and isinstance(node, ast.AsyncFor) and self._data: @@ -1245,10 +1245,10 @@ def _visit_functiondef( def _visit_functiondef( self, - cls: Type[T_Function], + cls: Type[_FunctionT], node: Union["ast.FunctionDef", "ast.AsyncFunctionDef"], parent: NodeNG, - ) -> T_Function: + ) -> _FunctionT: """visit an FunctionDef node to become astroid""" self._global_names.append({}) node, doc_ast_node = self._get_doc(node) @@ -1905,10 +1905,10 @@ def _visit_with( def _visit_with( self, - cls: Type[T_With], + cls: Type[_WithT], node: Union["ast.With", "ast.AsyncWith"], parent: NodeNG, - ) -> T_With: + ) -> _WithT: col_offset = node.col_offset if IS_PYPY and not PY39_PLUS and isinstance(node, ast.AsyncWith) and self._data: # pylint: disable-next=unsubscriptable-object