Skip to content

inspect.getmembers_static triggers __getattribute__ #99673

@gschaffner

Description

@gschaffner

Bug report

inspect.getmembers_static is documented as not triggering __getattribute__, but it can trigger it via dir. For example the following hits the recursion limit:

class A:
    def __getattribute__(self, name: str) -> Any:
        getmembers_static(self)
        return "all attrs exist!"

A().anything

(A workaround for examples like this one is to add the base cases required by dir to __getattribute__:

if name in ("__dict__", "__class__"):
    return object.__getattribute__(self, name)

(The __dict__ case is not always needed depending on the __dir__ implementation.))

Switching getmembers_static to use dir_static (gh-55979) would fix this problem, I believe. getmembers_static already documents that it can't find the dynamically created attributes, etc. that getattr_static can't find, so I don't think such a change would be breaking.

Your environment

  • CPython versions tested on: 3.11.0
  • Operating system and architecture: Linux-based, x86_64

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