diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2019-02-11 08:36:11 +0100 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2019-02-11 08:37:32 +0100 |
commit | 7a40b0b6d3113403a266ffef13431cdc8e6a8327 (patch) | |
tree | 1234156b8710a6ca817a72e7975d034dd4c19209 | |
parent | e552b81c5a61c08ff846d07611c1bae33718b49a (diff) | |
download | pylint-git-7a40b0b6d3113403a266ffef13431cdc8e6a8327.tar.gz |
Don't emit ``*-not-iterating`` checks for builtins consumed by ``itertools``
Close #2731
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | pylint/checkers/python3.py | 16 | ||||
-rw-r--r-- | pylint/test/unittest_checker_python3.py | 17 |
3 files changed, 30 insertions, 8 deletions
@@ -7,6 +7,11 @@ What's New in Pylint 2.3.0? Release date: TBA + +* Don't emit ``*-not-iterating`` checks for builtins consumed by ``itertools`` + + Close #2731 + * Fix a crash caused by iterating over ``Uninferable`` in a string formatting check. Close #2727 diff --git a/pylint/checkers/python3.py b/pylint/checkers/python3.py index 93be76844..c8e1dd92d 100644 --- a/pylint/checkers/python3.py +++ b/pylint/checkers/python3.py @@ -120,14 +120,14 @@ def _in_iterating_context(node): elif isinstance(parent.func, astroid.Attribute): if parent.func.attrname in ATTRIBUTES_ACCEPTS_ITERATOR: return True - try: - inferred = next(parent.func.infer()) - except astroid.InferenceError: - pass - else: - if inferred is not astroid.Uninferable: - if inferred.qname() in _BUILTIN_METHOD_ACCEPTS_ITERATOR: - return True + + inferred = utils.safe_infer(parent.func) + if inferred: + if inferred.qname() in _BUILTIN_METHOD_ACCEPTS_ITERATOR: + return True + root = inferred.root() + if root and root.name == "itertools": + return True # If the call is in an unpacking, there's no need to warn, # since it can be considered iterating. elif isinstance(parent, astroid.Assign) and isinstance( diff --git a/pylint/test/unittest_checker_python3.py b/pylint/test/unittest_checker_python3.py index 623e35961..8354ed82e 100644 --- a/pylint/test/unittest_checker_python3.py +++ b/pylint/test/unittest_checker_python3.py @@ -177,6 +177,22 @@ class TestPython3Checker(testutils.CheckerTestCase): with self.assertNoMessages(): self.walk(module) + def as_argument_to_itertools_functions(self, fxn): + code = """ + from __future__ import absolute_import + import itertools + from itertools import product + for i,j in product({fxn}(), repeat=2): + pass + for i,j in itertools.product({fxn}(), repeat=2): + pass + """.format( + fxn=fxn + ) + module = astroid.parse(code) + with self.assertNoMessages(): + self.walk(module) + def as_iterable_in_unpacking(self, fxn): node = astroid.extract_node( """ @@ -216,6 +232,7 @@ class TestPython3Checker(testutils.CheckerTestCase): self.as_argument_to_materialized_filter(fxn) self.as_iterable_in_yield_from(fxn) self.as_iterable_in_starred_context(fxn) + self.as_argument_to_itertools_functions(fxn) for func in ( "iter", |