Skip to content

Commit bf68bf8

Browse files
authored
Improve self argument typing (#2900)
1 parent 483166f commit bf68bf8

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

astroid/nodes/node_classes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ def get_annotations(self) -> Iterator[nodes.NodeNG]:
10391039

10401040
@decorators.raise_if_nothing_inferred
10411041
def _infer(
1042-
self: nodes.Arguments, context: InferenceContext | None = None, **kwargs: Any
1042+
self, context: InferenceContext | None = None, **kwargs: Any
10431043
) -> Generator[InferenceResult]:
10441044
# pylint: disable-next=import-outside-toplevel
10451045
from astroid.protocols import _arguments_infer_argname
@@ -1458,7 +1458,7 @@ def _infer_augassign(
14581458
@decorators.raise_if_nothing_inferred
14591459
@decorators.path_wrapper
14601460
def _infer(
1461-
self: nodes.AugAssign, context: InferenceContext | None = None, **kwargs: Any
1461+
self, context: InferenceContext | None = None, **kwargs: Any
14621462
) -> Generator[InferenceResult]:
14631463
return self._filter_operation_errors(
14641464
self._infer_augassign, context, util.BadBinaryOperationMessage
@@ -1573,7 +1573,7 @@ def _infer_binop(
15731573
@decorators.yes_if_nothing_inferred
15741574
@decorators.path_wrapper
15751575
def _infer(
1576-
self: nodes.BinOp, context: InferenceContext | None = None, **kwargs: Any
1576+
self, context: InferenceContext | None = None, **kwargs: Any
15771577
) -> Generator[InferenceResult]:
15781578
return self._filter_operation_errors(
15791579
self._infer_binop, context, util.BadBinaryOperationMessage
@@ -1650,7 +1650,7 @@ def op_precedence(self) -> int:
16501650
@decorators.raise_if_nothing_inferred
16511651
@decorators.path_wrapper
16521652
def _infer(
1653-
self: nodes.BoolOp, context: InferenceContext | None = None, **kwargs: Any
1653+
self, context: InferenceContext | None = None, **kwargs: Any
16541654
) -> Generator[InferenceResult, None, InferenceErrorInfo | None]:
16551655
"""Infer a boolean operation (and / or / not).
16561656
@@ -4335,7 +4335,7 @@ def op_precedence(self) -> int:
43354335
return super().op_precedence()
43364336

43374337
def _infer_unaryop(
4338-
self: nodes.UnaryOp, context: InferenceContext | None = None, **kwargs: Any
4338+
self, context: InferenceContext | None = None, **kwargs: Any
43394339
) -> Generator[
43404340
InferenceResult | util.BadUnaryOperationMessage, None, InferenceErrorInfo
43414341
]:
@@ -4401,7 +4401,7 @@ def _infer_unaryop(
44014401
@decorators.raise_if_nothing_inferred
44024402
@decorators.path_wrapper
44034403
def _infer(
4404-
self: nodes.UnaryOp, context: InferenceContext | None = None, **kwargs: Any
4404+
self, context: InferenceContext | None = None, **kwargs: Any
44054405
) -> Generator[InferenceResult, None, InferenceErrorInfo]:
44064406
"""Infer what an UnaryOp should return when evaluated."""
44074407
yield from self._filter_operation_errors(

astroid/nodes/scoped_nodes/mixin.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
from __future__ import annotations
88

9-
from typing import TYPE_CHECKING, TypeVar, overload
9+
from typing import TYPE_CHECKING, overload
10+
11+
from typing_extensions import Self
1012

1113
from astroid.exceptions import ParentMissingError
1214
from astroid.filter_statements import _filter_stmts
@@ -17,8 +19,6 @@
1719
if TYPE_CHECKING:
1820
from astroid import nodes
1921

20-
_T = TypeVar("_T")
21-
2222

2323
class LocalsDictNodeNG(_base_nodes.LookupMixIn):
2424
"""this class provides locals handling common to Module, FunctionDef
@@ -46,7 +46,7 @@ def qname(self) -> str:
4646
except ParentMissingError:
4747
return self.name
4848

49-
def scope(self: _T) -> _T:
49+
def scope(self) -> Self:
5050
"""The first parent node defining a new scope.
5151
5252
:returns: The first parent scope node.

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
import os
1616
from collections.abc import Generator, Iterable, Iterator, Sequence
1717
from functools import cached_property, lru_cache
18-
from typing import TYPE_CHECKING, Any, ClassVar, Literal, NoReturn, TypeVar
18+
from typing import TYPE_CHECKING, Any, ClassVar, Literal, NoReturn
19+
20+
from typing_extensions import Self
1921

2022
from astroid import bases, protocols, util
2123
from astroid.context import (
@@ -62,8 +64,6 @@
6264
{"classmethod", "staticmethod", "builtins.classmethod", "builtins.staticmethod"}
6365
)
6466

65-
_T = TypeVar("_T")
66-
6767

6868
def _c3_merge(sequences, cls, context):
6969
"""Merges MROs in *sequences* to a single MRO using the C3 algorithm.
@@ -587,7 +587,7 @@ def bool_value(self, context: InferenceContext | None = None) -> bool:
587587
def get_children(self):
588588
yield from self.body
589589

590-
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
590+
def frame(self, *, future: Literal[None, True] = None) -> Self:
591591
"""The node's frame node.
592592
593593
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -1030,7 +1030,7 @@ def get_children(self):
10301030
yield self.args
10311031
yield self.body
10321032

1033-
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
1033+
def frame(self, *, future: Literal[None, True] = None) -> Self:
10341034
"""The node's frame node.
10351035
10361036
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -1687,7 +1687,7 @@ def scope_lookup(
16871687
frame = self
16881688
return frame._scope_lookup(node, name, offset)
16891689

1690-
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
1690+
def frame(self, *, future: Literal[None, True] = None) -> Self:
16911691
"""The node's frame node.
16921692
16931693
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -2896,7 +2896,7 @@ def _assign_nodes_in_scope(self):
28962896
)
28972897
return list(itertools.chain.from_iterable(children_assign_nodes))
28982898

2899-
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
2899+
def frame(self, *, future: Literal[None, True] = None) -> Self:
29002900
"""The node's frame node.
29012901
29022902
A frame node is a :class:`Module`, :class:`FunctionDef`,

astroid/objects.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

1616
from collections.abc import Generator, Iterator
1717
from functools import cached_property
18-
from typing import Any, Literal, NoReturn, TypeVar
18+
from typing import Any, Literal, NoReturn
19+
20+
from typing_extensions import Self
1921

2022
from astroid import bases, util
2123
from astroid.context import InferenceContext
@@ -30,8 +32,6 @@
3032
from astroid.nodes import node_classes, scoped_nodes
3133
from astroid.typing import InferenceResult, SuccessfulInferenceResult
3234

33-
_T = TypeVar("_T")
34-
3535

3636
class FrozenSet(node_classes.BaseContainer):
3737
"""Class representing a FrozenSet composite node."""
@@ -355,6 +355,6 @@ def infer_call_result(
355355
raise InferenceError("Properties are not callable")
356356

357357
def _infer(
358-
self: _T, context: InferenceContext | None = None, **kwargs: Any
359-
) -> Generator[_T]:
358+
self, context: InferenceContext | None = None, **kwargs: Any
359+
) -> Generator[Self]:
360360
yield self

0 commit comments

Comments
 (0)