#19628: lazy_import breaks CachedRepresentation
----------------------------+-------------------------
Reporter: cheuberg | Owner:
Type: defect | Status: new
Priority: major | Milestone: sage-6.10
Component: coercion | Resolution:
Keywords: | Merged in:
Authors: | Reviewers:
Report Upstream: N/A | Work issues:
Branch: | Commit:
Dependencies: | Stopgaps:
----------------------------+-------------------------
Comment (by nbruin):
Replying to [comment:23 jdemeyer]:
> Now the question becomes: to what extent does equality matter? One of my
proposals is to fix lazy imports in cached functions. That would seriously
reduce the extent to which equality matters. It would certainly fix this:
> {{{
> sage: from sage.misc.lazy_import import LazyImport
> sage: lazyZZ = LazyImport('sage.rings.integer_ring', 'ZZ')
> sage: PolynomialRing(lazyZZ, 'x') is PolynomialRing(ZZ, 'x')
> False
> }}}
> What do you think about this?
I know that Robert Bradshaw did not endorse the use of `LazyImport` for
imports like this. The intended usage scenario was really just to replace
"import" with a few extensions to support "from .. import .." for
functions/classes/submodules. He was fully aware that the shim that
`LazyImport` objects provide is imperfect. In his opinion, you shouldn't
import `ZZ` this way. You can lazy-import sage.rings.integer_ring as
integer_ring and then address ZZ as integer_ring.ZZ.
You can of course try to extend the use of lazy_import and see if you can
get it to work for things like NN as well. However, you'll find (as some
examples have shown) that it's always fundamentally problematic. I think
we're better off rooting out the "bad use" than trying to (imperfectly)
support it. It would be nice to support lazy importing like this fully,
but I think it would require hacking python to an uncomfortable degree.
The problem isn't huge: I think `NN` is the worst symbol like this.
Perhaps we can just not lazily import it? or otherwise make it accessible
via an accessor function or via a lazily imported namespace (rather than
importing NN lazily itself).
For a pretty complete tally: (so we're talking about 291 objects)
{{{
sage: def lazy_names(S):
....: return [k for k,v in S.iteritems() if type(v) is LazyImport]
sage: L=[lazy_names(m.__dict__) for m in sys.modules.values() if m is not
None]
sage: len(Counter(flatten(L)))
291
}}}
There are a lot of interesting bindings there, though:
{{{
sage: type(sage.misc.misc.SAGE_ROOT)
<type 'sage.misc.lazy_import.LazyImport'>
}}}
To see the types of these lazy import objects:
{{{
from sage.misc.lazy_import import LazyImport,att
from collections import Counter
def lazy_objects(S):
return [v for k,v in S.iteritems() if type(v) is LazyImport]
L=[lazy_objects(m.__dict__) for m in sys.modules.values() if m is not
None]
O=[]
for l in L: O.extend(l)
%cpaste
for o in O: #try to load all objects
try:
_=repr(o)
except:
pass
--
Counter([type(att(o)["_object"]) for o in O])
}}}
Which gives:
{{{
sage: list(Counter([type(att(o)["_object"]) for o in O]))
[<class
'sage.rings.semirings.non_negative_integer_semiring.NonNegativeIntegerSemiring_with_category'>,
<type 'sage.misc.inherit_comparison.InheritComparisonMetaclass'>,
<type 'sage.misc.lazy_import.LazyImport'>,
<class
'sage.combinat.cluster_algebra_quiver.quiver_mutation_type.QuiverMutationTypeFactory'>,
<type 'module'>,
<class 'sage.modular.arithgroup.congroup_sl2z.SL2Z_class_with_category'>,
<class 'sage.categories.cartesian_product.CartesianProductFunctor'>,
<type 'dict'>,
<class
'sage.combinat.finite_state_machine_generators.AutomatonGenerators'>,
<class
'sage.geometry.hyperplane_arrangement.library.HyperplaneArrangementLibrary'>,
<type 'instance'>,
<class
'sage.combinat.finite_state_machine_generators.TransducerGenerators'>,
<class 'sage.libs.gap.libgap.Gap'>,
<type 'bool'>,
<type 'str'>,
<class 'sage.dev.sagedev_wrapper.SageDevWrapper'>,
<type 'NoneType'>,
<type 'sage.misc.classcall_metaclass.ClasscallMetaclass'>,
<class 'sage.interfaces.genus2reduction.Genus2reduction'>,
<type 'function'>,
<class 'sage.interfaces.maxima_lib.MaximaLib'>,
<type 'builtin_function_or_method'>,
<type 'list'>,
<type 'type'>,
<class 'sage.databases.findstat.FindStat'>,
<class 'sage.rings.invariant_theory.InvariantTheoryFactory'>,
<class
'sage.misc.inherit_comparison.InheritComparisonClasscallMetaclass'>,
<class 'abc.ABCMeta'>]
}}}
Most of these objects are straightforward callables, that are unlikely to
be used as objects to be worked with. The remaining objects could be
investigated if they need to by lazily imported by themselves or whether
some surrounding scope can be lazily imported instead.
--
Ticket URL: <http://trac.sagemath.org/ticket/19628#comment:24>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sage-trac.
For more options, visit https://groups.google.com/d/optout.