python.lang.correctness.baseclass-attribute-override.baseclass-attribute-override

profile photo of semgrepsemgrep
Author
7,892
Download Count*

Class $C inherits from both $A and $B which both have a method named $F; one of these methods will be overwritten.

Run Locally

Run in CI

Defintion

rules:
  - id: baseclass-attribute-override
    message: Class $C inherits from both `$A` and `$B` which both have a method
      named `$F`; one of these methods will be overwritten.
    languages:
      - python
    severity: WARNING
    patterns:
      - pattern-inside: |
          class $A(...):
            ...
            def $F(...):
              ...
            ...
          ...
      - pattern-inside: |
          class $B(...):
            ...
            def $F(...):
              ...
            ...
          ...
      - pattern: |
          class $C(..., $A, ..., $B, ...):
            ...
      - focus-metavariable: $C
    metadata:
      category: correctness
      references:
        - https://docs.python.org/3/tutorial/classes.html#multiple-inheritance
      technology:
        - python
      license: Commons Clause License Condition v1.0[LGPL-2.1-only]

Examples

baseclass-attribute-override.py

class A:
    def method1(self, args):
        pass


class A2:
    def method2(self, args):
        pass


class B:
    def method1(self, args):
        print('hello there')


# ruleid: baseclass-attribute-override
class C(A, B):
    def __init__():
        print("initialized")


# ok: baseclass-attribute-override
class C(A2, B):
    def __init__():
        print("initialized")