Skip to content

functools.cached_property and property swallows AttributeError raised during property computation when the class has __getattr__, causing silent failures. #139578

@PcdPanda

Description

@PcdPanda

Bug report

Bug description:

Minimal Reproduction

from functools import cached_property

class Example:
  @cached_property
  def data(self):
      return None.some_attribute  # Raises AttributeError

  def __getattr__(self, name):
      return "FALLBACK"

obj = Example()
print(obj.data)  # Expected: AttributeError, Got: "FALLBACK"

Expected Behavior

AttributeError should be raised: 'NoneType' object has no attribute 'some_attribute'

Actual Behavior

Returns "FALLBACK" (from getattr)

Impact

  • hasattr(obj, 'data') returns True (wrong)
  • getattr(obj, 'data', 'DEFAULT') returns "FALLBACK" instead of 'DEFAULT'
  • Silent failures instead of fast failures
  • Difficult to debug

Root Cause

Python's descriptor protocol treats AttributeError from get as "attribute doesn't exist", triggering getattr fallback.

Python Versions

  • Tested: 3.13

Workaround

Don't use @cached_property with classes that have getattr, or manually wrap exceptions.

Proposal

Add a ignore_error parameters to cached_property and property

CPython versions tested on:

CPython main branch, 3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions