Skip to content

improve LazyImport #31650

@videlec

Description

@videlec

The LazyImport objects suffer several problems

  1. copy and deepcopy are broken
sage: from sage.misc.lazy_import import LazyImport
sage: sage.all.foo = [1, 2]
sage: lazy_foo = LazyImport('sage.all', 'foo')
sage: a = copy(lazy_foo)
sage: a
[1, 2]
sage: a[0] = 18
sage: lazy_foo
[18, 2]
  1. Most doctests use lazy_import rather than LazyImport. The former modifies the namespace so that the name actually points to the object afterwards. Compare
sage: lazy_import('sage.rings.all', 'ZZ', 'lazy_ZZ')
sage: type(lazy_ZZ)
<class 'sage.misc.lazy_import.LazyImport'>
sage: _ = lazy_ZZ(3)
sage: type(lazy_ZZ)
<class 'sage.rings.integer_ring.IntegerRing_class'>
with
sage: from sage.misc.lazy_import import LazyImport
sage: lazy_ZZ = LazyImport('sage.rings.all', 'ZZ')
sage: _ = lazy_ZZ(3)
sage: type(lazy_ZZ)
<class 'sage.misc.lazy_import.LazyImport'>
In the former version, any code that uses more than once the lazy object actually uses it only once.
  1. It is not checked that lazy imported module resolves correctly... and it is not the case
sage: LinearCodeFromVectorSpace._get_object()
Traceback (most recent call last):
...
AttributeError: module 'sage.coding.linear_code' has no attribute 'LinearCodeFromVectorSpace'

Follow-up: #31656

Depends on #31648

CC: @kliem

Component: misc

Author: Vincent Delecroix

Branch/Commit: 99ad007

Reviewer: Travis Scrimshaw

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions