Skip to content

Commit 7fef5fb

Browse files
authored
Small typing improvements (#1435)
1 parent 58706b5 commit 7fef5fb

File tree

5 files changed

+18
-23
lines changed

5 files changed

+18
-23
lines changed

astroid/nodes/node_classes.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
ClassVar,
5353
Generator,
5454
Optional,
55+
Type,
5556
TypeVar,
5657
Union,
5758
)
@@ -685,7 +686,7 @@ def __init__(
685686
self.kwarg: Optional[str] = kwarg # can be None
686687
"""The name of the variable length keyword arguments."""
687688

688-
self.args: typing.Optional[typing.List[AssignName]]
689+
self.args: Optional[typing.List[AssignName]]
689690
"""The names of the required arguments.
690691
691692
Can be None if the associated function does not have a retrievable
@@ -1935,7 +1936,7 @@ class Const(mixins.NoChildrenMixin, NodeNG, Instance):
19351936

19361937
def __init__(
19371938
self,
1938-
value: typing.Any,
1939+
value: Any,
19391940
lineno: Optional[int] = None,
19401941
col_offset: Optional[int] = None,
19411942
parent: Optional[NodeNG] = None,
@@ -1961,7 +1962,7 @@ def __init__(
19611962
:param end_col_offset: The end column this node appears on in the
19621963
source code. Note: This is after the last symbol.
19631964
"""
1964-
self.value: typing.Any = value
1965+
self.value: Any = value
19651966
"""The value that the constant represents."""
19661967

19671968
self.kind: Optional[str] = kind # can be None
@@ -4100,7 +4101,7 @@ def __init__(
41004101
:param end_col_offset: The end column this node appears on in the
41014102
source code. Note: This is after the last symbol.
41024103
"""
4103-
self.body: typing.Union[typing.List[TryExcept], typing.List[NodeNG]] = []
4104+
self.body: typing.List[Union[NodeNG, TryExcept]] = []
41044105
"""The try-except that the finally is attached to."""
41054106

41064107
self.finalbody: typing.List[NodeNG] = []
@@ -4116,7 +4117,7 @@ def __init__(
41164117

41174118
def postinit(
41184119
self,
4119-
body: typing.Union[typing.List[TryExcept], typing.List[NodeNG], None] = None,
4120+
body: Optional[typing.List[Union[NodeNG, TryExcept]]] = None,
41204121
finalbody: Optional[typing.List[NodeNG]] = None,
41214122
) -> None:
41224123
"""Do some setup after initialisation.
@@ -4899,12 +4900,12 @@ class EvaluatedObject(NodeNG):
48994900
_other_fields = ("value",)
49004901

49014902
def __init__(
4902-
self, original: NodeNG, value: typing.Union[NodeNG, util.Uninferable]
4903+
self, original: NodeNG, value: Union[NodeNG, Type[util.Uninferable]]
49034904
) -> None:
49044905
self.original: NodeNG = original
49054906
"""The original node that has already been evaluated"""
49064907

4907-
self.value: typing.Union[NodeNG, util.Uninferable] = value
4908+
self.value: Union[NodeNG, Type[util.Uninferable]] = value
49084909
"""The inferred value"""
49094910

49104911
super().__init__(
@@ -5182,7 +5183,7 @@ def postinit(
51825183
"MatchMapping",
51835184
AssignName,
51845185
Optional[InferenceContext],
5185-
Literal[None],
5186+
None,
51865187
],
51875188
Generator[NodeNG, None, None],
51885189
]
@@ -5289,7 +5290,7 @@ def postinit(self, *, name: Optional[AssignName]) -> None:
52895290
"MatchStar",
52905291
AssignName,
52915292
Optional[InferenceContext],
5292-
Literal[None],
5293+
None,
52935294
],
52945295
Generator[NodeNG, None, None],
52955296
]
@@ -5360,7 +5361,7 @@ def postinit(
53605361
"MatchAs",
53615362
AssignName,
53625363
Optional[InferenceContext],
5363-
Literal[None],
5364+
None,
53645365
],
53655366
Generator[NodeNG, None, None],
53665367
]

astroid/nodes/node_ng.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def parent_of(self, node):
289289

290290
@overload
291291
def statement(
292-
self, *, future: Literal[None] = ...
292+
self, *, future: None = ...
293293
) -> Union["nodes.Statement", "nodes.Module"]:
294294
...
295295

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def __init__(
442442
file: Optional[str] = None,
443443
path: Optional[List[str]] = None,
444444
package: Optional[bool] = None,
445-
parent: Literal[None] = None,
445+
parent: None = None,
446446
pure_python: Optional[bool] = True,
447447
) -> None:
448448
"""
@@ -633,7 +633,7 @@ def fully_defined(self):
633633
return self.file is not None and self.file.endswith(".py")
634634

635635
@overload
636-
def statement(self, *, future: Literal[None] = ...) -> "Module":
636+
def statement(self, *, future: None = ...) -> "Module":
637637
...
638638

639639
@overload

astroid/protocols.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import collections
3636
import itertools
3737
import operator as operator_mod
38-
import sys
3938
from typing import Any, Generator, List, Optional, Union
4039

4140
from astroid import arguments, bases, decorators, helpers, nodes, util
@@ -50,11 +49,6 @@
5049
)
5150
from astroid.nodes import node_classes
5251

53-
if sys.version_info >= (3, 8):
54-
from typing import Literal
55-
else:
56-
from typing_extensions import Literal
57-
5852
raw_building = util.lazy_import("raw_building")
5953
objects = util.lazy_import("objects")
6054

@@ -873,7 +867,7 @@ def match_mapping_assigned_stmts(
873867
self: nodes.MatchMapping,
874868
node: nodes.AssignName,
875869
context: Optional[InferenceContext] = None,
876-
assign_path: Literal[None] = None,
870+
assign_path: None = None,
877871
) -> Generator[nodes.NodeNG, None, None]:
878872
"""Return empty generator (return -> raises StopIteration) so inferred value
879873
is Uninferable.
@@ -890,7 +884,7 @@ def match_star_assigned_stmts(
890884
self: nodes.MatchStar,
891885
node: nodes.AssignName,
892886
context: Optional[InferenceContext] = None,
893-
assign_path: Literal[None] = None,
887+
assign_path: None = None,
894888
) -> Generator[nodes.NodeNG, None, None]:
895889
"""Return empty generator (return -> raises StopIteration) so inferred value
896890
is Uninferable.
@@ -907,7 +901,7 @@ def match_as_assigned_stmts(
907901
self: nodes.MatchAs,
908902
node: nodes.AssignName,
909903
context: Optional[InferenceContext] = None,
910-
assign_path: Literal[None] = None,
904+
assign_path: None = None,
911905
) -> Generator[nodes.NodeNG, None, None]:
912906
"""Infer MatchAs as the Match subject if it's the only MatchCase pattern
913907
else raise StopIteration to yield Uninferable.

astroid/rebuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2317,7 +2317,7 @@ def visit_try(
23172317
)
23182318
else:
23192319
newnode = nodes.TryFinally(node.lineno, node.col_offset, parent)
2320-
body: Union[List[nodes.TryExcept], List[NodeNG]]
2320+
body: List[Union[NodeNG, nodes.TryExcept]]
23212321
if node.handlers:
23222322
body = [self.visit_tryexcept(node, newnode)]
23232323
else:

0 commit comments

Comments
 (0)