Skip to content

Commit 615587b

Browse files
authored
Rename TypeVars (#1527)
1 parent b6eb380 commit 615587b

File tree

5 files changed

+44
-44
lines changed

5 files changed

+44
-44
lines changed

astroid/decorators.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
else:
2222
from typing_extensions import ParamSpec
2323

24-
R = TypeVar("R")
25-
P = ParamSpec("P")
24+
_R = TypeVar("_R")
25+
_P = ParamSpec("_P")
2626

2727

2828
@wrapt.decorator
@@ -153,7 +153,7 @@ def raise_if_nothing_inferred(func, instance, args, kwargs):
153153

154154
def deprecate_default_argument_values(
155155
astroid_version: str = "3.0", **arguments: str
156-
) -> Callable[[Callable[P, R]], Callable[P, R]]:
156+
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]:
157157
"""Decorator which emits a DeprecationWarning if any arguments specified
158158
are None or not passed at all.
159159
@@ -167,11 +167,11 @@ def deprecate_default_argument_values(
167167
# Decorator for DeprecationWarning: https://stackoverflow.com/a/49802489
168168
# Typing of stacked decorators: https://stackoverflow.com/a/68290080
169169

170-
def deco(func: Callable[P, R]) -> Callable[P, R]:
170+
def deco(func: Callable[_P, _R]) -> Callable[_P, _R]:
171171
"""Decorator function."""
172172

173173
@functools.wraps(func)
174-
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
174+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
175175
"""Emit DeprecationWarnings if conditions are met."""
176176

177177
keys = list(inspect.signature(func).parameters.keys())
@@ -212,7 +212,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
212212

213213
def deprecate_arguments(
214214
astroid_version: str = "3.0", **arguments: str
215-
) -> Callable[[Callable[P, R]], Callable[P, R]]:
215+
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]:
216216
"""Decorator which emits a DeprecationWarning if any arguments specified
217217
are passed.
218218
@@ -223,9 +223,9 @@ def deprecate_arguments(
223223
the default one are enabled.
224224
"""
225225

226-
def deco(func: Callable[P, R]) -> Callable[P, R]:
226+
def deco(func: Callable[_P, _R]) -> Callable[_P, _R]:
227227
@functools.wraps(func)
228-
def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
228+
def wrapper(*args: _P.args, **kwargs: _P.kwargs) -> _R:
229229

230230
keys = list(inspect.signature(func).parameters.keys())
231231
for arg, note in arguments.items():
@@ -252,21 +252,21 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> R:
252252

253253
def deprecate_default_argument_values(
254254
astroid_version: str = "3.0", **arguments: str
255-
) -> Callable[[Callable[P, R]], Callable[P, R]]:
255+
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]:
256256
"""Passthrough decorator to improve performance if DeprecationWarnings are disabled."""
257257

258-
def deco(func: Callable[P, R]) -> Callable[P, R]:
258+
def deco(func: Callable[_P, _R]) -> Callable[_P, _R]:
259259
"""Decorator function."""
260260
return func
261261

262262
return deco
263263

264264
def deprecate_arguments(
265265
astroid_version: str = "3.0", **arguments: str
266-
) -> Callable[[Callable[P, R]], Callable[P, R]]:
266+
) -> Callable[[Callable[_P, _R]], Callable[_P, _R]]:
267267
"""Passthrough decorator to improve performance if DeprecationWarnings are disabled."""
268268

269-
def deco(func: Callable[P, R]) -> Callable[P, R]:
269+
def deco(func: Callable[_P, _R]) -> Callable[_P, _R]:
270270
"""Decorator function."""
271271
return func
272272

astroid/nodes/node_classes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def _is_const(value):
5656
return isinstance(value, tuple(CONST_CLS))
5757

5858

59-
T_Nodes = TypeVar("T_Nodes", bound=NodeNG)
59+
_NodesT = TypeVar("_NodesT", bound=NodeNG)
6060

6161
AssignedStmtsPossibleNode = Union["List", "Tuple", "AssignName", "AssignAttr", None]
6262
AssignedStmtsCall = Callable[
6363
[
64-
T_Nodes,
64+
_NodesT,
6565
AssignedStmtsPossibleNode,
6666
Optional[InferenceContext],
6767
Optional[typing.List[int]],

astroid/nodes/node_ng.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949
from astroid.decorators import cachedproperty as cached_property
5050

5151
# Types for 'NodeNG.nodes_of_class()'
52-
T_Nodes = TypeVar("T_Nodes", bound="NodeNG")
53-
T_Nodes2 = TypeVar("T_Nodes2", bound="NodeNG")
54-
T_Nodes3 = TypeVar("T_Nodes3", bound="NodeNG")
52+
_NodesT = TypeVar("_NodesT", bound="NodeNG")
53+
_NodesT2 = TypeVar("_NodesT2", bound="NodeNG")
54+
_NodesT3 = TypeVar("_NodesT3", bound="NodeNG")
5555
SkipKlassT = Union[None, Type["NodeNG"], Tuple[Type["NodeNG"], ...]]
5656

5757

@@ -513,45 +513,45 @@ def set_local(self, name, stmt):
513513
@overload
514514
def nodes_of_class(
515515
self,
516-
klass: Type[T_Nodes],
516+
klass: Type[_NodesT],
517517
skip_klass: SkipKlassT = None,
518-
) -> Iterator[T_Nodes]:
518+
) -> Iterator[_NodesT]:
519519
...
520520

521521
@overload
522522
def nodes_of_class(
523523
self,
524-
klass: Tuple[Type[T_Nodes], Type[T_Nodes2]],
524+
klass: Tuple[Type[_NodesT], Type[_NodesT2]],
525525
skip_klass: SkipKlassT = None,
526-
) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2]]:
526+
) -> Union[Iterator[_NodesT], Iterator[_NodesT2]]:
527527
...
528528

529529
@overload
530530
def nodes_of_class(
531531
self,
532-
klass: Tuple[Type[T_Nodes], Type[T_Nodes2], Type[T_Nodes3]],
532+
klass: Tuple[Type[_NodesT], Type[_NodesT2], Type[_NodesT3]],
533533
skip_klass: SkipKlassT = None,
534-
) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2], Iterator[T_Nodes3]]:
534+
) -> Union[Iterator[_NodesT], Iterator[_NodesT2], Iterator[_NodesT3]]:
535535
...
536536

537537
@overload
538538
def nodes_of_class(
539539
self,
540-
klass: Tuple[Type[T_Nodes], ...],
540+
klass: Tuple[Type[_NodesT], ...],
541541
skip_klass: SkipKlassT = None,
542-
) -> Iterator[T_Nodes]:
542+
) -> Iterator[_NodesT]:
543543
...
544544

545545
def nodes_of_class( # type: ignore[misc] # mypy doesn't correctly recognize the overloads
546546
self,
547547
klass: Union[
548-
Type[T_Nodes],
549-
Tuple[Type[T_Nodes], Type[T_Nodes2]],
550-
Tuple[Type[T_Nodes], Type[T_Nodes2], Type[T_Nodes3]],
551-
Tuple[Type[T_Nodes], ...],
548+
Type[_NodesT],
549+
Tuple[Type[_NodesT], Type[_NodesT2]],
550+
Tuple[Type[_NodesT], Type[_NodesT2], Type[_NodesT3]],
551+
Tuple[Type[_NodesT], ...],
552552
],
553553
skip_klass: SkipKlassT = None,
554-
) -> Union[Iterator[T_Nodes], Iterator[T_Nodes2], Iterator[T_Nodes3]]:
554+
) -> Union[Iterator[_NodesT], Iterator[_NodesT2], Iterator[_NodesT3]]:
555555
"""Get the nodes (including this one or below) of the given types.
556556
557557
:param klass: The types of node to search for.

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
{"classmethod", "staticmethod", "builtins.classmethod", "builtins.staticmethod"}
6767
)
6868

69-
T = TypeVar("T")
69+
_T = TypeVar("_T")
7070

7171

7272
def _c3_merge(sequences, cls, context):
@@ -647,7 +647,7 @@ def bool_value(self, context=None):
647647
def get_children(self):
648648
yield from self.body
649649

650-
def frame(self: T, *, future: Literal[None, True] = None) -> T:
650+
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
651651
"""The node's frame node.
652652
653653
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -1254,7 +1254,7 @@ def get_children(self):
12541254
yield self.args
12551255
yield self.body
12561256

1257-
def frame(self: T, *, future: Literal[None, True] = None) -> T:
1257+
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
12581258
"""The node's frame node.
12591259
12601260
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -1800,7 +1800,7 @@ def scope_lookup(self, node, name, offset=0):
18001800
return self, [frame]
18011801
return super().scope_lookup(node, name, offset)
18021802

1803-
def frame(self: T, *, future: Literal[None, True] = None) -> T:
1803+
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
18041804
"""The node's frame node.
18051805
18061806
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -3102,7 +3102,7 @@ def _get_assign_nodes(self):
31023102
)
31033103
return list(itertools.chain.from_iterable(children_assign_nodes))
31043104

3105-
def frame(self: T, *, future: Literal[None, True] = None) -> T:
3105+
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
31063106
"""The node's frame node.
31073107
31083108
A frame node is a :class:`Module`, :class:`FunctionDef`,

astroid/rebuilder.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
"ast.ClassDef",
5858
Union["ast.FunctionDef", "ast.AsyncFunctionDef"],
5959
)
60-
T_Function = TypeVar("T_Function", nodes.FunctionDef, nodes.AsyncFunctionDef)
61-
T_For = TypeVar("T_For", nodes.For, nodes.AsyncFor)
62-
T_With = TypeVar("T_With", nodes.With, nodes.AsyncWith)
60+
_FunctionT = TypeVar("_FunctionT", nodes.FunctionDef, nodes.AsyncFunctionDef)
61+
_ForT = TypeVar("_ForT", nodes.For, nodes.AsyncFor)
62+
_WithT = TypeVar("_WithT", nodes.With, nodes.AsyncWith)
6363
NodesWithDocsType = Union[nodes.Module, nodes.ClassDef, nodes.FunctionDef]
6464

6565

@@ -1179,8 +1179,8 @@ def _visit_for(
11791179
...
11801180

11811181
def _visit_for(
1182-
self, cls: Type[T_For], node: Union["ast.For", "ast.AsyncFor"], parent: NodeNG
1183-
) -> T_For:
1182+
self, cls: Type[_ForT], node: Union["ast.For", "ast.AsyncFor"], parent: NodeNG
1183+
) -> _ForT:
11841184
"""visit a For node by returning a fresh instance of it"""
11851185
col_offset = node.col_offset
11861186
if IS_PYPY and not PY39_PLUS and isinstance(node, ast.AsyncFor) and self._data:
@@ -1245,10 +1245,10 @@ def _visit_functiondef(
12451245

12461246
def _visit_functiondef(
12471247
self,
1248-
cls: Type[T_Function],
1248+
cls: Type[_FunctionT],
12491249
node: Union["ast.FunctionDef", "ast.AsyncFunctionDef"],
12501250
parent: NodeNG,
1251-
) -> T_Function:
1251+
) -> _FunctionT:
12521252
"""visit an FunctionDef node to become astroid"""
12531253
self._global_names.append({})
12541254
node, doc_ast_node = self._get_doc(node)
@@ -1905,10 +1905,10 @@ def _visit_with(
19051905

19061906
def _visit_with(
19071907
self,
1908-
cls: Type[T_With],
1908+
cls: Type[_WithT],
19091909
node: Union["ast.With", "ast.AsyncWith"],
19101910
parent: NodeNG,
1911-
) -> T_With:
1911+
) -> _WithT:
19121912
col_offset = node.col_offset
19131913
if IS_PYPY and not PY39_PLUS and isinstance(node, ast.AsyncWith) and self._data:
19141914
# pylint: disable-next=unsubscriptable-object

0 commit comments

Comments
 (0)