WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 05c79e1

Browse files
[Backport maintenance/4.0.x] Improve self argument typing (#2904)
Improve self argument typing (#2900) (cherry picked from commit bf68bf8) Co-authored-by: Marc Mueller <[email protected]>
1 parent a068430 commit 05c79e1

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
@@ -1022,7 +1022,7 @@ def get_children(self):
10221022

10231023
@decorators.raise_if_nothing_inferred
10241024
def _infer(
1025-
self: nodes.Arguments, context: InferenceContext | None = None, **kwargs: Any
1025+
self, context: InferenceContext | None = None, **kwargs: Any
10261026
) -> Generator[InferenceResult]:
10271027
# pylint: disable-next=import-outside-toplevel
10281028
from astroid.protocols import _arguments_infer_argname
@@ -1441,7 +1441,7 @@ def _infer_augassign(
14411441
@decorators.raise_if_nothing_inferred
14421442
@decorators.path_wrapper
14431443
def _infer(
1444-
self: nodes.AugAssign, context: InferenceContext | None = None, **kwargs: Any
1444+
self, context: InferenceContext | None = None, **kwargs: Any
14451445
) -> Generator[InferenceResult]:
14461446
return self._filter_operation_errors(
14471447
self._infer_augassign, context, util.BadBinaryOperationMessage
@@ -1556,7 +1556,7 @@ def _infer_binop(
15561556
@decorators.yes_if_nothing_inferred
15571557
@decorators.path_wrapper
15581558
def _infer(
1559-
self: nodes.BinOp, context: InferenceContext | None = None, **kwargs: Any
1559+
self, context: InferenceContext | None = None, **kwargs: Any
15601560
) -> Generator[InferenceResult]:
15611561
return self._filter_operation_errors(
15621562
self._infer_binop, context, util.BadBinaryOperationMessage
@@ -1633,7 +1633,7 @@ def op_precedence(self) -> int:
16331633
@decorators.raise_if_nothing_inferred
16341634
@decorators.path_wrapper
16351635
def _infer(
1636-
self: nodes.BoolOp, context: InferenceContext | None = None, **kwargs: Any
1636+
self, context: InferenceContext | None = None, **kwargs: Any
16371637
) -> Generator[InferenceResult, None, InferenceErrorInfo | None]:
16381638
"""Infer a boolean operation (and / or / not).
16391639
@@ -4318,7 +4318,7 @@ def op_precedence(self) -> int:
43184318
return super().op_precedence()
43194319

43204320
def _infer_unaryop(
4321-
self: nodes.UnaryOp, context: InferenceContext | None = None, **kwargs: Any
4321+
self, context: InferenceContext | None = None, **kwargs: Any
43224322
) -> Generator[
43234323
InferenceResult | util.BadUnaryOperationMessage, None, InferenceErrorInfo
43244324
]:
@@ -4384,7 +4384,7 @@ def _infer_unaryop(
43844384
@decorators.raise_if_nothing_inferred
43854385
@decorators.path_wrapper
43864386
def _infer(
4387-
self: nodes.UnaryOp, context: InferenceContext | None = None, **kwargs: Any
4387+
self, context: InferenceContext | None = None, **kwargs: Any
43884388
) -> Generator[InferenceResult, None, InferenceErrorInfo]:
43894389
"""Infer what an UnaryOp should return when evaluated."""
43904390
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`,
@@ -1677,7 +1677,7 @@ def scope_lookup(
16771677
frame = self
16781678
return frame._scope_lookup(node, name, offset)
16791679

1680-
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
1680+
def frame(self, *, future: Literal[None, True] = None) -> Self:
16811681
"""The node's frame node.
16821682
16831683
A frame node is a :class:`Module`, :class:`FunctionDef`,
@@ -2884,7 +2884,7 @@ def _assign_nodes_in_scope(self):
28842884
)
28852885
return list(itertools.chain.from_iterable(children_assign_nodes))
28862886

2887-
def frame(self: _T, *, future: Literal[None, True] = None) -> _T:
2887+
def frame(self, *, future: Literal[None, True] = None) -> Self:
28882888
"""The node's frame node.
28892889
28902890
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)