CodeQL documentation

Membership test with a non-container

ID: py/member-test-non-container
Kind: problem
Security severity: 
Severity: error
Precision: high
Tags:
   - quality
   - reliability
   - correctness
Query suites:
   - python-code-quality.qls
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

A membership test, that is a binary expression with in or not in as the operator, expects that the expression to the right of the operator will be a container.

As well as standard containers such as list, tuple, dict or set, a container can be an instance of any class that has the __contains__, __iter__ or __getitem__ method.

Recommendation

Ensure that the right hand side of the expression is a container, or add a guard clause for other cases. For example, if the right side may be a container or None then change