diff options
author | Claudiu Popa <pcmanticore@gmail.com> | 2015-11-26 17:18:16 +0200 |
---|---|---|
committer | Claudiu Popa <pcmanticore@gmail.com> | 2015-11-26 17:18:16 +0200 |
commit | 0c7dc2248c66da8d8884985ec5d96f2d243b1849 (patch) | |
tree | c147f8d0cc1690bfd777fd4044d946173bbc07a2 /pylint/test/functional | |
parent | e3aa096710619f7b4647b9656649f90b29fe6eba (diff) | |
download | pylint-0c7dc2248c66da8d8884985ec5d96f2d243b1849.tar.gz |
Added a new warning, 'unsupported-delete-operation'
It is emitted when item deletion is tried on an object which doesn't
have this ability. Closes issue #592.
Diffstat (limited to 'pylint/test/functional')
-rw-r--r-- | pylint/test/functional/unsupported_delete_operation.py | 94 | ||||
-rw-r--r-- | pylint/test/functional/unsupported_delete_operation.txt | 17 |
2 files changed, 111 insertions, 0 deletions
diff --git a/pylint/test/functional/unsupported_delete_operation.py b/pylint/test/functional/unsupported_delete_operation.py new file mode 100644 index 0000000..297cc15 --- /dev/null +++ b/pylint/test/functional/unsupported_delete_operation.py @@ -0,0 +1,94 @@ +""" +Checks that value used in a subscript support deletion +(i.e. defines __delitem__ method). +""" +# pylint: disable=missing-docstring,pointless-statement,expression-not-assigned,wrong-import-position +# pylint: disable=too-few-public-methods,import-error,invalid-name,wrong-import-order +import six + +# primitives +numbers = [1, 2, 3] +del numbers[0] + + +del bytearray(b"123")[0] +del dict(a=1, b=2)['a'] +del (1, 2, 3)[0] # [unsupported-delete-operation] + +# list/dict comprehensions are fine +del [x for x in range(10)][0] +del {x: 10 - x for x in range(10)}[0] + + +# instances +class NonSubscriptable(object): + pass + +class Subscriptable(object): + def __delitem__(self, key): + pass + +del NonSubscriptable()[0] # [unsupported-delete-operation] +del NonSubscriptable[0] # [unsupported-delete-operation] +del Subscriptable()[0] +del Subscriptable[0] # [unsupported-delete-operation] + +# generators are not subscriptable +def powers_of_two(): + k = 0 + while k < 10: + yield 2 ** k + k += 1 + +del powers_of_two()[0] # [unsupported-delete-operation] +del powers_of_two[0] # [unsupported-delete-operation] + + +# check that primitive non subscriptable types are catched +del True[0] # [unsupported-delete-operation] +del None[0] # [unsupported-delete-operation] +del 8.5[0] # [unsupported-delete-operation] +del 10[0] # [unsupported-delete-operation] + +# sets are not subscriptable +del {x ** 2 for x in range(10)}[0] # [unsupported-delete-operation] +del set(numbers)[0] # [unsupported-delete-operation] +del frozenset(numbers)[0] # [unsupported-delete-operation] + +# skip instances with unknown base classes +from some_missing_module import LibSubscriptable + +class MaybeSubscriptable(LibSubscriptable): + pass + +del MaybeSubscriptable()[0] + +# subscriptable classes (through metaclasses) + +class MetaSubscriptable(type): + def __delitem__(cls, key): + pass + +class SubscriptableClass(six.with_metaclass(MetaSubscriptable, object)): + pass + +del SubscriptableClass[0] +del SubscriptableClass()[0] # [unsupported-delete-operation] + +# functions are not subscriptable +def test(*args, **kwargs): + return args, kwargs + +del test()[0] # [unsupported-delete-operation] +del test[0] # [unsupported-delete-operation] + +# deque +from collections import deque +deq = deque(maxlen=10) +deq.append(42) +del deq[0] + +# tuples assignment +values = [1, 2, 3, 4] +del (values[0], values[1]) +del (values[0], SubscriptableClass()[0]) # [unsupported-delete-operation] diff --git a/pylint/test/functional/unsupported_delete_operation.txt b/pylint/test/functional/unsupported_delete_operation.txt new file mode 100644 index 0000000..2eead9b --- /dev/null +++ b/pylint/test/functional/unsupported_delete_operation.txt @@ -0,0 +1,17 @@ +unsupported-delete-operation:16::'(1, 2, 3)' does not support item deletion +unsupported-delete-operation:31::'NonSubscriptable()' does not support item deletion +unsupported-delete-operation:32::'NonSubscriptable' does not support item deletion +unsupported-delete-operation:34::'Subscriptable' does not support item deletion +unsupported-delete-operation:43::'powers_of_two()' does not support item deletion +unsupported-delete-operation:44::'powers_of_two' does not support item deletion +unsupported-delete-operation:48::'True' does not support item deletion +unsupported-delete-operation:49::'None' does not support item deletion +unsupported-delete-operation:50::'8.5' does not support item deletion +unsupported-delete-operation:51::'10' does not support item deletion +unsupported-delete-operation:54::'{(x) ** (2) for x in range(10)}' does not support item deletion +unsupported-delete-operation:55::'set(numbers)' does not support item deletion +unsupported-delete-operation:56::'frozenset(numbers)' does not support item deletion +unsupported-delete-operation:76::'SubscriptableClass()' does not support item deletion +unsupported-delete-operation:82::'test()' does not support item deletion +unsupported-delete-operation:83::'test' does not support item deletion +unsupported-delete-operation:94::'SubscriptableClass()' does not support item deletion |