
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
101 lines
2.3 KiB
Plaintext
101 lines
2.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Operators ``++in++`` and ``++not in++``, also called https://docs.python.org/3/reference/expressions.html#membership-test-operations["membership test operators"], require that the right operand supports membership protocol.
|
|
|
|
|
|
In order to support this membership protocol a user-defined class should implement at least one of the following methods: ``++__contains__++``, ``++__iter__++``, ``++__getitem__++``.
|
|
|
|
|
|
This rule raises an issue when operators ``++in++`` and ``++not in++`` are called with a right operand not supporting membership protocol.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
myint = 42
|
|
|
|
if 42 in myint: # Noncompliant. Integers don't support membership protocol
|
|
pass
|
|
|
|
class A:
|
|
def __init__(self, values):
|
|
self._values = values
|
|
|
|
if "mystring" in A(["mystring"]): # Noncompliant. Class A doesn't support membership protocol
|
|
pass
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
mylist = [42]
|
|
|
|
if 42 in mylist:
|
|
pass
|
|
|
|
class MyContains:
|
|
def __init__(self, values):
|
|
self._values = values
|
|
|
|
def __contains__(self, value):
|
|
return value in self._values
|
|
|
|
if "mystring" in MyContains(["mystring"]): # Noncompliant. Class A don't support membership protocol
|
|
pass
|
|
|
|
# OR
|
|
|
|
class MyIterable:
|
|
def __init__(self, values):
|
|
self._values = values
|
|
|
|
def __iter__(self):
|
|
return iter(self._values)
|
|
|
|
if "mystring" in MyIterable(["mystring"]): # Noncompliant. Class A don't support membership protocol
|
|
pass
|
|
|
|
# OR
|
|
|
|
class MyGetItem:
|
|
def __init__(self, values):
|
|
self._values = values
|
|
|
|
def __getitem__(self, key):
|
|
return self._values[key]
|
|
|
|
if "mystring" in MyGetItem(["mystring"]): # Noncompliant. Class A don't support membership protocol
|
|
pass
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* https://docs.python.org/3/reference/expressions.html#membership-test-operations[Python documentation - Membership test operations]
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Change the type of X; type Y does not support membership protocol.
|
|
|
|
|
|
For an expression like "a in X" where X has type Y
|
|
|
|
|
|
=== Highlighting
|
|
|
|
Primary: The "in" or "not in" operator
|
|
|
|
Secondary: The operator's right operand
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|