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

Commit

Permalink
Trac #28000: Get rid of most __nonzero__ references and/or replace th…
Browse files Browse the repository at this point in the history
…em with __bool__
  • Loading branch information
embray committed Jan 6, 2020
1 parent 0ff5ac8 commit 0e37a95
Show file tree
Hide file tree
Showing 110 changed files with 89 additions and 205 deletions.
2 changes: 1 addition & 1 deletion src/sage/algebras/commutative_dga.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,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: 0 additions & 4 deletions src/sage/algebras/jordan_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,6 @@ def __bool__(self):
"""
return bool(self._x)

__nonzero__ = __bool__

def __eq__(self, other):
"""
Check equality.
Expand Down Expand Up @@ -820,8 +818,6 @@ def __bool__(self):
"""
return bool(self._s) or bool(self._v)

__nonzero__ = __bool__

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

def __nonzero__(self):
def __bool__(self):
"""
TESTS::
Expand Down
2 changes: 0 additions & 2 deletions src/sage/algebras/lie_algebras/classical_lie_algebra.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,8 +1175,6 @@ def __bool__(self):
"""
return bool(self._real) or bool(self._imag)

__nonzero__ = __bool__

def __hash__(self):
r"""
Return the hash of ``self``.
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 @@ -301,7 +301,7 @@ cdef class LieAlgebraElementWrapper(ElementWrapper):
from sage.typeset.unicode_art import unicode_art
return unicode_art(self.value)

def __nonzero__(self):
def __bool__(self):
"""
Return if ``self`` is non-zero.
Expand Down Expand Up @@ -1126,7 +1126,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 @@ -345,7 +345,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 @@ -948,7 +948,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
5 changes: 1 addition & 4 deletions src/sage/categories/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -1767,10 +1767,7 @@ def required_methods(self):
EXAMPLES::
sage: Algebras(QQ).required_methods() # py2
{'element': {'optional': ['_add_', '_mul_'], 'required': ['__nonzero__']},
'parent': {'optional': ['algebra_generators'], 'required': ['__contains__']}}
sage: Algebras(QQ).required_methods() # py3
sage: Algebras(QQ).required_methods()
{'element': {'optional': ['_add_', '_mul_'], 'required': ['__bool__']},
'parent': {'optional': ['algebra_generators'], 'required': ['__contains__']}}
"""
Expand Down
2 changes: 0 additions & 2 deletions src/sage/categories/crystals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2196,8 +2196,6 @@ 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,5 @@ def __bool__(self):
"""
return any(x for x in self.value.values())

__nonzero__ = __bool__

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

__nonzero__ = __bool__

def _add_(self, rhs):
"""
Add ``self`` and ``rhs``.
Expand Down
4 changes: 1 addition & 3 deletions src/sage/categories/homset.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,6 @@ def __bool__(self):
"""
return True

__nonzero__ = __bool__

def homset_category(self):
"""
Return the category that this is a Hom in, i.e., this is typically
Expand Down Expand Up @@ -1086,7 +1084,7 @@ def __ne__(self, other):
True
"""
return not (self == other)

def __contains__(self, x):
"""
Test whether the parent of the argument is ``self``.
Expand Down
7 changes: 2 additions & 5 deletions src/sage/categories/morphism.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,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 All @@ -397,10 +397,7 @@ cdef class Morphism(Map):
try:
return self._is_nonzero()
except Exception:
if PY_MAJOR_VERSION < 3:
return super(Morphism, self).__nonzero__()
else:
return super().__bool__()
return super().__bool__()


cdef class FormalCoercionMorphism(Morphism):
Expand Down
2 changes: 1 addition & 1 deletion src/sage/categories/rings.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,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 Down
2 changes: 0 additions & 2 deletions src/sage/combinat/combinat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1291,8 +1291,6 @@ def __bool__(self):
"""
return bool(self._list)

__nonzero__ = __bool__

def __len__(self):
"""
EXAMPLES::
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 @@ -4162,4 +4162,3 @@ def is_isomorphism(self):
is_embedding = is_isomorphism
is_strict = is_isomorphism
__bool__ = is_isomorphism
__nonzero__ = is_isomorphism
8 changes: 0 additions & 8 deletions src/sage/combinat/finite_state_machine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2035,10 +2035,6 @@ def __bool__(self):
"""
return True # A state cannot be zero (see __init__)


__nonzero__ = __bool__


def _epsilon_successors_(self, fsm=None):
"""
Returns the dictionary with states reachable from ``self``
Expand Down Expand Up @@ -2540,8 +2536,6 @@ def __bool__(self):
"""
return True # A transition cannot be zero (see __init__)

__nonzero__ = __bool__


#*****************************************************************************

Expand Down Expand Up @@ -4005,8 +3999,6 @@ def __bool__(self):
"""
return bool(self._states_)

__nonzero__ = __bool__

def __eq__(left, right):
"""
Returns ``True`` if the two finite state machines are equal,
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 @@ -240,11 +240,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: 0 additions & 2 deletions src/sage/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,6 @@ def __bool__(self):
"""
return bool(self.is_present)

__nonzero__ = __bool__

def __repr__(self):
r"""
TESTS::
Expand Down
2 changes: 0 additions & 2 deletions src/sage/groups/abelian_gps/abelian_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,6 @@ 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: 0 additions & 2 deletions src/sage/interfaces/fricas.py
Original file line number Diff line number Diff line change
Expand Up @@ -1009,8 +1009,6 @@ def __bool__(self):
P = self._check_valid()
return not P.new("zero?(%s)" % self._name).sage()

__nonzero__ = __bool__

def __long__(self):
"""
TESTS::
Expand Down
2 changes: 0 additions & 2 deletions src/sage/interfaces/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,8 +1321,6 @@ def __bool__(self):
P._false_symbol())
return P.eval(cmd) != P._true_symbol()

__nonzero__ = __bool__

def __long__(self):
"""
EXAMPLES::
Expand Down
2 changes: 0 additions & 2 deletions src/sage/interfaces/macaulay2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,8 +1157,6 @@ def __bool__(self):
P = self.parent()
return P.eval('{0}===false or {0}==0'.format(self._name)) != 'true'

__nonzero__ = __bool__

def sage_polystring(self):
"""
If this Macaulay2 element is a polynomial, return a string
Expand Down
2 changes: 0 additions & 2 deletions src/sage/interfaces/magma.py
Original file line number Diff line number Diff line change
Expand Up @@ -2624,8 +2624,6 @@ def __bool__(self):
pass
return True

__nonzero__ = __bool__

def sub(self, gens):
"""
Return the sub-object of self with given gens.
Expand Down
2 changes: 0 additions & 2 deletions src/sage/interfaces/octave.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,6 @@ 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"""
Return Sage matrix from this octave element.
Expand Down
2 changes: 0 additions & 2 deletions src/sage/interfaces/r.py
Original file line number Diff line number Diff line change
Expand Up @@ -1615,8 +1615,6 @@ def __bool__(self):
"""
return "FALSE" in repr(self==0)

__nonzero__ = __bool__

def _comparison(self, other, symbol):
"""
Used to implement comparison of two objects.
Expand Down
2 changes: 0 additions & 2 deletions src/sage/interfaces/singular.py
Original file line number Diff line number Diff line change
Expand Up @@ -1443,8 +1443,6 @@ def __bool__(self):
P = self.parent()
return P.eval('%s == 0' % self.name()) == '0'

__nonzero__ = __bool__

def sage_polystring(self):
r"""
If this Singular element is a polynomial, return a string
Expand Down
6 changes: 3 additions & 3 deletions src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,7 @@ cdef class GapElement_Boolean(GapElement):
return False
raise ValueError('the GAP boolean value "fail" cannot be represented in Sage')

def __nonzero__(self):
def __bool__(self):
"""
Check that the boolean is "true".

Expand All @@ -2275,12 +2275,12 @@ cdef class GapElement_Boolean(GapElement):

sage: gap_bool = [libgap.eval('true'), libgap.eval('false'), libgap.eval('fail')]
sage: for x in gap_bool:
....: if x: # this calls __nonzero__
....: if x: # this calls __bool__
....: print("{} {}".format(x, type(x)))
true <type 'sage.libs.gap.element.GapElement_Boolean'>

sage: for x in gap_bool:
....: if not x: # this calls __nonzero__
....: if not x: # this calls __bool__
....: print("{} {}".format( x, type(x)))
false <type 'sage.libs.gap.element.GapElement_Boolean'>
fail <type 'sage.libs.gap.element.GapElement_Boolean'>
Expand Down
6 changes: 3 additions & 3 deletions src/sage/libs/mpmath/ext_main.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1927,7 +1927,7 @@ cdef class mpf(mpf_base):

_mpf_ = property(_get_mpf, _set_mpf, doc=_get_mpf.__doc__)

def __nonzero__(self):
def __bool__(self):
"""
Returns whether the number is nonzero ::

Expand Down Expand Up @@ -2273,7 +2273,7 @@ cdef class constant(mpf_base):
return str(self)
return "<%s: %s~>" % (self.name, global_context.nstr(self))

def __nonzero__(self):
def __bool__(self):
"""
Returns whether the constant is nonzero ::

Expand Down Expand Up @@ -2457,7 +2457,7 @@ cdef class mpc(mpnumber):
"""
return "(%s)" % libmp.mpc_to_str(s._mpc_, global_context._str_digits)

def __nonzero__(self):
def __bool__(self):
"""
TESTS ::

Expand Down
2 changes: 0 additions & 2 deletions src/sage/manifolds/chart_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,6 @@ def __bool__(self):
val = self.expr(curr).is_zero
return not val

__nonzero__ = __bool__ # For Python2 compatibility

def is_trivial_zero(self):
r"""
Check if ``self`` is trivially equal to zero without any
Expand Down
2 changes: 0 additions & 2 deletions src/sage/manifolds/differentiable/mixed_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,6 @@ def __bool__(self):
self._is_zero = True
return False

__nonzero__ = __bool__ # For Python2 compatibility

def _richcmp_(self, other, op):
r"""
Comparison method for sage objects.
Expand Down
Loading

0 comments on commit 0e37a95

Please sign in to comment.