Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various small improvements to imports #39276

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/sage/categories/category_singleton.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Singleton categories
# https://www.gnu.org/licenses/
# *****************************************************************************

from sage.misc.constant_function import ConstantFunction
from sage.misc.lazy_attribute import lazy_class_attribute
from sage.categories.category import Category
from sage.structure.category_object cimport CategoryObject
Expand Down Expand Up @@ -318,10 +317,12 @@ class Category_singleton(Category):
...
AssertionError: <class '__main__.MySubStuff'> is not a direct subclass of <class 'sage.categories.category_singleton.Category_singleton'>
"""
from sage.misc.constant_function import ConstantFunction
from sage.categories.category_with_axiom import CategoryWithAxiom_singleton

if isinstance(cls, DynamicMetaclass): # cls is something like Rings_with_category
cls = cls.__base__
# TODO: find a better way to check that cls is an abstract class
from sage.categories.category_with_axiom import CategoryWithAxiom_singleton
assert (cls.__mro__[1] is Category_singleton or cls.__mro__[1] is CategoryWithAxiom_singleton), \
"{} is not a direct subclass of {}".format(cls, Category_singleton)
obj = super().__classcall__(cls, *args)
Expand Down
4 changes: 2 additions & 2 deletions src/sage/categories/morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ AUTHORS:

from cpython.object cimport *

from sage.misc.constant_function import ConstantFunction

from sage.structure.element cimport Element, ModuleElement
from sage.structure.richcmp cimport richcmp_not_equal, rich_to_bool
from sage.structure.parent cimport Parent
Expand Down Expand Up @@ -92,6 +90,8 @@ cdef class Morphism(Map):
sage: phi
Defunct morphism
"""
from sage.misc.constant_function import ConstantFunction

D = self.domain()
if D is None:
return "Defunct morphism"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,13 @@
# *****************************************************************************

from sage.algebras.finite_gca import FiniteGCAlgebra
from sage.combinat.free_module import IndexedFreeModuleElement
from sage.manifolds.differentiable.affine_connection import AffineConnection
from sage.manifolds.differentiable.bundle_connection import BundleConnection
from sage.manifolds.differentiable.levi_civita_connection import LeviCivitaConnection
from sage.misc.abstract_method import abstract_method
from sage.misc.cachefunc import cached_method
from sage.misc.fast_methods import Singleton
from sage.modules.with_basis.indexed_element import IndexedFreeModuleElement
from sage.rings.polynomial.polynomial_element import Polynomial
from sage.rings.polynomial.polynomial_ring_constructor import PolynomialRing
from sage.structure.sage_object import SageObject
Expand Down
3 changes: 3 additions & 0 deletions src/sage/misc/lazy_attribute.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# This type-stub file helps pyright understand the decorator @lazy_attribute.

from collections.abc import Callable
from typing import Any

# Adapted from https://github.com/python/typeshed/blob/b9640005eb586afdbe0a57bac2b88a7a12465069/stdlib/builtins.pyi#L1237-L1254
class lazy_attribute:
def __init__(
Expand Down
24 changes: 14 additions & 10 deletions src/sage/modules/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,11 @@
from sage.misc.lazy_attribute import lazy_attribute
from sage.misc.lazy_import import LazyImport
from sage.misc.randstate import current_randstate
from sage.modules import free_module_element
from sage.modules.free_module_element import (
FreeModuleElement,
FreeModuleElement_generic_dense,
FreeModuleElement_generic_sparse,
)
from sage.modules.module import Module
from sage.rings.finite_rings.finite_field_base import FiniteField
from sage.structure.factory import UniqueFactory
Expand All @@ -213,13 +217,13 @@
)
from sage.structure.sequence import Sequence


###############################################################################
#
# Constructor functions
#
###############################################################################


class FreeModuleFactory(UniqueFactory):
r"""
Factory class for the finite-dimensional free modules with standard basis
Expand Down Expand Up @@ -776,7 +780,7 @@ def span(gens, base_ring=None, check=True, already_echelonized=False):
return FreeModule(R, 0)
else:
x = gens[0]
if isinstance(x, free_module_element.FreeModuleElement):
if isinstance(x, FreeModuleElement):
M = x.parent()
else:
try:
Expand Down Expand Up @@ -950,7 +954,7 @@ def _element_constructor_(self, x, coerce=True, copy=True, check=True):
"""
if isinstance(x, (int, sage.rings.integer.Integer)) and x == 0:
return self.zero_vector()
elif isinstance(x, free_module_element.FreeModuleElement):
elif isinstance(x, FreeModuleElement):
if x.parent() is self:
if copy:
return x.__copy__()
Expand Down Expand Up @@ -2205,7 +2209,7 @@ def _element_constructor_(self, x, coerce=True, copy=True, check=True):
"""
if (isinstance(x, (int, sage.rings.integer.Integer)) and x == 0):
return self.zero_vector()
if isinstance(x, free_module_element.FreeModuleElement):
if isinstance(x, FreeModuleElement):
if x.parent() is self:
if copy:
return x.__copy__()
Expand Down Expand Up @@ -7022,7 +7026,7 @@ def echelon_coordinates(self, v, check=True):
sage: W.echelon_coordinates([0,0,2,0,-1/2])
[0, 2]
"""
if not isinstance(v, free_module_element.FreeModuleElement):
if not isinstance(v, FreeModuleElement):
v = self.ambient_vector_space()(v)
elif v.degree() != self.degree():
raise ArithmeticError("vector is not in free module")
Expand Down Expand Up @@ -8051,7 +8055,7 @@ def echelon_coordinates(self, v, check=True):
sage: vector(QQ, W.echelon_coordinates(v)) * W.basis_matrix()
(1, 5, 9)
"""
if not isinstance(v, free_module_element.FreeModuleElement):
if not isinstance(v, FreeModuleElement):
v = self.ambient_vector_space()(v)
if v.degree() != self.degree():
raise ArithmeticError("v (=%s) is not in self" % v)
Expand Down Expand Up @@ -8195,7 +8199,7 @@ def element_class(R, is_sparse):
else:
if R.order() < MAX_MODULUS:
return Vector_modn_dense
return free_module_element.FreeModuleElement_generic_dense
return FreeModuleElement_generic_dense
elif isinstance(R, sage.rings.abc.RealDoubleField) and not is_sparse:
try:
from sage.modules.vector_real_double_dense import Vector_real_double_dense
Expand Down Expand Up @@ -8224,9 +8228,9 @@ def element_class(R, is_sparse):
return sage.modules.vector_symbolic_sparse.Vector_symbolic_sparse

if is_sparse:
return free_module_element.FreeModuleElement_generic_sparse
return FreeModuleElement_generic_sparse
else:
return free_module_element.FreeModuleElement_generic_dense
return FreeModuleElement_generic_dense


@richcmp_method
Expand Down
1 change: 0 additions & 1 deletion src/sage/rings/integer_ring.pxd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from sage.rings.ring cimport CommutativeRing
from sage.rings.integer cimport Integer
from sage.libs.gmp.types cimport mpz_t

cdef class IntegerRing_class(CommutativeRing):
Expand Down
42 changes: 20 additions & 22 deletions src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ from sage.structure.richcmp cimport rich_to_bool
from sage.misc.misc_c import prod
from sage.misc.randstate cimport randstate, current_randstate, SAGE_RAND_MAX

cimport sage.rings.integer as integer

from sage.rings import ring
from sage.rings.integer cimport Integer

arith = None
cdef void late_import() noexcept:
Expand Down Expand Up @@ -329,7 +327,7 @@ cdef class IntegerRing_class(CommutativeRing):
self._populate_coercion_lists_(init_no_parent=True,
convert_method_name='_integer_')

_element_constructor_ = integer.Integer
_element_constructor_ = Integer

def __reduce__(self):
"""
Expand Down Expand Up @@ -479,20 +477,20 @@ cdef class IntegerRing_class(CommutativeRing):
if step is None:
step = 1
if type(step) is not int:
if not isinstance(step, integer.Integer):
step = integer.Integer(step)
if not isinstance(step, Integer):
step = Integer(step)
if mpz_fits_slong_p((<Integer>step).value):
step = int(step)
if not isinstance(start, integer.Integer):
start = integer.Integer(start)
if not isinstance(end, integer.Integer):
end = integer.Integer(end)
cdef integer.Integer a = <Integer>start
cdef integer.Integer b = <Integer>end
if not isinstance(start, Integer):
start = Integer(start)
if not isinstance(end, Integer):
end = Integer(end)
cdef Integer a = <Integer>start
cdef Integer b = <Integer>end

cdef int step_sign
cdef long istep
cdef integer.Integer zstep, last
cdef Integer zstep, last

L = []
if type(step) is int:
Expand Down Expand Up @@ -797,8 +795,8 @@ cdef class IntegerRing_class(CommutativeRing):
sage: ZZ.random_element() # indirect doctest # random
6
"""
cdef integer.Integer r
cdef integer.Integer n_max, n_min, n_width
cdef Integer r
cdef Integer n_max, n_min, n_width
cdef randstate rstate = current_randstate()
cdef int den = rstate.c_random()-SAGE_RAND_MAX/2
if den == 0: den = 1
Expand All @@ -809,11 +807,11 @@ cdef class IntegerRing_class(CommutativeRing):
if x is None:
mpz_set_si(value, rstate.c_random()%5 - 2)
else:
n_max = x if isinstance(x, integer.Integer) else self(x)
n_max = x if isinstance(x, Integer) else self(x)
mpz_urandomm(value, rstate.gmp_state, n_max.value)
else:
n_min = x if isinstance(x, integer.Integer) else self(x)
n_max = y if isinstance(y, integer.Integer) else self(y)
n_min = x if isinstance(x, Integer) else self(x)
n_max = y if isinstance(y, Integer) else self(y)
n_width = n_max - n_min
if mpz_sgn(n_width.value) <= 0:
n_min = self(-2)
Expand Down Expand Up @@ -954,7 +952,7 @@ cdef class IntegerRing_class(CommutativeRing):
...
TypeError: I must be an ideal of ZZ
"""
if isinstance(I, sage.rings.integer.Integer):
if isinstance(I, Integer):
n = I
elif isinstance(I, sage.rings.ideal.Ideal_generic):
if not (I.ring() is self):
Expand Down Expand Up @@ -1023,7 +1021,7 @@ cdef class IntegerRing_class(CommutativeRing):
...
TypeError: 96 is not prime
"""
if isinstance(prime, sage.rings.integer.Integer):
if isinstance(prime, Integer):
p = self.ideal(prime)
elif isinstance(prime, sage.rings.ideal.Ideal_generic):
if not (prime.ring() is self):
Expand Down Expand Up @@ -1211,9 +1209,9 @@ cdef class IntegerRing_class(CommutativeRing):
ValueError: n must be positive in zeta()
"""
if n == 1:
return sage.rings.integer.Integer(1)
return Integer(1)
elif n == 2:
return sage.rings.integer.Integer(-1)
return Integer(-1)
elif n < 1:
raise ValueError("n must be positive in zeta()")
else:
Expand Down
11 changes: 8 additions & 3 deletions src/sage/rings/rational.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ from cysignals.signals cimport sig_on, sig_off
import operator
import fractions

import sage.rings.rational_field
from sage.rings.rational_field import Q

from sage.arith.long cimport integer_check_long_py
from sage.categories.morphism cimport Morphism
from sage.categories.map cimport Map
from sage.cpython.string cimport char_to_str, str_to_bytes
from sage.libs.gmp.pylong cimport mpz_set_pylong
from sage.rings.integer cimport Integer, smallInteger
from sage.rings.integer_ring import ZZ
from sage.structure.coerce cimport coercion_model, is_numpy_type
from sage.structure.element cimport Element
from sage.structure.parent cimport Parent
Expand Down Expand Up @@ -182,7 +181,7 @@ cdef Rational_sub_(Rational self, Rational other):

return x

cdef Parent the_rational_ring = sage.rings.rational_field.Q
cdef Parent the_rational_ring = Q
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly related but maybe we can change to use QQ instead

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and may be use from sage.rings.rational_field import Q as the_rational_ring ?


# make sure zero/one elements are set
cdef set_zero_one_elements():
Expand Down Expand Up @@ -2105,10 +2104,14 @@ cdef class Rational(sage.structure.element.FieldElement):

num, exact = self.numerator().nth_root(n, 1)
if not exact:
from sage.rings.integer_ring import ZZ

raise ValueError("not a perfect %s power" % ZZ(n).ordinal_str())

den, exact = self.denominator().nth_root(n, 1)
if not exact:
from sage.rings.integer_ring import ZZ

raise ValueError("not a perfect %s power" % ZZ(n).ordinal_str())

if negative:
Expand Down Expand Up @@ -3195,6 +3198,8 @@ cdef class Rational(sage.structure.element.FieldElement):
sage: (-1/2).log(3) # needs sage.symbolic
(I*pi + log(1/2))/log(3)
"""
from sage.rings.integer_ring import ZZ

cdef int self_sgn
if self.denom().is_one():
return ZZ(self.numer()).log(m, prec)
Expand Down
5 changes: 2 additions & 3 deletions src/sage/structure/sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@
# ****************************************************************************

from sage.misc.persist import register_unpickle_override
import sage.structure.sage_object
import sage.structure.coerce
from sage.structure.sage_object import SageObject


def Sequence(x, universe=None, check=True, immutable=False, cr=False, cr_str=None, use_sage_types=False):
Expand Down Expand Up @@ -306,7 +305,7 @@ def Sequence(x, universe=None, check=True, immutable=False, cr=False, cr_str=Non
return Sequence_generic(x, universe, check, immutable, cr, cr_str, use_sage_types)


class Sequence_generic(sage.structure.sage_object.SageObject, list):
class Sequence_generic(SageObject, list):
"""
A mutable list of elements with a common guaranteed universe,
which can be set immutable.
Expand Down
Loading