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

Commit

Permalink
fine tuning : full removal of __nonzero__
Browse files Browse the repository at this point in the history
  • Loading branch information
fchapoton committed Mar 25, 2022
1 parent b16bc4a commit cf98130
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 26 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
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
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
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')



def _sage_(self, locals={}, *args):
"""
Convert this object to Sage.
Expand Down
4 changes: 2 additions & 2 deletions src/sage/libs/gap/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2284,12 +2284,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 <class '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 <class 'sage.libs.gap.element.GapElement_Boolean'>
fail <class 'sage.libs.gap.element.GapElement_Boolean'>
Expand Down
2 changes: 1 addition & 1 deletion src/sage/misc/cachefunc.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ cdef frozenset special_method_names = frozenset(['__abs__', '__add__',
'__index__', '__init__', '__instancecheck__', '__int__', '__invert__', '__ior__', '__ipow__',
'__irshift__', '__isub__', '__iter__', '__itruediv__', '__ixor__', '__le__', '__len__',
'__length_hint__', '__long__', '__lshift__', '__lt__', '__missing__', '__mod__', '__mul__',
'__ne__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__',
'__ne__', '__neg__', '__new__', '__oct__', '__or__', '__pos__', '__pow__',
'__radd__', '__rand__', '__rdiv__', '__repr__', '__reversed__', '__rfloordiv__', '__rlshift__',
'__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__',
'__rtruediv__', '__rxor__', '__set__', '__setattr__', '__setitem__', '__setslice__', '__sizeof__',
Expand Down
6 changes: 3 additions & 3 deletions src/sage/numerical/linear_functions.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ cdef class LinearFunctionOrConstraint(ModuleElement):
# x <= y <= z into:
#
# temp = x <= y # calls x.__richcmp__(y)
# if temp: # calls temp.__nonzero__()
# if temp: # calls temp.__bool__()
# return y <= z # calls y.__richcmp__(z)
# else:
# return temp
Expand All @@ -397,14 +397,14 @@ cdef class LinearFunctionOrConstraint(ModuleElement):
# non-Sage type):
#
# temp = y >= x # calls y.__richcmp__(x)
# if temp: # calls temp.__nonzero__()
# if temp: # calls temp.__bool__()
# return y <= z # calls y.__richcmp__(z)
# else:
# return temp
#
# but we would like x <= y <= z as output. The trick to make it
# work is to store x and y in the first call to __richcmp__
# and temp in the call to __nonzero__. Then we can replace x
# and temp in the call to __bool__. Then we can replace x
# or y by x <= y in the second call to __richcmp__.
if chain_replace is not None:
if chain_replace.equality != equality:
Expand Down
8 changes: 3 additions & 5 deletions src/sage/rings/polynomial/polynomial_element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2617,17 +2617,15 @@ cdef class Polynomial(CommutativeAlgebraElement):
TESTS:
We verify that :trac:`23020` has been resolved. (There are no elements
in the Sage library yet that do not implement ``__nonzero__``
and ``__bool__``, so we have to create one artificially.)::
We verify that :trac:`23020` has been resolved. (There are no
elements in the Sage library yet that do not implement
``__bool__``, so we have to create one artificially.)::
sage: class PatchedAlgebraicNumber(sage.rings.qqbar.AlgebraicNumber):
....: def __bool__(self): raise NotImplementedError()
....: def __bool__(self): raise NotImplementedError()
sage: R.<x> = QQbar[]
sage: R([PatchedAlgebraicNumber(0), 1])
x + 0
"""
if name is None:
name = self._parent._names[0]
Expand Down
2 changes: 1 addition & 1 deletion src/sage/rings/rational.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ AUTHORS:
- David Harvey (2006-09-15): added nth_root
- Pablo De Napoli (2007-04-01): corrected the implementations of
multiplicative_order, is_one; optimized __nonzero__ ; documented:
multiplicative_order, is_one; optimized __bool__ ; documented:
lcm,gcd
- John Cremona (2009-05-15): added support for local and global
Expand Down
4 changes: 2 additions & 2 deletions src/sage/structure/element.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1054,12 +1054,12 @@ cdef class Element(SageObject):
Return ``True`` if ``self`` equals ``self.parent()(0)``.
The default implementation is to fall back to ``not
self.__nonzero__``.
self.__bool__``.
.. WARNING::
Do not re-implement this method in your subclass but
implement ``__nonzero__`` instead.
implement ``__bool__`` instead.
"""
return not self

Expand Down

0 comments on commit cf98130

Please sign in to comment.