Skip to content

Commit ef104c3

Browse files
DanielNoordcdce8p
andauthored
Type infer_unary_op (#1664)
Co-authored-by: Marc Mueller <[email protected]>
1 parent 10315fd commit ef104c3

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

astroid/nodes/node_classes.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@
3030
from astroid.nodes import _base_nodes
3131
from astroid.nodes.const import OP_PRECEDENCE
3232
from astroid.nodes.node_ng import NodeNG
33-
from astroid.typing import InferenceResult, SuccessfulInferenceResult
33+
from astroid.typing import (
34+
ConstFactoryResult,
35+
InferenceResult,
36+
SuccessfulInferenceResult,
37+
)
3438

3539
if sys.version_info >= (3, 8):
3640
from typing import Literal
@@ -64,14 +68,15 @@ def _is_const(value):
6468
],
6569
Any,
6670
]
67-
InferLHS = Callable[
68-
[_NodesT, Optional[InferenceContext]],
69-
typing.Generator[InferenceResult, None, None],
70-
]
7171
InferBinaryOperation = Callable[
7272
[_NodesT, Optional[InferenceContext]],
7373
typing.Generator[Union[InferenceResult, _BadOpMessageT], None, None],
7474
]
75+
InferLHS = Callable[
76+
[_NodesT, Optional[InferenceContext]],
77+
typing.Generator[InferenceResult, None, None],
78+
]
79+
InferUnaryOp = Callable[[_NodesT, str], ConstFactoryResult]
7580

7681

7782
@decorators.raise_if_nothing_inferred
@@ -1906,6 +1911,8 @@ def __init__(
19061911
parent=parent,
19071912
)
19081913

1914+
infer_unary_op: ClassVar[InferUnaryOp[Const]]
1915+
19091916
def __getattr__(self, name):
19101917
# This is needed because of Proxy's __getattr__ method.
19111918
# Calling object.__new__ on this class without calling
@@ -2267,6 +2274,8 @@ def postinit(
22672274
"""
22682275
self.items = items
22692276

2277+
infer_unary_op: ClassVar[InferUnaryOp[Dict]]
2278+
22702279
@classmethod
22712280
def from_elements(cls, items=None):
22722281
"""Create a :class:`Dict` of constants from a live dictionary.
@@ -3390,6 +3399,8 @@ def __init__(
33903399
See astroid/protocols.py for actual implementation.
33913400
"""
33923401

3402+
infer_unary_op: ClassVar[InferUnaryOp[List]]
3403+
33933404
def pytype(self):
33943405
"""Get the name of the type that this node represents.
33953406
@@ -3626,6 +3637,8 @@ class Set(BaseContainer):
36263637
<Set.set l.1 at 0x7f23b2e71d68>
36273638
"""
36283639

3640+
infer_unary_op: ClassVar[InferUnaryOp[Set]]
3641+
36293642
def pytype(self):
36303643
"""Get the name of the type that this node represents.
36313644
@@ -4152,6 +4165,8 @@ def __init__(
41524165
See astroid/protocols.py for actual implementation.
41534166
"""
41544167

4168+
infer_unary_op: ClassVar[InferUnaryOp[Tuple]]
4169+
41554170
def pytype(self):
41564171
"""Get the name of the type that this node represents.
41574172
@@ -5401,7 +5416,7 @@ def _create_dict_items(
54015416
return elements
54025417

54035418

5404-
def const_factory(value: Any) -> List | Set | Tuple | Dict | Const | EmptyNode:
5419+
def const_factory(value: Any) -> ConstFactoryResult:
54055420
"""Return an astroid node for a python value."""
54065421
assert not isinstance(value, NodeNG)
54075422

astroid/protocols.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import collections
1212
import itertools
1313
import operator as operator_mod
14-
from collections.abc import Generator
14+
from collections.abc import Callable, Generator
1515
from typing import Any
1616

1717
from astroid import arguments, bases, decorators, helpers, nodes, util
@@ -25,6 +25,7 @@
2525
NoDefault,
2626
)
2727
from astroid.nodes import node_classes
28+
from astroid.typing import ConstFactoryResult
2829

2930
raw_building = util.lazy_import("raw_building")
3031
objects = util.lazy_import("objects")
@@ -68,15 +69,19 @@ def _augmented_name(name):
6869
"~": "__invert__",
6970
"not": None, # XXX not '__nonzero__'
7071
}
71-
_UNARY_OPERATORS = {
72+
_UNARY_OPERATORS: dict[str, Callable[[Any], Any]] = {
7273
"+": operator_mod.pos,
7374
"-": operator_mod.neg,
7475
"~": operator_mod.invert,
7576
"not": operator_mod.not_,
7677
}
7778

7879

79-
def _infer_unary_op(obj, op):
80+
def _infer_unary_op(obj: Any, op: str) -> ConstFactoryResult:
81+
"""Perform unary operation on object.
82+
83+
Can raise TypeError if operation is unsupported.
84+
"""
8085
func = _UNARY_OPERATORS[op]
8186
value = func(obj)
8287
return nodes.const_factory(value)

astroid/typing.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,12 @@ class AstroidManagerBrain(TypedDict):
4343

4444
InferenceResult = Union["nodes.NodeNG", "type[util.Uninferable]", "bases.Proxy"]
4545
SuccessfulInferenceResult = Union["nodes.NodeNG", "bases.Proxy"]
46+
47+
ConstFactoryResult = Union[
48+
"nodes.List",
49+
"nodes.Set",
50+
"nodes.Tuple",
51+
"nodes.Dict",
52+
"nodes.Const",
53+
"nodes.EmptyNode",
54+
]

0 commit comments

Comments
 (0)