Skip to content

Commit 5580f31

Browse files
gh-115808: Add is_none and is_not_none to operator (#115814)
Co-authored-by: Kirill Podoprigora <kirill.bast9@mail.ru>
1 parent 0fd97e4 commit 5580f31

File tree

7 files changed

+107
-3
lines changed

7 files changed

+107
-3
lines changed

Doc/library/operator.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ truth tests, identity tests, and boolean operations:
8080
Return ``a is not b``. Tests object identity.
8181

8282

83+
.. function:: is_none(a)
84+
85+
Return ``a is None``. Tests object identity.
86+
87+
.. versionadded:: 3.14
88+
89+
90+
.. function:: is_not_none(a)
91+
92+
Return ``a is not None``. Tests object identity.
93+
94+
.. versionadded:: 3.14
95+
96+
8397
The mathematical and bitwise operations are the most numerous:
8498

8599

@@ -405,6 +419,10 @@ Python syntax and the functions in the :mod:`operator` module.
405419
+-----------------------+-------------------------+---------------------------------------+
406420
| Identity | ``a is not b`` | ``is_not(a, b)`` |
407421
+-----------------------+-------------------------+---------------------------------------+
422+
| Identity | ``a is None`` | ``is_none(a)`` |
423+
+-----------------------+-------------------------+---------------------------------------+
424+
| Identity | ``a is not None`` | ``is_not_none(a)`` |
425+
+-----------------------+-------------------------+---------------------------------------+
408426
| Indexed Assignment | ``obj[k] = v`` | ``setitem(obj, k, v)`` |
409427
+-----------------------+-------------------------+---------------------------------------+
410428
| Indexed Deletion | ``del obj[k]`` | ``delitem(obj, k)`` |

Doc/whatsnew/3.14.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ Add notes for JSON serialization errors that allow to identify the source
124124
of the error.
125125
(Contributed by Serhiy Storchaka in :gh:`122163`.)
126126

127+
operator
128+
--------
129+
130+
* Two new functions ``operator.is_none`` and ``operator.is_not_none``
131+
have been added, such that ``operator.is_none(obj)`` is equivalent
132+
to ``obj is None`` and ``operator.is_not_none(obj)`` is equivalent
133+
to ``obj is not None``.
134+
(Contributed by Raymond Hettinger and Nico Mexis in :gh:`115808`.)
135+
127136
os
128137
--
129138

Lib/operator.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
'delitem', 'eq', 'floordiv', 'ge', 'getitem', 'gt', 'iadd', 'iand',
1515
'iconcat', 'ifloordiv', 'ilshift', 'imatmul', 'imod', 'imul',
1616
'index', 'indexOf', 'inv', 'invert', 'ior', 'ipow', 'irshift',
17-
'is_', 'is_not', 'isub', 'itemgetter', 'itruediv', 'ixor', 'le',
18-
'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',
17+
'is_', 'is_none', 'is_not', 'is_not_none', 'isub', 'itemgetter', 'itruediv',
18+
'ixor', 'le', 'length_hint', 'lshift', 'lt', 'matmul', 'methodcaller', 'mod',
1919
'mul', 'ne', 'neg', 'not_', 'or_', 'pos', 'pow', 'rshift',
2020
'setitem', 'sub', 'truediv', 'truth', 'xor']
2121

@@ -66,6 +66,14 @@ def is_not(a, b):
6666
"Same as a is not b."
6767
return a is not b
6868

69+
def is_none(a):
70+
"Same as a is None."
71+
return a is None
72+
73+
def is_not_none(a):
74+
"Same as a is not None."
75+
return a is not None
76+
6977
# Mathematical/Bitwise Operations *********************************************#
7078

7179
def abs(a):

Lib/test/test_operator.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,26 @@ def test_is_not(self):
347347
self.assertFalse(operator.is_not(a, b))
348348
self.assertTrue(operator.is_not(a,c))
349349

350+
def test_is_none(self):
351+
operator = self.module
352+
a = 'xyzpdq'
353+
b = ''
354+
c = None
355+
self.assertRaises(TypeError, operator.is_none)
356+
self.assertFalse(operator.is_none(a))
357+
self.assertFalse(operator.is_none(b))
358+
self.assertTrue(operator.is_none(c))
359+
360+
def test_is_not_none(self):
361+
operator = self.module
362+
a = 'xyzpdq'
363+
b = ''
364+
c = None
365+
self.assertRaises(TypeError, operator.is_not_none)
366+
self.assertTrue(operator.is_not_none(a))
367+
self.assertTrue(operator.is_not_none(b))
368+
self.assertFalse(operator.is_not_none(c))
369+
350370
def test_attrgetter(self):
351371
operator = self.module
352372
class A:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add :func:`operator.is_none` and :func:`operator.is_not_none` functions.

Modules/_operator.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,34 @@ _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
728728
return Py_NewRef(result);
729729
}
730730

731+
/*[clinic input]
732+
_operator.is_none = _operator.neg
733+
734+
Same as a is None.
735+
[clinic start generated code]*/
736+
737+
static PyObject *
738+
_operator_is_none(PyObject *module, PyObject *a)
739+
/*[clinic end generated code: output=07159cc102261dec input=0448b38af7b8533d]*/
740+
{
741+
PyObject *result = Py_IsNone(a) ? Py_True : Py_False;
742+
return Py_NewRef(result);
743+
}
744+
745+
/*[clinic input]
746+
_operator.is_not_none = _operator.neg
747+
748+
Same as a is not None.
749+
[clinic start generated code]*/
750+
751+
static PyObject *
752+
_operator_is_not_none(PyObject *module, PyObject *a)
753+
/*[clinic end generated code: output=b0168a51451d9140 input=7587f38ebac51688]*/
754+
{
755+
PyObject *result = Py_IsNone(a) ? Py_False : Py_True;
756+
return Py_NewRef(result);
757+
}
758+
731759
/* compare_digest **********************************************************/
732760

733761
/*
@@ -916,6 +944,8 @@ static struct PyMethodDef operator_methods[] = {
916944
_OPERATOR_COUNTOF_METHODDEF
917945
_OPERATOR_IS__METHODDEF
918946
_OPERATOR_IS_NOT_METHODDEF
947+
_OPERATOR_IS_NONE_METHODDEF
948+
_OPERATOR_IS_NOT_NONE_METHODDEF
919949
_OPERATOR_INDEX_METHODDEF
920950
_OPERATOR_ADD_METHODDEF
921951
_OPERATOR_SUB_METHODDEF

Modules/clinic/_operator.c.h

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)