summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-06-17 23:23:35 +0200
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-06-19 09:13:16 +0200
commit87b5d88193b71637271ad9aa71d9bb8cef41179c (patch)
tree4b238b9718d8812915f4851bc0a279c77d3a4505
parent1e9a3d18e99010a843e6d0cb06a598ab9ba7a986 (diff)
downloadastroid-git-87b5d88193b71637271ad9aa71d9bb8cef41179c.tar.gz
Create a permanent enum for Context instead of deleting the class
-rw-r--r--astroid/__init__.py9
-rw-r--r--astroid/_ast.py15
-rw-r--r--astroid/constants.py12
-rw-r--r--astroid/protocols.py3
-rw-r--r--astroid/rebuilder.py16
-rw-r--r--tests/unittest_nodes.py18
6 files changed, 40 insertions, 33 deletions
diff --git a/astroid/__init__.py b/astroid/__init__.py
index 087282d0..665bf09d 100644
--- a/astroid/__init__.py
+++ b/astroid/__init__.py
@@ -39,7 +39,6 @@ Main modules are:
* builder contains the class responsible to build astroid trees
"""
-import enum
import os
from importlib import import_module
@@ -48,12 +47,6 @@ from pathlib import Path
from .__pkginfo__ import __version__, version
-_Context = enum.Enum("Context", "Load Store Del")
-Load = _Context.Load
-Store = _Context.Store
-Del = _Context.Del
-del _Context
-
# WARNING: internal imports order matters !
# pylint: disable=wrong-import-order,wrong-import-position,redefined-builtin
@@ -69,6 +62,8 @@ from astroid import inference
# more stuff available
from astroid import raw_building
+
+from astroid.constants import Load, Del, Store
from astroid.inference_tip import _inference_tip_cached, inference_tip
from astroid.bases import BaseInstance, Instance, BoundMethod, UnboundMethod
from astroid.node_classes import are_exclusive, unpack_infer
diff --git a/astroid/_ast.py b/astroid/_ast.py
index 8bafa799..b569727e 100644
--- a/astroid/_ast.py
+++ b/astroid/_ast.py
@@ -2,10 +2,9 @@ import ast
import sys
from collections import namedtuple
from functools import partial
-from typing import Optional
+from typing import Dict, Optional
-import astroid
-from astroid.constants import PY38
+from astroid.constants import PY38, Context
try:
import typed_ast.ast3 as _ast_py3
@@ -121,10 +120,10 @@ def _compare_operators_from_module(module):
}
-def _contexts_from_module(module):
+def _contexts_from_module(module) -> Context:
return {
- module.Load: astroid.Load,
- module.Store: astroid.Store,
- module.Del: astroid.Del,
- module.Param: astroid.Store,
+ module.Load: Context.Load,
+ module.Store: Context.Store,
+ module.Del: Context.Del,
+ module.Param: Context.Store,
}
diff --git a/astroid/constants.py b/astroid/constants.py
index 3b0b3c67..dd0e29a3 100644
--- a/astroid/constants.py
+++ b/astroid/constants.py
@@ -1,6 +1,18 @@
+import enum
import sys
PY37 = sys.version_info >= (3, 7)
PY38 = sys.version_info >= (3, 8)
PY39 = sys.version_info >= (3, 9)
PY310 = sys.version_info >= (3, 10)
+
+
+class Context(enum.Enum):
+ Load = 1
+ Store = 2
+ Del = 3
+
+
+Load: Context = Context.Load
+Store: Context = Context.Store
+Del: Context = Context.Del
diff --git a/astroid/protocols.py b/astroid/protocols.py
index 7967949a..54fadf5a 100644
--- a/astroid/protocols.py
+++ b/astroid/protocols.py
@@ -29,9 +29,10 @@ import collections
import itertools
import operator as operator_mod
-from astroid import Store, arguments, bases
+from astroid import arguments, bases
from astroid import context as contextmod
from astroid import decorators, helpers, node_classes, nodes, util
+from astroid.constants import Store
from astroid.exceptions import (
AstroidIndexError,
AstroidTypeError,
diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py
index d71f69b9..139a1ed4 100644
--- a/astroid/rebuilder.py
+++ b/astroid/rebuilder.py
@@ -44,7 +44,7 @@ from typing import (
overload,
)
-from astroid.constants import PY37, PY38, PY39
+from astroid.constants import PY37, PY38, PY39, Del, Load, Store
try:
from typing import Final
@@ -135,7 +135,7 @@ class TreeRebuilder:
"ast.Tuple",
],
): # TODO return type needs change to _Context enum
- return self._parser_module.context_classes.get(type(node.ctx), astroid.Load)
+ return self._parser_module.context_classes.get(type(node.ctx), Load)
def visit_module(
self, node: "ast.Module", modname: str, modpath: str, package: bool
@@ -900,7 +900,7 @@ class TreeRebuilder:
self, node: "ast.ExtSlice", parent: nodes.Subscript
) -> nodes.Tuple:
"""visit an ExtSlice node by returning a fresh instance of Tuple"""
- newnode = nodes.Tuple(ctx=astroid.Load, parent=parent)
+ newnode = nodes.Tuple(ctx=Load, parent=parent)
newnode.postinit([self.visit(dim, newnode) for dim in node.dims]) # type: ignore
return newnode
@@ -1034,11 +1034,11 @@ class TreeRebuilder:
) -> Union[nodes.Attribute, nodes.AssignAttr, nodes.DelAttr]:
"""visit an Attribute node by returning a fresh instance of it"""
context = self._get_context(node)
- if context == astroid.Del:
+ if context == Del:
# FIXME : maybe we should reintroduce and visit_delattr ?
# for instance, deactivating assign_ctx
newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset, parent)
- elif context == astroid.Store:
+ elif context == Store:
newnode = nodes.AssignAttr(node.attr, node.lineno, node.col_offset, parent)
# Prohibit a local save if we are in an ExceptHandler.
if not isinstance(parent, astroid.ExceptHandler):
@@ -1162,14 +1162,14 @@ class TreeRebuilder:
) -> Union[nodes.Name, nodes.Const, nodes.AssignName, nodes.DelName]:
"""visit a Name node by returning a fresh instance of it"""
context = self._get_context(node)
- if context == astroid.Del:
+ if context == Del:
newnode = nodes.DelName(node.id, node.lineno, node.col_offset, parent)
- elif context == astroid.Store:
+ elif context == Store:
newnode = nodes.AssignName(node.id, node.lineno, node.col_offset, parent)
else:
newnode = nodes.Name(node.id, node.lineno, node.col_offset, parent)
# XXX REMOVE me :
- if context in (astroid.Del, astroid.Store): # 'Aug' ??
+ if context in (Del, Store): # 'Aug' ??
newnode = cast(Union[nodes.AssignName, nodes.DelName], newnode)
self._save_assignment(newnode)
return newnode
diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py
index 3c67bbe4..e059b7b7 100644
--- a/tests/unittest_nodes.py
+++ b/tests/unittest_nodes.py
@@ -40,7 +40,7 @@ import astroid
from astroid import bases, builder
from astroid import context as contextmod
from astroid import node_classes, nodes, parse, test_utils, transforms, util
-from astroid.constants import PY38, PY310
+from astroid.constants import PY38, PY310, Del, Load, Store
from astroid.exceptions import (
AstroidBuildingError,
AstroidSyntaxError,
@@ -928,24 +928,24 @@ class Python35AsyncTest(unittest.TestCase):
class ContextTest(unittest.TestCase):
def test_subscript_load(self):
node = builder.extract_node("f[1]")
- self.assertIs(node.ctx, astroid.Load)
+ self.assertIs(node.ctx, Load)
def test_subscript_del(self):
node = builder.extract_node("del f[1]")
- self.assertIs(node.targets[0].ctx, astroid.Del)
+ self.assertIs(node.targets[0].ctx, Del)
def test_subscript_store(self):
node = builder.extract_node("f[1] = 2")
subscript = node.targets[0]
- self.assertIs(subscript.ctx, astroid.Store)
+ self.assertIs(subscript.ctx, Store)
def test_list_load(self):
node = builder.extract_node("[]")
- self.assertIs(node.ctx, astroid.Load)
+ self.assertIs(node.ctx, Load)
def test_list_del(self):
node = builder.extract_node("del []")
- self.assertIs(node.targets[0].ctx, astroid.Del)
+ self.assertIs(node.targets[0].ctx, Del)
def test_list_store(self):
with self.assertRaises(AstroidSyntaxError):
@@ -953,7 +953,7 @@ class ContextTest(unittest.TestCase):
def test_tuple_load(self):
node = builder.extract_node("(1, )")
- self.assertIs(node.ctx, astroid.Load)
+ self.assertIs(node.ctx, Load)
def test_tuple_store(self):
with self.assertRaises(AstroidSyntaxError):
@@ -962,12 +962,12 @@ class ContextTest(unittest.TestCase):
def test_starred_load(self):
node = builder.extract_node("a = *b")
starred = node.value
- self.assertIs(starred.ctx, astroid.Load)
+ self.assertIs(starred.ctx, Load)
def test_starred_store(self):
node = builder.extract_node("a, *b = 1, 2")
starred = node.targets[0].elts[1]
- self.assertIs(starred.ctx, astroid.Store)
+ self.assertIs(starred.ctx, Store)
def test_unknown():