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

bpo-41810: Reintroduce types.EllipsisType, .NoneType & .NotImplementedType #22336

Merged
merged 13 commits into from
Sep 22, 2020
Merged
13 changes: 8 additions & 5 deletions Doc/library/constants.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ A small number of constants live in the built-in namespace. They are:

.. data:: None

The sole value of the type ``NoneType``. ``None`` is frequently used to
represent the absence of a value, as when default arguments are not passed to a
function. Assignments to ``None`` are illegal and raise a :exc:`SyntaxError`.
An object frequently used to represent the absence of a value, as when
default arguments are not passed to a function. Assignments to ``None``
are illegal and raise a :exc:`SyntaxError`.
``None`` is the sole instance of the :data:`NoneType` type.


.. data:: NotImplemented

Special value which should be returned by the binary special methods
A special value which should be returned by the binary special methods
(e.g. :meth:`__eq__`, :meth:`__lt__`, :meth:`__add__`, :meth:`__rsub__`,
etc.) to indicate that the operation is not implemented with respect to
the other type; may be returned by the in-place binary special methods
(e.g. :meth:`__imul__`, :meth:`__iand__`, etc.) for the same purpose.
It should not be evaluated in a boolean context.
``NotImplemented`` is the sole instance of the :data:`types.NotImplementedType` type.

.. note::

Expand Down Expand Up @@ -59,8 +61,9 @@ A small number of constants live in the built-in namespace. They are:
.. index:: single: ...; ellipsis literal
.. data:: Ellipsis

The same as the ellipsis literal "``...``". Special value used mostly in conjunction
The same as the ellipsis literal "``...``". Special value used mostly in conjunction
with extended slicing syntax for user-defined container data types.
``Ellipsis`` is the sole instance of the :data:`types.EllipsisType` type.


.. data:: __debug__
Expand Down
21 changes: 21 additions & 0 deletions Doc/library/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ If you instantiate any of these types, note that signatures may vary between Pyt

Standard names are defined for the following types:

.. data:: NoneType

The type of :data:`None`.

.. versionadded:: 3.10


.. data:: FunctionType
LambdaType

Expand Down Expand Up @@ -186,6 +193,13 @@ Standard names are defined for the following types:
.. versionadded:: 3.7


.. data:: NotImplementedType

The type of :data:`NotImplemented`.

.. versionadded:: 3.10


.. data:: MethodDescriptorType

The type of methods of some built-in data types such as :meth:`str.join`.
Expand Down Expand Up @@ -236,6 +250,13 @@ Standard names are defined for the following types:
Defaults to ``None``. Previously the attribute was optional.


.. data:: EllipsisType

The type of :data:`Ellipsis`.

.. versionadded:: 3.10


.. class:: TracebackType(tb_next, tb_frame, tb_lasti, tb_lineno)

The type of traceback objects such as found in ``sys.exc_info()[2]``.
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.10.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ Add :data:`sys.orig_argv` attribute: the list of the original command line
arguments passed to the Python executable.
(Contributed by Victor Stinner in :issue:`23427`.)

types
-----

Reintroduced the :data:`types.EllipsisType`, :data:`types.NoneType`
and :data:`types.NotImplementedType` classes, providing a new set
of types readily interpretable by type checkers.
Copy link
Member

Choose a reason for hiding this comment

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

I think this and the blurb are fine, providing a reason without saying either that this is the only reason or that these should be use everywhere.

(Contributed by Bas van Beek in :issue:`41810`.)

unittest
--------

Expand Down
10 changes: 10 additions & 0 deletions Lib/test/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,16 @@ def test_or_type_repr(self):
assert repr(int | None) == "int | None"
assert repr(int | typing.GenericAlias(list, int)) == "int | list[int]"

def test_ellipsis_type(self):
self.assertIsInstance(Ellipsis, types.EllipsisType)

def test_notimplemented_type(self):
self.assertIsInstance(NotImplemented, types.NotImplementedType)

def test_none_type(self):
self.assertIsInstance(None, types.NoneType)


class MappingProxyTests(unittest.TestCase):
mappingproxy = types.MappingProxyType

Expand Down
3 changes: 3 additions & 0 deletions Lib/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,5 +296,8 @@ def wrapped(*args, **kwargs):
GenericAlias = type(list[int])
Union = type(int | str)

EllipsisType = type(Ellipsis)
NoneType = type(None)
NotImplementedType = type(NotImplemented)

__all__ = [n for n in globals() if n[:1] != '_']
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Robin Becker
Torsten Becker
Bill Bedford
Michał Bednarski
Bas van Beek
Ian Beer
Stefan Behnel
Reimer Behrends
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:data:`types.EllipsisType`, :data:`types.NotImplementedType` and
:data:`types.NoneType` have been reintroduced, providing a new set
of types readily interpretable by static type checkers.