diff options
Diffstat (limited to 'pylint/test/functional/iterable_context.py')
-rw-r--r-- | pylint/test/functional/iterable_context.py | 77 |
1 files changed, 48 insertions, 29 deletions
diff --git a/pylint/test/functional/iterable_context.py b/pylint/test/functional/iterable_context.py index 0571583..47400f9 100644 --- a/pylint/test/functional/iterable_context.py +++ b/pylint/test/functional/iterable_context.py @@ -2,32 +2,28 @@ Checks that primitive values are not used in an iterating/mapping context. """ -# pylint: disable=missing-docstring,invalid-name,too-few-public-methods,no-init +# pylint: disable=missing-docstring,invalid-name,too-few-public-methods,no-init,no-self-use from __future__ import print_function -# for-statement -for i in 42: # [not-an-iterable] - pass +# primitives +numbers = [1, 2, 3] -for i in True: # [not-an-iterable] +for i in numbers: pass -for i in range(10): +for i in iter(numbers): pass -for i in ''.join(x ** 2 for x in range(10)): +for i in "123": pass -numbers = [1, 2, 3] -inumbers = iter(numbers) - -for i in inumbers: +for i in u"123": pass -for i in "123": +for i in b"123": pass -for i in u"123": +for i in bytearray(b"123"): pass for i in set(numbers): @@ -39,6 +35,7 @@ for i in frozenset(numbers): for i in dict(a=1, b=2): pass +# comprehensions for i in [x for x in range(10)]: pass @@ -48,6 +45,7 @@ for i in {x for x in range(1, 100, 2)}: for i in {x: 10 - x for x in range(10)}: pass +# generators def powers_of_two(): k = 0 while k < 10: @@ -57,9 +55,25 @@ def powers_of_two(): for i in powers_of_two(): pass +for i in powers_of_two: # [not-an-iterable] + pass + +# check for custom iterators +class A(object): + pass + +class B(object): + def __iter__(self): + return self + + def __next__(self): + return 1 + + def next(self): + return 1 -# check for old-style iterator class C(object): + "old-style iterator" def __getitem__(self, k): if k > 10: raise IndexError @@ -72,27 +86,32 @@ for i in C(): print(i) -# check for custom iterators -class A: - pass - +def test(*args): + print(args) -class B: - def __iter__(self): - return self - def __next__(self): - return 1 +test(*A()) # [not-an-iterable] +test(*B()) +test(*B) # [not-an-iterable] +for i in A(): # [not-an-iterable] + pass +for i in B(): + pass +for i in B: # [not-an-iterable] + pass - def next(self): - return 1 +for i in range: # [not-an-iterable] + pass +# check that primitive non-iterable types are catched +for i in True: # [not-an-iterable] + pass -def test(*args): +for i in None: # [not-an-iterable] pass +for i in 8.5: # [not-an-iterable] + pass -test(*A()) # [not-an-iterable] -test(*B()) -for i in A(): # [not-an-iterable] +for i in 10: # [not-an-iterable] pass |