GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • CodeQL documentation
  • CodeQL overview
  • Writing CodeQL queries
  • CodeQL language guides
  • QL language reference
  • CodeQL standard-libraries
  • CodeQL query help
  • CodeQL repository
  • QL publications
  • CodeQL query help for C and C++
  • CodeQL query help for C#
  • CodeQL query help for GitHub Actions
  • CodeQL query help for Go
  • CodeQL query help for Java and Kotlin

This view

  • Original page
  • New address

GeezerVison is showing codeql.github.com — Superclass attribute shadows subclass method — CodeQL query help documentation

Superclass attribute shadows subclass method¶

ID: py/attribute-shadows-method
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

When an object has an attribute that shares its name with a method on the object’s class (or another class attribute), the instance attribute is prioritized during attribute lookup, shadowing the method. If a method on a subclass is shadowed by an attribute on a superclass in this way, this may lead to unexpected results or errors, as this shadowing behavior is nonlocal and may be unintended.

Recommendation¶

Ensure method names on subclasses don’t conflict with attribute names on superclasses, and rename one. If the shadowing behavior is intended, ensure this is explicit in the superclass.

Example¶

In the following example, the _foo attribute of class A shadows the method _foo of class B. Calls to B()._foo() will result in a TypeError, as 3 will be called instead.

class A:
    def __init__(self):
        self._foo = 3

class B(A):
    # BAD: _foo is shadowed by attribute A._foo
    def _foo(self):
        return 2

In the following example, the behavior of the default attribute being shadowed to allow for customization during initialization is intended in within the superclass A. Overriding default in the subclass B is then OK.

class A:
    def __init__(self, default_func=None):
        if default_func is not None:
            self.default = default_func 

    # GOOD: The shadowing behavior is explicitly intended in the superclass.
    def default(self):
        return []
    
class B(A):
    
    # Subclasses may override the method `default`, which will still be shadowed by the attribute `default` if it is set.
    # As this is part of the expected behavior of the superclass, this is fine. 
    def default(self):
        return {}
  • © GitHub, Inc.
  • Terms
  • Privacy
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.