Skip to content

LazyImport.__instancecheck__, __subclasscheck__: Return False on ImportError #33017

@mkoeppe

Description

@mkoeppe

No object is an instance of a class that cannot be imported.

No class is a subclass of a class that cannot be imported.

Hence, we change LazyImport so that the special methods __instancecheck__, __subclasscheck__ catch ImportError and just return False.

(We do not use the stronger versions of these two statements: "No object is an instance of a class that has not been imported yet (i.e., the module name is not in sys.modules)." These are true only if there are no re-exports.)

With this change to LazyImport, we have another instrument for modularization of code that uses isinstance checks. (The use of isinstance with ABCs (#32566) is preferable because it avoids importing an otherwise unused implementation class.)

Depends on #32899

CC: @tscrim @kliem

Component: refactoring

Author: Matthias Koeppe

Branch: 7adef96

Reviewer: Jonathan Kliem

Issue created by migration from https://trac.sagemath.org/ticket/33017

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions