summaryrefslogtreecommitdiff
path: root/astroid/_ast.py
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2022-05-13 00:39:14 +0200
committerGitHub <noreply@github.com>2022-05-13 00:39:14 +0200
commitd616f2b08b8aae4ad835103e4ff7eb21a96cc2d4 (patch)
treef14658746d2370b1fe5ec06ce7cc7810c55774c9 /astroid/_ast.py
parent58bbe0f77a3cdd4e91d4af719208e324334a869f (diff)
downloadastroid-git-d616f2b08b8aae4ad835103e4ff7eb21a96cc2d4.tar.gz
Update typing for Python 3.7 (1) (#1555)
Diffstat (limited to 'astroid/_ast.py')
-rw-r--r--astroid/_ast.py30
1 files changed, 16 insertions, 14 deletions
diff --git a/astroid/_ast.py b/astroid/_ast.py
index e8578586..3a866c2c 100644
--- a/astroid/_ast.py
+++ b/astroid/_ast.py
@@ -2,17 +2,19 @@
# For details: https://github.com/PyCQA/astroid/blob/main/LICENSE
# Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt
+from __future__ import annotations
+
import ast
import sys
import types
from functools import partial
-from typing import Dict, List, NamedTuple, Optional, Type
+from typing import NamedTuple
from astroid.const import PY38_PLUS, Context
if sys.version_info >= (3, 8):
# On Python 3.8, typed_ast was merged back into `ast`
- _ast_py3: Optional[types.ModuleType] = ast
+ _ast_py3: types.ModuleType | None = ast
else:
try:
import typed_ast.ast3 as _ast_py3
@@ -21,17 +23,17 @@ else:
class FunctionType(NamedTuple):
- argtypes: List[ast.expr]
+ argtypes: list[ast.expr]
returns: ast.expr
class ParserModule(NamedTuple):
module: types.ModuleType
- unary_op_classes: Dict[Type[ast.unaryop], str]
- cmp_op_classes: Dict[Type[ast.cmpop], str]
- bool_op_classes: Dict[Type[ast.boolop], str]
- bin_op_classes: Dict[Type[ast.operator], str]
- context_classes: Dict[Type[ast.expr_context], Context]
+ unary_op_classes: dict[type[ast.unaryop], str]
+ cmp_op_classes: dict[type[ast.cmpop], str]
+ bool_op_classes: dict[type[ast.boolop], str]
+ bin_op_classes: dict[type[ast.operator], str]
+ context_classes: dict[type[ast.expr_context], Context]
def parse(self, string: str, type_comments: bool = True) -> ast.Module:
if self.module is _ast_py3:
@@ -46,7 +48,7 @@ class ParserModule(NamedTuple):
return parse_func(string)
-def parse_function_type_comment(type_comment: str) -> Optional[FunctionType]:
+def parse_function_type_comment(type_comment: str) -> FunctionType | None:
"""Given a correct type comment, obtain a FunctionType object"""
if _ast_py3 is None:
return None
@@ -78,13 +80,13 @@ def get_parser_module(type_comments: bool = True) -> ParserModule:
def _unary_operators_from_module(
module: types.ModuleType,
-) -> Dict[Type[ast.unaryop], str]:
+) -> dict[type[ast.unaryop], str]:
return {module.UAdd: "+", module.USub: "-", module.Not: "not", module.Invert: "~"}
def _binary_operators_from_module(
module: types.ModuleType,
-) -> Dict[Type[ast.operator], str]:
+) -> dict[type[ast.operator], str]:
binary_operators = {
module.Add: "+",
module.BitAnd: "&",
@@ -105,13 +107,13 @@ def _binary_operators_from_module(
def _bool_operators_from_module(
module: types.ModuleType,
-) -> Dict[Type[ast.boolop], str]:
+) -> dict[type[ast.boolop], str]:
return {module.And: "and", module.Or: "or"}
def _compare_operators_from_module(
module: types.ModuleType,
-) -> Dict[Type[ast.cmpop], str]:
+) -> dict[type[ast.cmpop], str]:
return {
module.Eq: "==",
module.Gt: ">",
@@ -128,7 +130,7 @@ def _compare_operators_from_module(
def _contexts_from_module(
module: types.ModuleType,
-) -> Dict[Type[ast.expr_context], Context]:
+) -> dict[type[ast.expr_context], Context]:
return {
module.Load: Context.Load,
module.Store: Context.Store,