Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
LazyImport.__instancecheck__, __subclasscheck__: Return False on Impo…
Browse files Browse the repository at this point in the history
…rtError
  • Loading branch information
Matthias Koeppe committed Dec 13, 2021
1 parent d6d0edc commit 447df69
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/sage/misc/lazy_import.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -923,8 +923,17 @@ cdef class LazyImport(object):
sage: lazy_import('sage.rings.rational_field', 'RationalField')
sage: isinstance(QQ, RationalField)
True
No object is an instance of a class that cannot be imported::
sage: lazy_import('sage.xxxxx_does_not_exist', 'DoesNotExist')
sage: isinstance(QQ, DoesNotExist)
False
"""
return isinstance(x, self.get_object())
try:
return isinstance(x, self.get_object())
except ImportError:
return False

def __subclasscheck__(self, x):
"""
Expand All @@ -935,8 +944,17 @@ cdef class LazyImport(object):
sage: lazy_import('sage.structure.parent', 'Parent')
sage: issubclass(RationalField, Parent)
True
No class is a subclass of a class that cannot be imported::
sage: lazy_import('sage.xxxxx_does_not_exist', 'DoesNotExist')
sage: issubclass(RationalField, DoesNotExist)
False
"""
return issubclass(x, self.get_object())
try:
return issubclass(x, self.get_object())
except ImportError:
return False


def lazy_import(module, names, as_=None, *,
Expand Down

0 comments on commit 447df69

Please sign in to comment.