From 331644a677506a6ed482085740b39eb90387742f Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Fri, 12 Nov 2021 17:17:51 +0100 Subject: [PATCH 1/3] Fix F403 wildcart import used in astroid/__init__.py And apply flake8 on setup.py and astroid/__init__.py --- .pre-commit-config.yaml | 2 +- astroid/__init__.py | 31 ++++++++++++++++++++++++++++++- astroid/exceptions.py | 2 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6c29a7420d..5d4cb02bb8 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -47,7 +47,7 @@ repos: hooks: - id: flake8 additional_dependencies: [flake8-bugbear, flake8-typing-imports==1.11.0] - exclude: tests/testdata|doc/conf.py|astroid/__init__.py + exclude: tests/testdata|doc/conf.py - repo: local hooks: - id: pylint diff --git a/astroid/__init__.py b/astroid/__init__.py index a16a281512..62cc54f7a0 100644 --- a/astroid/__init__.py +++ b/astroid/__init__.py @@ -58,7 +58,36 @@ from astroid.brain.helpers import register_module_extender from astroid.builder import extract_node, parse from astroid.const import Context, Del, Load, Store -from astroid.exceptions import * +from astroid.exceptions import ( + AstroidBuildingError, + AstroidBuildingException, + AstroidError, + AstroidImportError, + AstroidIndexError, + AstroidSyntaxError, + AstroidTypeError, + AstroidValueError, + AttributeInferenceError, + BinaryOperationError, + DuplicateBasesError, + InconsistentMroError, + InferenceError, + InferenceOverwriteError, + MroError, + NameInferenceError, + NoDefault, + NotFoundError, + OperationError, + ParentMissingError, + ResolveError, + StatementMissing, + SuperArgumentTypeError, + SuperError, + TooManyLevelsError, + UnaryOperationError, + UnresolvableName, + UseInferenceDefault, +) from astroid.inference_tip import _inference_tip_cached, inference_tip from astroid.objects import ExceptionInstance diff --git a/astroid/exceptions.py b/astroid/exceptions.py index b8838023e4..9aaaaa539c 100644 --- a/astroid/exceptions.py +++ b/astroid/exceptions.py @@ -42,7 +42,9 @@ "NoDefault", "NotFoundError", "OperationError", + "ParentMissingError", "ResolveError", + "StatementMissing", "SuperArgumentTypeError", "SuperError", "TooManyLevelsError", From d52f0302a76c85ef4af895498a76e9b9c4e3f6bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Tue, 1 Mar 2022 10:42:15 +0100 Subject: [PATCH 2/3] Fix issues --- astroid/inference.py | 21 +++++++++++---------- astroid/nodes/scoped_nodes/scoped_nodes.py | 2 -- astroid/rebuilder.py | 2 ++ pylintrc | 2 +- tests/unittest_builder.py | 1 + tests/unittest_lookup.py | 1 + tests/unittest_nodes.py | 1 + tests/unittest_scoped_nodes.py | 2 +- 8 files changed, 18 insertions(+), 14 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index 384d4e6346..f8fa66a758 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -34,7 +34,7 @@ import functools import itertools import operator -from typing import Any, Callable, Dict, Iterable, Optional +from typing import Any, Callable, Dict, Iterable, Iterator, Optional, Union import wrapt @@ -824,7 +824,7 @@ def _to_literal(node: nodes.NodeNG) -> Any: def _do_compare( left_iter: Iterable[nodes.NodeNG], op: str, right_iter: Iterable[nodes.NodeNG] -) -> "bool | type[util.Uninferable]": +) -> Union[bool, util.Uninferable]: """ If all possible combinations are either True or False, return that: >>> _do_compare([1, 2], '<=', [3, 4]) @@ -839,17 +839,17 @@ def _do_compare( """ retval = None if op in UNINFERABLE_OPS: - return util.Uninferable + return util.Uninferable # type: ignore[return-value] op_func = COMPARE_OPS[op] for left, right in itertools.product(left_iter, right_iter): if left is util.Uninferable or right is util.Uninferable: - return util.Uninferable + return util.Uninferable # type: ignore[return-value] try: left, right = _to_literal(left), _to_literal(right) except (SyntaxError, ValueError, AttributeError): - return util.Uninferable + return util.Uninferable # type: ignore[return-value] try: expr = op_func(left, right) @@ -859,17 +859,18 @@ def _do_compare( if retval is None: retval = expr elif retval != expr: - return util.Uninferable + return util.Uninferable # type: ignore[return-value] # (or both, but "True | False" is basically the same) + assert retval is not None return retval # it was all the same value def _infer_compare( self: nodes.Compare, context: Optional[InferenceContext] = None -) -> Any: +) -> Iterator[Union[nodes.Const, util.Uninferable]]: """Chained comparison inference logic.""" - retval = True + retval: Union[bool, util.Uninferable] = True ops = self.ops left_node = self.left @@ -881,13 +882,13 @@ def _infer_compare( try: retval = _do_compare(lhs, op, rhs) except AstroidTypeError: - retval = util.Uninferable + retval = util.Uninferable # type: ignore[assignment] break if retval is not True: break # short-circuit lhs = rhs # continue if retval is util.Uninferable: - yield retval + yield retval # type: ignore[misc] else: yield nodes.Const(retval) diff --git a/astroid/nodes/scoped_nodes/scoped_nodes.py b/astroid/nodes/scoped_nodes/scoped_nodes.py index a8dcf3549f..b6eb14018a 100644 --- a/astroid/nodes/scoped_nodes/scoped_nodes.py +++ b/astroid/nodes/scoped_nodes/scoped_nodes.py @@ -630,8 +630,6 @@ def fully_defined(self): def statement(self, *, future: Literal[None] = ...) -> "Module": ... - # pylint: disable-next=arguments-differ - # https://github.com/PyCQA/pylint/issues/5264 @overload def statement(self, *, future: Literal[True]) -> NoReturn: ... diff --git a/astroid/rebuilder.py b/astroid/rebuilder.py index 141494ea72..52cd8f7f13 100644 --- a/astroid/rebuilder.py +++ b/astroid/rebuilder.py @@ -1750,6 +1750,7 @@ def visit_attribute( newnode = nodes.DelAttr(node.attr, node.lineno, node.col_offset, parent) elif context == Context.Store: if sys.version_info >= (3, 8): + # pylint: disable=redefined-variable-type newnode = nodes.AssignAttr( attrname=node.attr, lineno=node.lineno, @@ -2018,6 +2019,7 @@ def visit_name( newnode = nodes.DelName(node.id, node.lineno, node.col_offset, parent) elif context == Context.Store: if sys.version_info >= (3, 8): + # pylint: disable=redefined-variable-type newnode = nodes.AssignName( name=node.id, lineno=node.lineno, diff --git a/pylintrc b/pylintrc index 2ca5a62dd2..6f9dc1f37e 100644 --- a/pylintrc +++ b/pylintrc @@ -310,7 +310,7 @@ mixin-class-rgx=.*Mix[Ii]n # List of members which are set dynamically and missed by pylint inference # system, and so shouldn't trigger E0201 when accessed. Python regular # expressions are accepted. -generated-members=REQUEST,acl_users,aq_parent +generated-members=REQUEST,acl_users,aq_parent,argparse.Namespace [VARIABLES] diff --git a/tests/unittest_builder.py b/tests/unittest_builder.py index ad61b12784..e5ff073723 100644 --- a/tests/unittest_builder.py +++ b/tests/unittest_builder.py @@ -607,6 +607,7 @@ def func2(a={}): a.custom_attr = 0 """ builder.parse(code) + # pylint: disable=no-member nonetype = nodes.const_factory(None) self.assertNotIn("custom_attr", nonetype.locals) self.assertNotIn("custom_attr", nonetype.instance_attrs) diff --git a/tests/unittest_lookup.py b/tests/unittest_lookup.py index 1555603b9e..19d87432b8 100644 --- a/tests/unittest_lookup.py +++ b/tests/unittest_lookup.py @@ -394,6 +394,7 @@ def test_builtin_lookup(self) -> None: self.assertEqual(len(intstmts), 1) self.assertIsInstance(intstmts[0], nodes.ClassDef) self.assertEqual(intstmts[0].name, "int") + # pylint: disable=no-member self.assertIs(intstmts[0], nodes.const_factory(1)._proxied) def test_decorator_arguments_lookup(self) -> None: diff --git a/tests/unittest_nodes.py b/tests/unittest_nodes.py index a7735c5c6e..f07fd85791 100644 --- a/tests/unittest_nodes.py +++ b/tests/unittest_nodes.py @@ -633,6 +633,7 @@ def test_as_string(self) -> None: class ConstNodeTest(unittest.TestCase): def _test(self, value: Any) -> None: node = nodes.const_factory(value) + # pylint: disable=no-member self.assertIsInstance(node._proxied, nodes.ClassDef) self.assertEqual(node._proxied.name, value.__class__.__name__) self.assertIs(node.value, value) diff --git a/tests/unittest_scoped_nodes.py b/tests/unittest_scoped_nodes.py index 6a95385cec..251186626b 100644 --- a/tests/unittest_scoped_nodes.py +++ b/tests/unittest_scoped_nodes.py @@ -283,7 +283,7 @@ def test_file_stream_api(self) -> None: path = resources.find("data/all.py") file_build = builder.AstroidBuilder().file_build(path, "all") with self.assertRaises(AttributeError): - # pylint: disable=pointless-statement + # pylint: disable=pointless-statement, no-member file_build.file_stream def test_stream_api(self) -> None: From 7e11a1ccfc74860fc1cd9f0434ba5a188247898d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Noord?= <13665637+DanielNoord@users.noreply.github.com> Date: Thu, 3 Mar 2022 12:38:22 +0100 Subject: [PATCH 3/3] Remove unnecessary changes --- astroid/inference.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/astroid/inference.py b/astroid/inference.py index cb820622cd..1fb4e93ccc 100644 --- a/astroid/inference.py +++ b/astroid/inference.py @@ -824,7 +824,7 @@ def _to_literal(node: nodes.NodeNG) -> Any: def _do_compare( left_iter: Iterable[nodes.NodeNG], op: str, right_iter: Iterable[nodes.NodeNG] -) -> Union[bool, util.Uninferable]: +) -> "bool | type[util.Uninferable]": """ If all possible combinations are either True or False, return that: >>> _do_compare([1, 2], '<=', [3, 4]) @@ -839,17 +839,17 @@ def _do_compare( """ retval: Union[None, bool] = None if op in UNINFERABLE_OPS: - return util.Uninferable # type: ignore[return-value] + return util.Uninferable op_func = COMPARE_OPS[op] for left, right in itertools.product(left_iter, right_iter): if left is util.Uninferable or right is util.Uninferable: - return util.Uninferable # type: ignore[return-value] + return util.Uninferable try: left, right = _to_literal(left), _to_literal(right) except (SyntaxError, ValueError, AttributeError): - return util.Uninferable # type: ignore[return-value] + return util.Uninferable try: expr = op_func(left, right) @@ -859,7 +859,7 @@ def _do_compare( if retval is None: retval = expr elif retval != expr: - return util.Uninferable # type: ignore[return-value] + return util.Uninferable # (or both, but "True | False" is basically the same) assert retval is not None @@ -882,7 +882,7 @@ def _infer_compare( try: retval = _do_compare(lhs, op, rhs) except AstroidTypeError: - retval = util.Uninferable # type: ignore[assignment] + retval = util.Uninferable break if retval is not True: break # short-circuit