CodeQL documentation

__init__ method is a generator

ID: py/init-method-is-generator
Kind: problem
Security severity: 
Severity: error
Precision: very-high
   - reliability
   - correctness
Query suites:
   - python-security-and-quality.qls

Click to see the query in the CodeQL repository

The __init__ method of a class is used to initialize new objects, not create them. As such, it should not return any value. By including a yield expression in the method turns it into a generator method. On calling it will return a generator resulting in a runtime error.


The presence of a yield expression in an __init__ method suggests a logical error, so it is not possible to suggest a general fix.


In this example the __init__ method contains a yield expression. This is not logical in the context of an initializer.

class InitIsGenerator(object):
    def __init__(self, i):
        yield i


  • © GitHub, Inc.
  • Terms
  • Privacy