From a91a8d60ccd5ca8e5f8e162d67b3b93444105235 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 23 Apr 2023 23:53:59 +0200 Subject: Drop support for Python 3.7 (#2137) --- astroid/__init__.py | 4 +- astroid/_ast.py | 133 ++++++++++------------------- astroid/bases.py | 8 +- astroid/brain/brain_dataclasses.py | 8 +- astroid/brain/brain_namedtuple_enum.py | 8 +- astroid/brain/brain_typing.py | 7 +- astroid/decorators.py | 16 ++-- astroid/interpreter/_import/spec.py | 7 +- astroid/interpreter/objectmodel.py | 10 +-- astroid/modutils.py | 2 +- astroid/nodes/_base_nodes.py | 7 +- astroid/nodes/node_classes.py | 26 +++--- astroid/nodes/node_ng.py | 12 +-- astroid/nodes/scoped_nodes/scoped_nodes.py | 13 +-- astroid/objects.py | 12 +-- astroid/rebuilder.py | 125 +++++++++++---------------- astroid/typing.py | 7 +- astroid/util.py | 8 +- 18 files changed, 133 insertions(+), 280 deletions(-) (limited to 'astroid') diff --git a/astroid/__init__.py b/astroid/__init__.py index 88a0c374..1c7c4af7 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -181,13 +181,13 @@ from astroid.nodes import ( # pylint: disable=redefined-builtin (Ellipsis) from astroid.util import Uninferable # Performance hack for tokenize. See https://bugs.python.org/issue43014 -# Adapted from https://github.com/pylint-dev/pycodestyle/pull/993 +# Adapted from https://github.com/PyCQA/pycodestyle/pull/993 if ( not PY310_PLUS and callable(getattr(tokenize, "_compile", None)) and getattr(tokenize._compile, "__wrapped__", None) is None # type: ignore[attr-defined] ): - tokenize._compile = functools.lru_cache()(tokenize._compile) # type: ignore[attr-defined] + tokenize._compile = functools.lru_cache(tokenize._compile) # type: ignore[attr-defined] # load brain plugins for module in BRAIN_MODULES_DIRECTORY.iterdir(): diff --git a/astroid/_ast.py b/astroid/_ast.py index fc81be34..c134ae70 100644 --- a/astroid/_ast.py +++ b/astroid/_ast.py @@ -5,22 +5,9 @@ from __future__ import annotations import ast -import sys -import types -from collections.abc import Callable -from functools import partial 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: types.ModuleType | None = ast -else: - try: - import typed_ast.ast3 as _ast_py3 - except ImportError: - _ast_py3 = None +from astroid.const import Context class FunctionType(NamedTuple): @@ -29,7 +16,6 @@ class FunctionType(NamedTuple): 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] @@ -37,41 +23,23 @@ class ParserModule(NamedTuple): context_classes: dict[type[ast.expr_context], Context] def parse(self, string: str, type_comments: bool = True) -> ast.Module: - parse_func: Callable[[str], ast.Module] - if self.module is _ast_py3: - if PY38_PLUS: - parse_func = partial(self.module.parse, type_comments=type_comments) - else: - parse_func = partial( - self.module.parse, feature_version=sys.version_info.minor - ) - else: - parse_func = self.module.parse - return parse_func(string) + return ast.parse(string, type_comments=type_comments) 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 - - func_type = _ast_py3.parse(type_comment, "", "func_type") # type: ignore[attr-defined] + func_type = ast.parse(type_comment, "", "func_type") # type: ignore[attr-defined] return FunctionType(argtypes=func_type.argtypes, returns=func_type.returns) def get_parser_module(type_comments: bool = True) -> ParserModule: - parser_module = ast - if type_comments and _ast_py3: - parser_module = _ast_py3 - - unary_op_classes = _unary_operators_from_module(parser_module) - cmp_op_classes = _compare_operators_from_module(parser_module) - bool_op_classes = _bool_operators_from_module(parser_module) - bin_op_classes = _binary_operators_from_module(parser_module) - context_classes = _contexts_from_module(parser_module) + unary_op_classes = _unary_operators_from_module() + cmp_op_classes = _compare_operators_from_module() + bool_op_classes = _bool_operators_from_module() + bin_op_classes = _binary_operators_from_module() + context_classes = _contexts_from_module() return ParserModule( - parser_module, unary_op_classes, cmp_op_classes, bool_op_classes, @@ -80,62 +48,51 @@ def get_parser_module(type_comments: bool = True) -> ParserModule: ) -def _unary_operators_from_module( - module: types.ModuleType, -) -> 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]: - binary_operators = { - module.Add: "+", - module.BitAnd: "&", - module.BitOr: "|", - module.BitXor: "^", - module.Div: "/", - module.FloorDiv: "//", - module.MatMult: "@", - module.Mod: "%", - module.Mult: "*", - module.Pow: "**", - module.Sub: "-", - module.LShift: "<<", - module.RShift: ">>", +def _unary_operators_from_module() -> dict[type[ast.unaryop], str]: + return {ast.UAdd: "+", ast.USub: "-", ast.Not: "not", ast.Invert: "~"} + + +def _binary_operators_from_module() -> dict[type[ast.operator], str]: + return { + ast.Add: "+", + ast.BitAnd: "&", + ast.BitOr: "|", + ast.BitXor: "^", + ast.Div: "/", + ast.FloorDiv: "//", + ast.MatMult: "@", + ast.Mod: "%", + ast.Mult: "*", + ast.Pow: "**", + ast.Sub: "-", + ast.LShift: "<<", + ast.RShift: ">>", } - return binary_operators -def _bool_operators_from_module( - module: types.ModuleType, -) -> dict[type[ast.boolop], str]: - return {module.And: "and", module.Or: "or"} +def _bool_operators_from_module() -> dict[type[ast.boolop], str]: + return {ast.And: "and", ast.Or: "or"} -def _compare_operators_from_module( - module: types.ModuleType, -) -> dict[type[ast.cmpop], str]: +def _compare_operators_from_module() -> dict[type[ast.cmpop], str]: return { - module.Eq: "==", - module.Gt: ">", - module.GtE: ">=", - module.In: "in", - module.Is: "is", - module.IsNot: "is not", - module.Lt: "<", - module.LtE: "<=", - module.NotEq: "!=", - module.NotIn: "not in", + ast.Eq: "==", + ast.Gt: ">", + ast.GtE: ">=", + ast.In: "in", + ast.Is: "is", + ast.IsNot: "is not", + ast.Lt: "<", + ast.LtE: "<=", + ast.NotEq: "!=", + ast.NotIn: "not in", } -def _contexts_from_module( - module: types.ModuleType, -) -> dict[type[ast.expr_context], Context]: +def _contexts_from_module() -> dict[type[ast.expr_context], Context]: return { - module.Load: Context.Load, - module.Store: Context.Store, - module.Del: Context.Del, - module.Param: Context.Store, + ast.Load: Context.Load, + ast.Store: Context.Store, + ast.Del: Context.Del, + ast.Param: Context.Store, } diff --git a/astroid/bases.py b/astroid/bases.py index de63e7c9..e3832d8b 100644 --- a/astroid/bases.py +++ b/astroid/bases.py @@ -9,9 +9,8 @@ from __future__ import annotations import collections import collections.abc -import sys from collections.abc import Iterator, Sequence -from typing import TYPE_CHECKING, Any, ClassVar +from typing import TYPE_CHECKING, Any, ClassVar, Literal from astroid import nodes from astroid.const import PY310_PLUS @@ -36,11 +35,6 @@ from astroid.typing import ( ) from astroid.util import Uninferable, UninferableBase, lazy_descriptor -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - if TYPE_CHECKING: from astroid.constraint import Constraint diff --git a/astroid/brain/brain_dataclasses.py b/astroid/brain/brain_dataclasses.py index 133209d5..49f47b67 100644 --- a/astroid/brain/brain_dataclasses.py +++ b/astroid/brain/brain_dataclasses.py @@ -14,9 +14,8 @@ dataclasses. References: from __future__ import annotations -import sys from collections.abc import Iterator -from typing import Tuple, Union +from typing import Literal, Tuple, Union from astroid import bases, context, helpers, nodes from astroid.builder import parse @@ -27,11 +26,6 @@ from astroid.manager import AstroidManager from astroid.typing import InferenceResult from astroid.util import Uninferable, UninferableBase -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - _FieldDefaultReturn = Union[ None, Tuple[Literal["default"], nodes.NodeNG], diff --git a/astroid/brain/brain_namedtuple_enum.py b/astroid/brain/brain_namedtuple_enum.py index f5d5f595..2af99017 100644 --- a/astroid/brain/brain_namedtuple_enum.py +++ b/astroid/brain/brain_namedtuple_enum.py @@ -8,9 +8,9 @@ from __future__ import annotations import functools import keyword -import sys from collections.abc import Iterator from textwrap import dedent +from typing import Final import astroid from astroid import arguments, bases, inference_tip, nodes, util @@ -25,12 +25,6 @@ from astroid.exceptions import ( ) from astroid.manager import AstroidManager -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - - ENUM_BASE_NAMES = { "Enum", "IntEnum", diff --git a/astroid/brain/brain_typing.py b/astroid/brain/brain_typing.py index b50211db..35c5f0d8 100644 --- a/astroid/brain/brain_typing.py +++ b/astroid/brain/brain_typing.py @@ -6,10 +6,10 @@ from __future__ import annotations -import sys import typing from collections.abc import Iterator from functools import partial +from typing import Final from astroid import context, extract_node, inference_tip from astroid.builder import _extract_single_node @@ -34,11 +34,6 @@ from astroid.nodes.node_classes import ( ) from astroid.nodes.scoped_nodes import ClassDef, FunctionDef -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - TYPING_TYPEVARS = {"TypeVar", "NewType"} TYPING_TYPEVARS_QUALIFIED: Final = { "typing.TypeVar", diff --git a/astroid/decorators.py b/astroid/decorators.py index 1aaf14b1..48f64058 100644 --- a/astroid/decorators.py +++ b/astroid/decorators.py @@ -27,8 +27,7 @@ _R = TypeVar("_R") _P = ParamSpec("_P") -# TODO: Remove when support for 3.7 is dropped -# TODO: astroid 3.0 -> move class behind sys.version_info < (3, 8) guard +# TODO: Remove for astroid 3.0 class cachedproperty: """Provides a cached property equivalent to the stacking of @cached and @property, but more efficient. @@ -47,13 +46,12 @@ class cachedproperty: __slots__ = ("wrapped",) def __init__(self, wrapped): - if sys.version_info >= (3, 8): - warnings.warn( - "cachedproperty has been deprecated and will be removed in astroid 3.0 for Python 3.8+. " - "Use functools.cached_property instead.", - DeprecationWarning, - stacklevel=2, - ) + warnings.warn( + "cachedproperty has been deprecated and will be removed in astroid 3.0" + "Use functools.cached_property instead.", + DeprecationWarning, + stacklevel=2, + ) try: wrapped.__name__ # noqa[B018] except AttributeError as exc: diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index f17ce51f..1630ca8a 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -17,18 +17,13 @@ import warnings import zipimport from collections.abc import Iterator, Sequence from pathlib import Path -from typing import Any, NamedTuple +from typing import Any, Literal, NamedTuple, Protocol from astroid.const import PY310_PLUS from astroid.modutils import EXT_LIB_DIRS from . import util -if sys.version_info >= (3, 8): - from typing import Literal, Protocol -else: - from typing_extensions import Literal, Protocol - # The MetaPathFinder protocol comes from typeshed, which says: # Intentionally omits one deprecated and one optional method of `importlib.abc.MetaPathFinder` diff --git a/astroid/interpreter/objectmodel.py b/astroid/interpreter/objectmodel.py index bd9e9f5b..12405684 100644 --- a/astroid/interpreter/objectmodel.py +++ b/astroid/interpreter/objectmodel.py @@ -26,10 +26,9 @@ from __future__ import annotations import itertools import os import pprint -import sys import types from functools import lru_cache -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING, Any, Literal import astroid from astroid import bases, nodes, util @@ -38,11 +37,6 @@ from astroid.exceptions import AttributeInferenceError, InferenceError, NoDefaul from astroid.manager import AstroidManager from astroid.nodes import node_classes -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - if TYPE_CHECKING: from astroid.objects import Property @@ -115,7 +109,7 @@ class ObjectModel: def __contains__(self, name) -> bool: return name in self.attributes() - @lru_cache() # noqa + @lru_cache # noqa def attributes(self) -> list[str]: """Get the attributes which are exported by this object model.""" return [o[LEN_OF_IMPL_PREFIX:] for o in dir(self) if o.startswith(IMPL_PREFIX)] diff --git a/astroid/modutils.py b/astroid/modutils.py index 4e8d8f9f..b4f3b6e3 100644 --- a/astroid/modutils.py +++ b/astroid/modutils.py @@ -155,7 +155,7 @@ def _handle_blacklist( filenames.remove(norecurs) -@lru_cache() +@lru_cache def _cache_normalize_path_(path: str) -> str: return _normalize_path(path) diff --git a/astroid/nodes/_base_nodes.py b/astroid/nodes/_base_nodes.py index 25d7316e..3ef97b58 100644 --- a/astroid/nodes/_base_nodes.py +++ b/astroid/nodes/_base_nodes.py @@ -10,8 +10,8 @@ Previously these were called Mixin nodes. from __future__ import annotations import itertools -import sys from collections.abc import Iterator +from functools import cached_property from typing import TYPE_CHECKING, ClassVar from astroid.exceptions import AttributeInferenceError @@ -20,11 +20,6 @@ from astroid.nodes.node_ng import NodeNG if TYPE_CHECKING: from astroid import nodes -if sys.version_info >= (3, 8): - from functools import cached_property -else: - from astroid.decorators import cachedproperty as cached_property - class Statement(NodeNG): """Statement node adding a few attributes. diff --git a/astroid/nodes/node_classes.py b/astroid/nodes/node_classes.py index 2f29355d..c9221e14 100644 --- a/astroid/nodes/node_classes.py +++ b/astroid/nodes/node_classes.py @@ -8,12 +8,20 @@ from __future__ import annotations import abc import itertools -import sys import typing import warnings from collections.abc import Generator, Iterable, Iterator, Mapping -from functools import lru_cache -from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional, TypeVar, Union +from functools import cached_property, lru_cache +from typing import ( + TYPE_CHECKING, + Any, + Callable, + ClassVar, + Literal, + Optional, + TypeVar, + Union, +) from astroid import decorators, util from astroid.bases import Instance, _infer_stmts @@ -39,20 +47,10 @@ from astroid.typing import ( SuccessfulInferenceResult, ) -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - if TYPE_CHECKING: from astroid import nodes from astroid.nodes import LocalsDictNodeNG -if sys.version_info >= (3, 8): - from functools import cached_property -else: - from astroid.decorators import cachedproperty as cached_property - def _is_const(value) -> bool: return isinstance(value, tuple(CONST_CLS)) @@ -354,7 +352,7 @@ class BaseContainer(_base_nodes.ParentAssignNode, Instance, metaclass=abc.ABCMet class LookupMixIn(NodeNG): """Mixin to look up a name in the right scope.""" - @lru_cache() # noqa + @lru_cache # noqa def lookup(self, name: str) -> tuple[LocalsDictNodeNG, list[NodeNG]]: """Lookup where the given variable is assigned. diff --git a/astroid/nodes/node_ng.py b/astroid/nodes/node_ng.py index 14e9a624..bc981b91 100644 --- a/astroid/nodes/node_ng.py +++ b/astroid/nodes/node_ng.py @@ -5,14 +5,15 @@ from __future__ import annotations import pprint -import sys import warnings from collections.abc import Generator, Iterator +from functools import cached_property from functools import singledispatch as _singledispatch from typing import ( TYPE_CHECKING, Any, ClassVar, + Literal, Tuple, Type, TypeVar, @@ -39,15 +40,6 @@ from astroid.typing import InferenceErrorInfo, InferenceResult, InferFn if TYPE_CHECKING: from astroid import nodes -if sys.version_info >= (3, 8): - from typing import Literal -else: - from typing_extensions import Literal - -if sys.version_info >= (3, 8): - from functools import cached_property -else: - from astroid.decorators import cachedproperty as cached_property # Types for 'NodeNG.nodes_of_class()' _NodesT = TypeVar("_NodesT", bound="NodeNG") diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index 0dfd1658..771f93f3 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -13,11 +13,10 @@ from __future__ import annotations import io import itertools import os -import sys import warnings from collections.abc import Generator, Iterable, Iterator, Sequence -from functools import lru_cache -from typing import TYPE_CHECKING, ClassVar, NoReturn, TypeVar, overload +from functools import cached_property, lru_cache +from typing import TYPE_CHECKING, ClassVar, Literal, NoReturn, TypeVar, overload from astroid import bases, util from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, PYPY_7_3_11_PLUS @@ -47,14 +46,6 @@ from astroid.nodes.scoped_nodes.utils import builtin_lookup from astroid.nodes.utils import Position from astroid.typing import InferBinaryOp, InferenceResult, SuccessfulInferenceResult -if sys.version_info >= (3, 8): - from functools import cached_property - from typing import Literal -else: - from typing_extensions import Literal - - from astroid.decorators import cachedproperty as cached_property - if TYPE_CHECKING: from astroid import nodes diff --git a/astroid/objects.py b/astroid/objects.py index 08750b3e..784881be 100644 --- a/astroid/objects.py +++ b/astroid/objects.py @@ -13,9 +13,9 @@ leads to an inferred FrozenSet: from __future__ import annotations -import sys from collections.abc import Generator, Iterator -from typing import Any, TypeVar +from functools import cached_property +from typing import Any, Literal, TypeVar from astroid import bases, decorators, util from astroid.context import InferenceContext @@ -30,14 +30,6 @@ from astroid.manager import AstroidManager from astroid.nodes import node_classes, scoped_nodes from astroid.typing import InferenceResult, SuccessfulInferenceResult -if sys.version_info >= (3, 8): - from functools import cached_property - from typing import Literal -else: - from typing_extensions import Literal - - from astroid.decorators import cachedproperty as cached_property - _T = TypeVar("_T") diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 011b6f15..5a17c3b9 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -14,7 +14,7 @@ import token from collections.abc import Callable, Generator from io import StringIO from tokenize import TokenInfo, generate_tokens -from typing import TYPE_CHECKING, TypeVar, Union, cast, overload +from typing import TYPE_CHECKING, Final, TypeVar, Union, cast, overload from astroid import nodes from astroid._ast import ParserModule, get_parser_module, parse_function_type_comment @@ -24,12 +24,6 @@ from astroid.nodes import NodeNG from astroid.nodes.utils import Position from astroid.typing import SuccessfulInferenceResult -if sys.version_info >= (3, 8): - from typing import Final -else: - from typing_extensions import Final - - REDIRECT: Final[dict[str, str]] = { "arguments": "Arguments", "comprehension": "Comprehension", @@ -74,16 +68,15 @@ class TreeRebuilder: self._parser_module = get_parser_module() else: self._parser_module = parser_module - self._module = self._parser_module.module def _get_doc(self, node: T_Doc) -> tuple[T_Doc, ast.Constant | ast.Str | None]: """Return the doc ast node.""" try: - if node.body and isinstance(node.body[0], self._module.Expr): + if node.body and isinstance(node.body[0], ast.Expr): first_value = node.body[0].value - if isinstance(first_value, self._module.Str) or ( + if isinstance(first_value, ast.Str) or ( PY38_PLUS - and isinstance(first_value, self._module.Constant) + and isinstance(first_value, ast.Constant) and isinstance(first_value.value, str) ): doc_ast_node = first_value @@ -400,11 +393,9 @@ class TreeRebuilder: ) -> nodes.FormattedValue: ... - if sys.version_info >= (3, 8): - - @overload - def visit(self, node: ast.NamedExpr, parent: NodeNG) -> nodes.NamedExpr: - ... + @overload + def visit(self, node: ast.NamedExpr, parent: NodeNG) -> nodes.NamedExpr: + ... if sys.version_info < (3, 9): # Not used in Python 3.9+ @@ -629,7 +620,6 @@ class TreeRebuilder: defaults = [self.visit(child, newnode) for child in node.defaults] varargannotation: NodeNG | None = None kwargannotation: NodeNG | None = None - posonlyargs: list[nodes.AssignName] = [] if node.vararg: vararg = node.vararg.arg varargannotation = self.visit(node.vararg.annotation, newnode) @@ -652,24 +642,19 @@ class TreeRebuilder: self.visit(arg.annotation, newnode) for arg in node.kwonlyargs ] - posonlyargs_annotations: list[NodeNG | None] = [] - if PY38_PLUS: - posonlyargs = [self.visit(child, newnode) for child in node.posonlyargs] - posonlyargs_annotations = [ - self.visit(arg.annotation, newnode) for arg in node.posonlyargs - ] + posonlyargs = [self.visit(child, newnode) for child in node.posonlyargs] + posonlyargs_annotations = [ + self.visit(arg.annotation, newnode) for arg in node.posonlyargs + ] type_comment_args = [ self.check_type_comment(child, parent=newnode) for child in node.args ] type_comment_kwonlyargs = [ self.check_type_comment(child, parent=newnode) for child in node.kwonlyargs ] - type_comment_posonlyargs: list[NodeNG | None] = [] - if PY38_PLUS: - type_comment_posonlyargs = [ - self.check_type_comment(child, parent=newnode) - for child in node.posonlyargs - ] + type_comment_posonlyargs = [ + self.check_type_comment(child, parent=newnode) for child in node.posonlyargs + ] newnode.postinit( args=args, @@ -1038,15 +1023,12 @@ class TreeRebuilder: return None # /!\ node is actually an _ast.FunctionDef node while # parent is an astroid.nodes.FunctionDef node - if sys.version_info >= (3, 8): - # Set the line number of the first decorator for Python 3.8+. - lineno = node.decorator_list[0].lineno - end_lineno = node.decorator_list[-1].end_lineno - end_col_offset = node.decorator_list[-1].end_col_offset - else: - lineno = node.lineno - end_lineno = None - end_col_offset = None + + # Set the line number of the first decorator for Python 3.8+. + lineno = node.decorator_list[0].lineno + end_lineno = node.decorator_list[-1].end_lineno + end_col_offset = node.decorator_list[-1].end_col_offset + newnode = nodes.Decorators( lineno=lineno, col_offset=node.col_offset, @@ -1455,23 +1437,19 @@ class TreeRebuilder: ) return newnode - if sys.version_info >= (3, 8): - - def visit_namedexpr( - self, node: ast.NamedExpr, parent: NodeNG - ) -> nodes.NamedExpr: - newnode = nodes.NamedExpr( - lineno=node.lineno, - col_offset=node.col_offset, - # end_lineno and end_col_offset added in 3.8 - end_lineno=getattr(node, "end_lineno", None), - end_col_offset=getattr(node, "end_col_offset", None), - parent=parent, - ) - newnode.postinit( - self.visit(node.target, newnode), self.visit(node.value, newnode) - ) - return newnode + def visit_namedexpr(self, node: ast.NamedExpr, parent: NodeNG) -> nodes.NamedExpr: + newnode = nodes.NamedExpr( + lineno=node.lineno, + col_offset=node.col_offset, + # end_lineno and end_col_offset added in 3.8 + end_lineno=getattr(node, "end_lineno", None), + end_col_offset=getattr(node, "end_col_offset", None), + parent=parent, + ) + newnode.postinit( + self.visit(node.target, newnode), self.visit(node.value, newnode) + ) + return newnode if sys.version_info < (3, 9): # Not used in Python 3.9+. @@ -1776,26 +1754,23 @@ class TreeRebuilder: def visit_tryexcept(self, node: ast.Try, parent: NodeNG) -> nodes.TryExcept: """Visit a TryExcept node by returning a fresh instance of it.""" - if sys.version_info >= (3, 8): - # TryExcept excludes the 'finally' but that will be included in the - # end_lineno from 'node'. Therefore, we check all non 'finally' - # children to find the correct end_lineno and column. - end_lineno = node.end_lineno - end_col_offset = node.end_col_offset - all_children: list[ast.AST] = [*node.body, *node.handlers, *node.orelse] - for child in reversed(all_children): - end_lineno = child.end_lineno - end_col_offset = child.end_col_offset - break - newnode = nodes.TryExcept( - lineno=node.lineno, - col_offset=node.col_offset, - end_lineno=end_lineno, - end_col_offset=end_col_offset, - parent=parent, - ) - else: - newnode = nodes.TryExcept(node.lineno, node.col_offset, parent) + # TryExcept excludes the 'finally' but that will be included in the + # end_lineno from 'node'. Therefore, we check all non 'finally' + # children to find the correct end_lineno and column. + end_lineno = node.end_lineno + end_col_offset = node.end_col_offset + all_children: list[ast.AST] = [*node.body, *node.handlers, *node.orelse] + for child in reversed(all_children): + end_lineno = child.end_lineno + end_col_offset = child.end_col_offset + break + newnode = nodes.TryExcept( + lineno=node.lineno, + col_offset=node.col_offset, + end_lineno=end_lineno, + end_col_offset=end_col_offset, + parent=parent, + ) newnode.postinit( [self.visit(child, newnode) for child in node.body], [self.visit(child, newnode) for child in node.handlers], diff --git a/astroid/typing.py b/astroid/typing.py index c0c184a4..0b6ec8e3 100644 --- a/astroid/typing.py +++ b/astroid/typing.py @@ -4,18 +4,13 @@ from __future__ import annotations -import sys -from typing import TYPE_CHECKING, Any, Callable, Generator, TypeVar, Union +from typing import TYPE_CHECKING, Any, Callable, Generator, TypedDict, TypeVar, Union if TYPE_CHECKING: from astroid import bases, exceptions, nodes, transforms, util from astroid.context import InferenceContext from astroid.interpreter._import import spec -if sys.version_info >= (3, 8): - from typing import TypedDict -else: - from typing_extensions import TypedDict _NodesT = TypeVar("_NodesT", bound="nodes.NodeNG") diff --git a/astroid/util.py b/astroid/util.py index 50bde0b1..d2564f30 100644 --- a/astroid/util.py +++ b/astroid/util.py @@ -5,17 +5,11 @@ from __future__ import annotations -import sys import warnings -from typing import Any +from typing import Any, Final, Literal import lazy_object_proxy -if sys.version_info >= (3, 8): - from typing import Final, Literal -else: - from typing_extensions import Final, Literal - def lazy_descriptor(obj): class DescriptorProxy(lazy_object_proxy.Proxy): -- cgit v1.2.1