Skip to content

Commit

Permalink
Trac #33558: get rid of __nonzero__ and use __bool__ instead
Browse files Browse the repository at this point in the history
as this is the official python3 choice

the first 2 commits were scripted

other commits are hand-made, finishing the task

URL: https://trac.sagemath.org/33558
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Mar 31, 2022
2 parents fbc8bd0 + b24ef83 commit 1762a6c
Show file tree
Hide file tree
Showing 114 changed files with 143 additions and 153 deletions.
2 changes: 1 addition & 1 deletion src/doc/en/thematic_tutorials/coercion_and_categories.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1503,7 +1503,7 @@ The elements have to provide more::
Hence, the elements must provide ``denominator()`` and ``numerator()``
methods, and must be able to tell whether they are zero or not. The base class
:class:`~sage.structure.element.Element` provides a default ``__nonzero__()``
:class:`~sage.structure.element.Element` provides a default ``__bool__()``
method. In addition, the elements may provide Sage's single underscore
arithmetic methods (actually any ring element *should* provide them).

Expand Down
2 changes: 1 addition & 1 deletion src/sage/algebras/commutative_dga.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def _is_nonzero(self):
"""
Return ``True`` iff this morphism is nonzero.
This is used by the :meth:`Morphism.__nonzero__` method, which
This is used by the :meth:`Morphism.__bool__` method, which
in turn is used by the :func:`TestSuite` test
``_test_nonzero_equal``.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/algebras/jordan_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ def __bool__(self):
"""
return bool(self._x)

__nonzero__ = __bool__


def __eq__(self, other):
"""
Expand Down Expand Up @@ -834,7 +834,7 @@ def __bool__(self):
"""
return bool(self._s) or bool(self._v)

__nonzero__ = __bool__


def __eq__(self, other):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ cdef class FreeAlgebraElement_letterplace(AlgebraElement):
"""
return self._poly.lc()

def __nonzero__(self):
def __bool__(self):
"""
TESTS::
Expand Down
2 changes: 1 addition & 1 deletion src/sage/algebras/lie_algebras/classical_lie_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1346,7 +1346,7 @@ def __bool__(self):
"""
return bool(self._real) or bool(self._imag)

__nonzero__ = __bool__


def __hash__(self):
r"""
Expand Down
4 changes: 2 additions & 2 deletions src/sage/algebras/lie_algebras/lie_algebra_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ cdef class LieAlgebraElementWrapper(ElementWrapper):
├┼┘
└┘
"""
def __nonzero__(self):
def __bool__(self):
"""
Return if ``self`` is non-zero.
Expand Down Expand Up @@ -1151,7 +1151,7 @@ cdef class UntwistedAffineLieAlgebraElement(Element):
self._c_coeff, self._d_coeff))
return self._hash

def __nonzero__(self):
def __bool__(self):
"""
Return ``self`` as a boolean.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/algebras/quatalg/quaternion_algebra_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ cdef class QuaternionAlgebraElement_abstract(AlgebraElement):
return Rational(self[0])
raise TypeError

def __nonzero__(self):
def __bool__(self):
"""
Return ``True`` if this quaternion is nonzero.
Expand Down Expand Up @@ -946,7 +946,7 @@ cdef class QuaternionAlgebraElement_rational_field(QuaternionAlgebraElement_abst
"""
return not (mpz_sgn(self.y) or mpz_sgn(self.z) or mpz_sgn(self.w))

def __nonzero__(self):
def __bool__(self):
"""
Return True if this quaternion is nonzero.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/crystals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@ def __bool__(self):
"""
return any(self._on_gens(mg) is not None for mg in self._gens)

__nonzero__ = __bool__


# TODO: Does this belong in the element_class of the Crystals() category?
def to_module_generator(self, x):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ def __bool__(self):
"""
return any(x for x in self.value.values())

__nonzero__ = __bool__


Example = FreeCommutativeAdditiveMonoid
2 changes: 1 addition & 1 deletion src/sage/categories/examples/lie_algebras.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def __bool__(self):
"""
return bool(self.value)

__nonzero__ = __bool__


def _add_(self, rhs):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/homset.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ def __bool__(self):
"""
return True

__nonzero__ = __bool__


def homset_category(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ cdef class Morphism(Map):
domain = <Parent?>base
return rich_to_bool(op, 0)

def __nonzero__(self):
def __bool__(self):
r"""
Return whether this morphism is not a zero morphism.
Expand Down
3 changes: 1 addition & 2 deletions src/sage/categories/rings.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _is_nonzero(self):
.. NOTE::
We can not override ``is_zero()`` from the category framework
and we can not implement ``__nonzero__`` because it is a
and we can not implement ``__bool__`` because it is a
special method. That this is why this has a cumbersome name.
EXAMPLES::
Expand All @@ -202,7 +202,6 @@ def _is_nonzero(self):
True
sage: ZZ.hom(Zmod(1))._is_nonzero()
False
"""
return bool(self.codomain().one())

Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/combinat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,7 @@ def __bool__(self) -> bool:
"""
return bool(self._list)

__nonzero__ = __bool__


def __len__(self) -> Integer:
"""
Expand Down
1 change: 0 additions & 1 deletion src/sage/combinat/crystals/kirillov_reshetikhin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4158,4 +4158,3 @@ def is_isomorphism(self):
is_embedding = is_isomorphism
is_strict = is_isomorphism
__bool__ = is_isomorphism
__nonzero__ = is_isomorphism
6 changes: 3 additions & 3 deletions src/sage/combinat/finite_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2012,7 +2012,7 @@ def __bool__(self):
"""
return True # A state cannot be zero (see __init__)

__nonzero__ = __bool__


def _epsilon_successors_(self, fsm=None):
"""
Expand Down Expand Up @@ -2491,7 +2491,7 @@ def __bool__(self):
"""
return True # A transition cannot be zero (see __init__)

__nonzero__ = __bool__



# ****************************************************************************
Expand Down Expand Up @@ -3932,7 +3932,7 @@ def __bool__(self):
"""
return bool(self._states_)

__nonzero__ = __bool__


def __eq__(self, other):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/recognizable_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,7 @@ def __bool__(self):
return False
return True

__nonzero__ = __bool__


def transposed(self):
r"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ cdef class RiggedPartition(SageObject):
self._hash = hash(tuple(self._list))
return self._hash

def __nonzero__(self):
def __bool__(self):
"""
TESTS::
Expand Down
2 changes: 1 addition & 1 deletion src/sage/combinat/words/word_char.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ cdef class WordDatatype_char(WordDatatype):
if self._is_slice == 0:
sig_free(self._data)

def __nonzero__(self):
def __bool__(self):
r"""
Test whether the word is not empty.
Expand Down
6 changes: 1 addition & 5 deletions src/sage/cpython/debugimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,7 @@ static void _type_debug(PyTypeObject* tp)
subattr_pointer_meth(tp_as_number, nb_negative, "__neg__");
subattr_pointer_meth(tp_as_number, nb_positive, "__pos__");
subattr_pointer_meth(tp_as_number, nb_absolute, "__abs__");
#if PY_MAJOR_VERSION <= 2
subattr_pointer_meth(tp_as_number, nb_nonzero, "__nonzero__");
#else
subattr_pointer_meth(tp_as_number, nb_bool, "__bool__");
#endif
subattr_pointer_meth(tp_as_number, nb_bool, "__bool__");
subattr_pointer_meth(tp_as_number, nb_invert, "__invert__");
subattr_pointer_meth(tp_as_number, nb_lshift, "__lshift__");
subattr_pointer_meth(tp_as_number, nb_rshift, "__rshift__");
Expand Down
2 changes: 1 addition & 1 deletion src/sage/data_structures/bounded_integer_sequences.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ cdef class BoundedIntegerSequence:
"""
return self.data.length

def __nonzero__(self):
def __bool__(self):
"""
A bounded integer sequence is nonzero if and only if its length is nonzero.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ def __bool__(self):
"""
return bool(self.is_present)

__nonzero__ = __bool__


def __repr__(self):
r"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/groups/abelian_gps/abelian_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def __bool__(self):
"""
return not self.is_trivial()

__nonzero__ = __bool__


@cached_method
def dual_group(self, names="X", base_ring=None):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/fricas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1145,7 +1145,7 @@ def __bool__(self):
P = self._check_valid()
return not P.new("zero?(%s)" % self._name).sage()

__nonzero__ = __bool__


def __float__(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gap.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,7 @@ def __bool__(self):
P = self._check_valid()
return self != P(0) and repr(self) != 'false'

__nonzero__ = __bool__


def __len__(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/gp.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ def __bool__(self):
P = self._check_valid()
return P.eval('%s != 0'%(self.name())) == '1'

__nonzero__ = __bool__


def _complex_mpfr_field_(self, CC):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1337,7 +1337,7 @@ def __bool__(self):
P._false_symbol())
return P.eval(cmd) != P._true_symbol()

__nonzero__ = __bool__


def __float__(self):
"""
Expand Down
4 changes: 1 addition & 3 deletions src/sage/interfaces/kash.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ def __bool__(self):
# Kash has separate integer and boolean types, and FALSE does not
# compare equal to 0 (i.e, FALSE = 0 is FALSE)

# Python 2.x uses __nonzero__ for type conversion to 'bool', so we
# Python 3.x uses __bool__ for type conversion to 'bool', so we
# have to test against FALSE, and sage.structure.element.Element's
# default implementation of is_zero() is to return 'not self', so
# our boolean conversion also has to test against 0.
Expand All @@ -747,8 +747,6 @@ def __bool__(self):
return (P.eval('%s = FALSE' % self.name()) == 'FALSE' and
P.eval('%s = 0' % self.name()) == 'FALSE')

__nonzero__ = __bool__

def _sage_(self, locals={}, *args):
"""
Convert this object to Sage.
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/lisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def __bool__(self):
"""
return self != 0 and repr(self) != 'NIL'

__nonzero__ = __bool__


def _add_(self, right):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/macaulay2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@ def __bool__(self):
P = self.parent()
return P.eval('{0}===false or {0}==0'.format(self._name)) != 'true'

__nonzero__ = __bool__


def sage_polystring(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/magma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,7 +2651,7 @@ def __bool__(self):
pass
return True

__nonzero__ = __bool__


def sub(self, gens):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/mathematica.py
Original file line number Diff line number Diff line change
Expand Up @@ -1037,7 +1037,7 @@ def __bool__(self):
cmd = '%s===%s' % (self._name, P._false_symbol())
return P.eval(cmd).strip() != P._true_symbol()

__nonzero__ = __bool__


def n(self, *args, **kwargs):
r"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/mathics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,7 @@ def __bool__(self):
cmd = '%s===%s' % (self._name, P._false_symbol())
return not str(P(cmd)) == P._true_symbol()

__nonzero__ = __bool__


def n(self, *args, **kwargs):
r"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/maxima_abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ def __bool__(self):
# but be careful, since for relations things like is(equal(a,b)) are
# what Maxima needs

__nonzero__ = __bool__


def _richcmp_(self, other, op):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/octave.py
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ def __bool__(self):
"""
return str(self) != ' [](0x0)' and any(x != '0' for x in str(self).split())

__nonzero__ = __bool__


def _matrix_(self, R=None):
r"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/polymake.py
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ def __bool__(self):
cmd = '{} {} {};'.format(self._name, P._equality_symbol(), t)
return P.get(cmd) == t

__nonzero__ = __bool__


def known_properties(self):
"""
Expand Down
2 changes: 1 addition & 1 deletion src/sage/interfaces/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def __bool__(self):
"""
return "FALSE" in repr(self==0)

__nonzero__ = __bool__


def _comparison(self, other, symbol):
"""
Expand Down
Loading

0 comments on commit 1762a6c

Please sign in to comment.