Skip to content

Commit

Permalink
Remove unused constants (#2141)
Browse files Browse the repository at this point in the history
* Remove `PY38_PLUS` constant
* Remove `BUILTINS` constants
* Remove `Load` + `Store` + `Del`
* Remove `BOOL_SPECIAL_METHOD`
  • Loading branch information
cdce8p committed Apr 23, 2023
1 parent 7fa8481 commit 1336ee4
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 146 deletions.
11 changes: 11 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ Release date: TBA

Refs #1490

* Remove unused and / or deprecated constants:
- ``astroid.bases.BOOL_SPECIAL_METHOD``
- ``astroid.bases.BUILTINS``
- ``astroid.const.BUILTINS``
- ``astroid.const.PY38_PLUS``
- ``astroid.const.Load``
- ``astroid.const.Store``
- ``astroid.const.Del``

Refs #2141


What's New in astroid 2.15.4?
=============================
Expand Down
2 changes: 1 addition & 1 deletion astroid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from astroid.bases import BaseInstance, BoundMethod, Instance, UnboundMethod
from astroid.brain.helpers import register_module_extender
from astroid.builder import extract_node, parse
from astroid.const import BRAIN_MODULES_DIRECTORY, PY310_PLUS, Context, Del, Load, Store
from astroid.const import BRAIN_MODULES_DIRECTORY, PY310_PLUS, Context
from astroid.exceptions import (
AstroidBuildingError,
AstroidBuildingException,
Expand Down
6 changes: 1 addition & 5 deletions astroid/bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@
from astroid.constraint import Constraint


# TODO: check if needs special treatment
BOOL_SPECIAL_METHOD = "__bool__"
BUILTINS = "builtins" # TODO Remove in 2.8

PROPERTIES = {"builtins.property", "abc.abstractproperty"}
if PY310_PLUS:
PROPERTIES.add("enum.property")
Expand Down Expand Up @@ -383,7 +379,7 @@ def bool_value(
context.boundnode = self

try:
result = _infer_method_result_truth(self, BOOL_SPECIAL_METHOD, context)
result = _infer_method_result_truth(self, "__bool__", context)
except (InferenceError, AttributeInferenceError):
# Fallback to __len__.
try:
Expand Down
6 changes: 2 additions & 4 deletions astroid/brain/brain_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from astroid import parse
from astroid.brain.helpers import register_module_extender
from astroid.const import PY38_PLUS, PY310_PLUS
from astroid.const import PY310_PLUS
from astroid.manager import AstroidManager


Expand Down Expand Up @@ -41,9 +41,7 @@ class Options(_IntFlag):
OP_SINGLE_ECDH_USE = 10
OP_NO_COMPRESSION = 11
OP_NO_TICKET = 12
OP_NO_RENEGOTIATION = 13"""
if PY38_PLUS:
enum += """
OP_NO_RENEGOTIATION = 13
OP_ENABLE_MIDDLEBOX_COMPAT = 14"""
return enum

Expand Down
4 changes: 2 additions & 2 deletions astroid/brain/brain_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from astroid import context, extract_node, inference_tip
from astroid.builder import _extract_single_node
from astroid.const import PY38_PLUS, PY39_PLUS
from astroid.const import PY39_PLUS
from astroid.exceptions import (
AttributeInferenceError,
InferenceError,
Expand Down Expand Up @@ -428,7 +428,7 @@ def infer_typing_cast(
AstroidManager().register_transform(
FunctionDef, inference_tip(infer_typedDict), _looks_like_typedDict
)
elif PY38_PLUS:
else:
AstroidManager().register_transform(
ClassDef, inference_tip(infer_old_typedDict), _looks_like_typedDict
)
Expand Down
6 changes: 1 addition & 5 deletions astroid/brain/brain_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"""Astroid hooks for unittest module."""
from astroid.brain.helpers import register_module_extender
from astroid.builder import parse
from astroid.const import PY38_PLUS
from astroid.manager import AstroidManager


Expand All @@ -27,7 +26,4 @@ def IsolatedAsyncioTestCaseImport():
)


if PY38_PLUS:
register_module_extender(
AstroidManager(), "unittest", IsolatedAsyncioTestCaseImport
)
register_module_extender(AstroidManager(), "unittest", IsolatedAsyncioTestCaseImport)
8 changes: 0 additions & 8 deletions astroid/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
from pathlib import Path

PY38 = sys.version_info[:2] == (3, 8)
PY38_PLUS = sys.version_info >= (3, 8)
PY39_PLUS = sys.version_info >= (3, 9)
PY310_PLUS = sys.version_info >= (3, 10)
PY311_PLUS = sys.version_info >= (3, 11)
BUILTINS = "builtins" # TODO Remove in 2.8

WIN32 = sys.platform == "win32"

Expand All @@ -28,12 +26,6 @@ class Context(enum.Enum):
Del = 3


# TODO Remove in 3.0 in favor of Context
Load = Context.Load
Store = Context.Store
Del = Context.Del


ASTROID_INSTALL_DIRECTORY = Path(__file__).parent
BRAIN_MODULES_DIRECTORY = ASTROID_INSTALL_DIRECTORY / "brain"

Expand Down
4 changes: 2 additions & 2 deletions astroid/nodes/scoped_nodes/scoped_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
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
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS
from astroid.context import (
CallContext,
InferenceContext,
Expand Down Expand Up @@ -2000,7 +2000,7 @@ def fromlineno(self) -> int:
Can also return 0 if the line can not be determined.
"""
if not PY38_PLUS or IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
if IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
# For Python < 3.8 the lineno is the line number of the first decorator.
# We want the class statement lineno. Similar to 'FunctionDef.fromlineno'
# PyPy (3.8): Fixed with version v7.3.11
Expand Down
63 changes: 4 additions & 59 deletions astroid/rebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from astroid import nodes
from astroid._ast import ParserModule, get_parser_module, parse_function_type_comment
from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, Context
from astroid.const import IS_PYPY, PY38, PY39_PLUS, Context
from astroid.manager import AstroidManager
from astroid.nodes import NodeNG
from astroid.nodes.utils import Position
Expand Down Expand Up @@ -74,10 +74,8 @@ def _get_doc(self, node: T_Doc) -> tuple[T_Doc, ast.Constant | ast.Str | None]:
try:
if node.body and isinstance(node.body[0], ast.Expr):
first_value = node.body[0].value
if isinstance(first_value, ast.Str) or (
PY38_PLUS
and isinstance(first_value, ast.Constant)
and isinstance(first_value.value, str)
if isinstance(first_value, ast.Constant) and isinstance(
first_value.value, str
):
doc_ast_node = first_value
node.body = node.body[1:]
Expand Down Expand Up @@ -158,56 +156,6 @@ def _get_position_info(
end_col_offset=t.end[1],
)

def _fix_doc_node_position(self, node: NodesWithDocsType) -> None:
"""Fix start and end position of doc nodes for Python < 3.8."""
if not self._data or not node.doc_node or node.lineno is None:
return
if PY38_PLUS:
return

lineno = node.lineno or 1 # lineno of modules is 0
end_range: int | None = node.doc_node.lineno
if IS_PYPY and not PY39_PLUS:
end_range = None
# pylint: disable-next=unsubscriptable-object
data = "\n".join(self._data[lineno - 1 : end_range])

found_start, found_end = False, False
open_brackets = 0
skip_token: set[int] = {token.NEWLINE, token.INDENT, token.NL, token.COMMENT}

if isinstance(node, nodes.Module):
found_end = True

for t in generate_tokens(StringIO(data).readline):
if found_end is False:
if (
found_start is False
and t.type == token.NAME
and t.string in {"def", "class"}
):
found_start = True
elif found_start is True and t.type == token.OP:
if t.exact_type == token.COLON and open_brackets == 0:
found_end = True
elif t.exact_type == token.LPAR:
open_brackets += 1
elif t.exact_type == token.RPAR:
open_brackets -= 1
continue
if t.type in skip_token:
continue
if t.type == token.STRING:
break
return
else:
return

node.doc_node.lineno = lineno + t.start[0] - 1
node.doc_node.col_offset = t.start[1]
node.doc_node.end_lineno = lineno + t.end[0] - 1
node.doc_node.end_col_offset = t.end[1]

def _reset_end_lineno(self, newnode: nodes.NodeNG) -> None:
"""Reset end_lineno and end_col_offset attributes for PyPy 3.8.
Expand Down Expand Up @@ -246,7 +194,6 @@ def visit_module(
[self.visit(child, newnode) for child in node.body],
doc_node=self.visit(doc_ast_node, newnode),
)
self._fix_doc_node_position(newnode)
if IS_PYPY and PY38:
self._reset_end_lineno(newnode)
return newnode
Expand Down Expand Up @@ -953,7 +900,6 @@ def visit_classdef(
position=self._get_position_info(node, newnode),
doc_node=self.visit(doc_ast_node, newnode),
)
self._fix_doc_node_position(newnode)
return newnode

def visit_continue(self, node: ast.Continue, parent: NodeNG) -> nodes.Continue:
Expand Down Expand Up @@ -1225,7 +1171,7 @@ def _visit_functiondef(
node, doc_ast_node = self._get_doc(node)

lineno = node.lineno
if PY38_PLUS and node.decorator_list:
if node.decorator_list:
# Python 3.8 sets the line number of a decorated function
# to be the actual line number of the function, but the
# previous versions expected the decorator's line number instead.
Expand Down Expand Up @@ -1265,7 +1211,6 @@ def _visit_functiondef(
position=self._get_position_info(node, newnode),
doc_node=self.visit(doc_ast_node, newnode),
)
self._fix_doc_node_position(newnode)
self._global_names.pop()
return newnode

Expand Down
14 changes: 5 additions & 9 deletions tests/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import pytest

from astroid import Instance, builder, nodes, test_utils, util
from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, PYPY_7_3_11_PLUS
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS
from astroid.exceptions import (
AstroidBuildingError,
AstroidSyntaxError,
Expand Down Expand Up @@ -62,10 +62,7 @@ def test_callfunc_lineno(self) -> None:
else:
self.assertEqual(strarg.tolineno, 5)
else:
if not PY38_PLUS:
self.assertEqual(strarg.fromlineno, 5)
else:
self.assertEqual(strarg.fromlineno, 4)
self.assertEqual(strarg.fromlineno, 4)
self.assertEqual(strarg.tolineno, 5)
namearg = callfunc.args[1]
self.assertIsInstance(namearg, nodes.Name)
Expand Down Expand Up @@ -160,8 +157,8 @@ class C: # L13

c = ast_module.body[2]
assert isinstance(c, nodes.ClassDef)
if not PY38_PLUS or IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
# Not perfect, but best we can do for Python 3.7 and PyPy 3.8 (< v7.3.11).
if IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
# Not perfect, but best we can do for PyPy 3.8 (< v7.3.11).
# Can't detect closing bracket on new line.
assert c.fromlineno == 12
else:
Expand Down Expand Up @@ -923,8 +920,7 @@ def test_module_build_dunder_file() -> None:
assert module.path[0] == collections.__file__


@pytest.mark.skipif(
PY38_PLUS,
@pytest.mark.xfail(
reason=(
"The builtin ast module does not fail with a specific error "
"for syntax error caused by invalid type comments."
Expand Down
6 changes: 2 additions & 4 deletions tests/test_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from astroid.arguments import CallSite
from astroid.bases import BoundMethod, Instance, UnboundMethod, UnionType
from astroid.builder import AstroidBuilder, _extract_single_node, extract_node, parse
from astroid.const import PY38_PLUS, PY39_PLUS, PY310_PLUS
from astroid.const import PY39_PLUS, PY310_PLUS
from astroid.context import CallContext, InferenceContext
from astroid.exceptions import (
AstroidTypeError,
Expand Down Expand Up @@ -959,8 +959,7 @@ class D(C):
self.assertEqual("module.C", should_be_c[0].qname())
self.assertEqual("module.D", should_be_d[0].qname())

@pytest.mark.skipif(
PY38_PLUS,
@pytest.mark.xfail(
reason="pathlib.Path cannot be inferred on Python 3.8",
)
def test_factory_methods_inside_binary_operation(self):
Expand Down Expand Up @@ -6588,7 +6587,6 @@ def test_custom_decorators_for_classmethod_and_staticmethods(code, obj, obj_type
assert inferred.type == obj_type


@pytest.mark.skipif(not PY38_PLUS, reason="Needs dataclasses available")
@pytest.mark.skipif(
PY39_PLUS,
reason="Exact inference with dataclasses (replace function) in python3.9",
Expand Down

0 comments on commit 1336ee4

Please sign in to comment.