Skip to content

Commit 1336ee4

Browse files
authored
Remove unused constants (#2141)
* Remove `PY38_PLUS` constant * Remove `BUILTINS` constants * Remove `Load` + `Store` + `Del` * Remove `BOOL_SPECIAL_METHOD`
1 parent 7fa8481 commit 1336ee4

17 files changed

+39
-146
lines changed

ChangeLog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,17 @@ Release date: TBA
8888

8989
Refs #1490
9090

91+
* Remove unused and / or deprecated constants:
92+
- ``astroid.bases.BOOL_SPECIAL_METHOD``
93+
- ``astroid.bases.BUILTINS``
94+
- ``astroid.const.BUILTINS``
95+
- ``astroid.const.PY38_PLUS``
96+
- ``astroid.const.Load``
97+
- ``astroid.const.Store``
98+
- ``astroid.const.Del``
99+
100+
Refs #2141
101+
91102

92103
What's New in astroid 2.15.4?
93104
=============================

astroid/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
from astroid.bases import BaseInstance, BoundMethod, Instance, UnboundMethod
4747
from astroid.brain.helpers import register_module_extender
4848
from astroid.builder import extract_node, parse
49-
from astroid.const import BRAIN_MODULES_DIRECTORY, PY310_PLUS, Context, Del, Load, Store
49+
from astroid.const import BRAIN_MODULES_DIRECTORY, PY310_PLUS, Context
5050
from astroid.exceptions import (
5151
AstroidBuildingError,
5252
AstroidBuildingException,

astroid/bases.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@
3939
from astroid.constraint import Constraint
4040

4141

42-
# TODO: check if needs special treatment
43-
BOOL_SPECIAL_METHOD = "__bool__"
44-
BUILTINS = "builtins" # TODO Remove in 2.8
45-
4642
PROPERTIES = {"builtins.property", "abc.abstractproperty"}
4743
if PY310_PLUS:
4844
PROPERTIES.add("enum.property")
@@ -383,7 +379,7 @@ def bool_value(
383379
context.boundnode = self
384380

385381
try:
386-
result = _infer_method_result_truth(self, BOOL_SPECIAL_METHOD, context)
382+
result = _infer_method_result_truth(self, "__bool__", context)
387383
except (InferenceError, AttributeInferenceError):
388384
# Fallback to __len__.
389385
try:

astroid/brain/brain_ssl.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from astroid import parse
88
from astroid.brain.helpers import register_module_extender
9-
from astroid.const import PY38_PLUS, PY310_PLUS
9+
from astroid.const import PY310_PLUS
1010
from astroid.manager import AstroidManager
1111

1212

@@ -41,9 +41,7 @@ class Options(_IntFlag):
4141
OP_SINGLE_ECDH_USE = 10
4242
OP_NO_COMPRESSION = 11
4343
OP_NO_TICKET = 12
44-
OP_NO_RENEGOTIATION = 13"""
45-
if PY38_PLUS:
46-
enum += """
44+
OP_NO_RENEGOTIATION = 13
4745
OP_ENABLE_MIDDLEBOX_COMPAT = 14"""
4846
return enum
4947

astroid/brain/brain_typing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from astroid import context, extract_node, inference_tip
1515
from astroid.builder import _extract_single_node
16-
from astroid.const import PY38_PLUS, PY39_PLUS
16+
from astroid.const import PY39_PLUS
1717
from astroid.exceptions import (
1818
AttributeInferenceError,
1919
InferenceError,
@@ -428,7 +428,7 @@ def infer_typing_cast(
428428
AstroidManager().register_transform(
429429
FunctionDef, inference_tip(infer_typedDict), _looks_like_typedDict
430430
)
431-
elif PY38_PLUS:
431+
else:
432432
AstroidManager().register_transform(
433433
ClassDef, inference_tip(infer_old_typedDict), _looks_like_typedDict
434434
)

astroid/brain/brain_unittest.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
"""Astroid hooks for unittest module."""
66
from astroid.brain.helpers import register_module_extender
77
from astroid.builder import parse
8-
from astroid.const import PY38_PLUS
98
from astroid.manager import AstroidManager
109

1110

@@ -27,7 +26,4 @@ def IsolatedAsyncioTestCaseImport():
2726
)
2827

2928

30-
if PY38_PLUS:
31-
register_module_extender(
32-
AstroidManager(), "unittest", IsolatedAsyncioTestCaseImport
33-
)
29+
register_module_extender(AstroidManager(), "unittest", IsolatedAsyncioTestCaseImport)

astroid/const.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
from pathlib import Path
88

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

1614
WIN32 = sys.platform == "win32"
1715

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

3028

31-
# TODO Remove in 3.0 in favor of Context
32-
Load = Context.Load
33-
Store = Context.Store
34-
Del = Context.Del
35-
36-
3729
ASTROID_INSTALL_DIRECTORY = Path(__file__).parent
3830
BRAIN_MODULES_DIRECTORY = ASTROID_INSTALL_DIRECTORY / "brain"
3931

astroid/nodes/scoped_nodes/scoped_nodes.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import TYPE_CHECKING, ClassVar, Literal, NoReturn, TypeVar, overload
2020

2121
from astroid import bases, util
22-
from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, PYPY_7_3_11_PLUS
22+
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS
2323
from astroid.context import (
2424
CallContext,
2525
InferenceContext,
@@ -2000,7 +2000,7 @@ def fromlineno(self) -> int:
20002000
20012001
Can also return 0 if the line can not be determined.
20022002
"""
2003-
if not PY38_PLUS or IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
2003+
if IS_PYPY and PY38 and not PYPY_7_3_11_PLUS:
20042004
# For Python < 3.8 the lineno is the line number of the first decorator.
20052005
# We want the class statement lineno. Similar to 'FunctionDef.fromlineno'
20062006
# PyPy (3.8): Fixed with version v7.3.11

astroid/rebuilder.py

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
from astroid import nodes
2020
from astroid._ast import ParserModule, get_parser_module, parse_function_type_comment
21-
from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, Context
21+
from astroid.const import IS_PYPY, PY38, PY39_PLUS, Context
2222
from astroid.manager import AstroidManager
2323
from astroid.nodes import NodeNG
2424
from astroid.nodes.utils import Position
@@ -74,10 +74,8 @@ def _get_doc(self, node: T_Doc) -> tuple[T_Doc, ast.Constant | ast.Str | None]:
7474
try:
7575
if node.body and isinstance(node.body[0], ast.Expr):
7676
first_value = node.body[0].value
77-
if isinstance(first_value, ast.Str) or (
78-
PY38_PLUS
79-
and isinstance(first_value, ast.Constant)
80-
and isinstance(first_value.value, str)
77+
if isinstance(first_value, ast.Constant) and isinstance(
78+
first_value.value, str
8179
):
8280
doc_ast_node = first_value
8381
node.body = node.body[1:]
@@ -158,56 +156,6 @@ def _get_position_info(
158156
end_col_offset=t.end[1],
159157
)
160158

161-
def _fix_doc_node_position(self, node: NodesWithDocsType) -> None:
162-
"""Fix start and end position of doc nodes for Python < 3.8."""
163-
if not self._data or not node.doc_node or node.lineno is None:
164-
return
165-
if PY38_PLUS:
166-
return
167-
168-
lineno = node.lineno or 1 # lineno of modules is 0
169-
end_range: int | None = node.doc_node.lineno
170-
if IS_PYPY and not PY39_PLUS:
171-
end_range = None
172-
# pylint: disable-next=unsubscriptable-object
173-
data = "\n".join(self._data[lineno - 1 : end_range])
174-
175-
found_start, found_end = False, False
176-
open_brackets = 0
177-
skip_token: set[int] = {token.NEWLINE, token.INDENT, token.NL, token.COMMENT}
178-
179-
if isinstance(node, nodes.Module):
180-
found_end = True
181-
182-
for t in generate_tokens(StringIO(data).readline):
183-
if found_end is False:
184-
if (
185-
found_start is False
186-
and t.type == token.NAME
187-
and t.string in {"def", "class"}
188-
):
189-
found_start = True
190-
elif found_start is True and t.type == token.OP:
191-
if t.exact_type == token.COLON and open_brackets == 0:
192-
found_end = True
193-
elif t.exact_type == token.LPAR:
194-
open_brackets += 1
195-
elif t.exact_type == token.RPAR:
196-
open_brackets -= 1
197-
continue
198-
if t.type in skip_token:
199-
continue
200-
if t.type == token.STRING:
201-
break
202-
return
203-
else:
204-
return
205-
206-
node.doc_node.lineno = lineno + t.start[0] - 1
207-
node.doc_node.col_offset = t.start[1]
208-
node.doc_node.end_lineno = lineno + t.end[0] - 1
209-
node.doc_node.end_col_offset = t.end[1]
210-
211159
def _reset_end_lineno(self, newnode: nodes.NodeNG) -> None:
212160
"""Reset end_lineno and end_col_offset attributes for PyPy 3.8.
213161
@@ -246,7 +194,6 @@ def visit_module(
246194
[self.visit(child, newnode) for child in node.body],
247195
doc_node=self.visit(doc_ast_node, newnode),
248196
)
249-
self._fix_doc_node_position(newnode)
250197
if IS_PYPY and PY38:
251198
self._reset_end_lineno(newnode)
252199
return newnode
@@ -953,7 +900,6 @@ def visit_classdef(
953900
position=self._get_position_info(node, newnode),
954901
doc_node=self.visit(doc_ast_node, newnode),
955902
)
956-
self._fix_doc_node_position(newnode)
957903
return newnode
958904

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

12271173
lineno = node.lineno
1228-
if PY38_PLUS and node.decorator_list:
1174+
if node.decorator_list:
12291175
# Python 3.8 sets the line number of a decorated function
12301176
# to be the actual line number of the function, but the
12311177
# previous versions expected the decorator's line number instead.
@@ -1265,7 +1211,6 @@ def _visit_functiondef(
12651211
position=self._get_position_info(node, newnode),
12661212
doc_node=self.visit(doc_ast_node, newnode),
12671213
)
1268-
self._fix_doc_node_position(newnode)
12691214
self._global_names.pop()
12701215
return newnode
12711216

tests/test_builder.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import pytest
2020

2121
from astroid import Instance, builder, nodes, test_utils, util
22-
from astroid.const import IS_PYPY, PY38, PY38_PLUS, PY39_PLUS, PYPY_7_3_11_PLUS
22+
from astroid.const import IS_PYPY, PY38, PY39_PLUS, PYPY_7_3_11_PLUS
2323
from astroid.exceptions import (
2424
AstroidBuildingError,
2525
AstroidSyntaxError,
@@ -62,10 +62,7 @@ def test_callfunc_lineno(self) -> None:
6262
else:
6363
self.assertEqual(strarg.tolineno, 5)
6464
else:
65-
if not PY38_PLUS:
66-
self.assertEqual(strarg.fromlineno, 5)
67-
else:
68-
self.assertEqual(strarg.fromlineno, 4)
65+
self.assertEqual(strarg.fromlineno, 4)
6966
self.assertEqual(strarg.tolineno, 5)
7067
namearg = callfunc.args[1]
7168
self.assertIsInstance(namearg, nodes.Name)
@@ -160,8 +157,8 @@ class C: # L13
160157

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

925922

926-
@pytest.mark.skipif(
927-
PY38_PLUS,
923+
@pytest.mark.xfail(
928924
reason=(
929925
"The builtin ast module does not fail with a specific error "
930926
"for syntax error caused by invalid type comments."

0 commit comments

Comments
 (0)