Skip to content

gh-118647: Add defaults to typing.Generator and typing.AsyncGenerator #118648

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

Merged
merged 4 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions Doc/library/typing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3648,8 +3648,14 @@ Aliases to asynchronous ABCs in :mod:`collections.abc`
is no ``ReturnType`` type parameter. As with :class:`Generator`, the
``SendType`` behaves contravariantly.

If your generator will only yield values, set the ``SendType`` to
``None``::
The ``SendType`` defaults to :const:`!None`::

async def infinite_stream(start: int) -> AsyncGenerator[int]:
while True:
yield start
start = await increment(start)

It is also possible to set this type explicitly::

async def infinite_stream(start: int) -> AsyncGenerator[int, None]:
while True:
Expand All @@ -3671,6 +3677,9 @@ Aliases to asynchronous ABCs in :mod:`collections.abc`
now supports subscripting (``[]``).
See :pep:`585` and :ref:`types-genericalias`.

.. versionchanged:: 3.13
The ``SendType`` parameter now has a default.

.. class:: AsyncIterable(Generic[T_co])

Deprecated alias to :class:`collections.abc.AsyncIterable`.
Expand Down Expand Up @@ -3754,8 +3763,14 @@ Aliases to other ABCs in :mod:`collections.abc`
of :class:`Generator` behaves contravariantly, not covariantly or
invariantly.

If your generator will only yield values, set the ``SendType`` and
``ReturnType`` to ``None``::
The ``SendType`` and ``ReturnType`` parameters default to :const:`!None`::

def infinite_stream(start: int) -> Generator[int]:
while True:
yield start
start += 1

It is also possible to set these types explicitly::

def infinite_stream(start: int) -> Generator[int, None, None]:
while True:
Expand All @@ -3774,6 +3789,9 @@ Aliases to other ABCs in :mod:`collections.abc`
:class:`collections.abc.Generator` now supports subscripting (``[]``).
See :pep:`585` and :ref:`types-genericalias`.

.. versionchanged:: 3.13
Default values for the send and return types were added.

.. class:: Hashable

Deprecated alias to :class:`collections.abc.Hashable`.
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -7289,6 +7289,17 @@ def foo():
g = foo()
self.assertIsSubclass(type(g), typing.Generator)

def test_generator_default(self):
g1 = typing.Generator[int]
g2 = typing.Generator[int, None, None]
self.assertEqual(get_args(g1), (int, type(None), type(None)))
self.assertEqual(get_args(g1), get_args(g2))

g3 = typing.Generator[int, float]
g4 = typing.Generator[int, float, None]
self.assertEqual(get_args(g3), (int, float, type(None)))
self.assertEqual(get_args(g3), get_args(g4))

def test_no_generator_instantiation(self):
with self.assertRaises(TypeError):
typing.Generator()
Expand Down
21 changes: 16 additions & 5 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,7 +1328,7 @@ def __getattr__(self, attr):
raise AttributeError(attr)

def __setattr__(self, attr, val):
if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams'}:
if _is_dunder(attr) or attr in {'_name', '_inst', '_nparams', '_defaults'}:
super().__setattr__(attr, val)
else:
setattr(self.__origin__, attr, val)
Expand Down Expand Up @@ -1578,11 +1578,12 @@ def __iter__(self):
# parameters are accepted (needs custom __getitem__).

class _SpecialGenericAlias(_NotIterable, _BaseGenericAlias, _root=True):
def __init__(self, origin, nparams, *, inst=True, name=None):
def __init__(self, origin, nparams, *, inst=True, name=None, defaults=()):
if name is None:
name = origin.__name__
super().__init__(origin, inst=inst, name=name)
self._nparams = nparams
self._defaults = defaults
if origin.__module__ == 'builtins':
self.__doc__ = f'A generic version of {origin.__qualname__}.'
else:
Expand All @@ -1594,12 +1595,22 @@ def __getitem__(self, params):
params = (params,)
msg = "Parameters to generic types must be types."
params = tuple(_type_check(p, msg) for p in params)
if (self._defaults
and len(params) < self._nparams
and len(params) + len(self._defaults) >= self._nparams
):
params = (*params, *self._defaults[len(params) - self._nparams:])
actual_len = len(params)

if actual_len != self._nparams:
if self._defaults:
expected = f"at least {self._nparams - len(self._defaults)}"
else:
expected = str(self._nparams)
if not self._nparams:
raise TypeError(f"{self} is not a generic class")
raise TypeError(f"Too {'many' if actual_len > self._nparams else 'few'} arguments for {self};"
f" actual {actual_len}, expected {self._nparams}")
f" actual {actual_len}, expected {expected}")
return self.copy_with(params)

def copy_with(self, params):
Expand Down Expand Up @@ -2813,8 +2824,8 @@ class Other(Leaf): # Error reported by type checker
OrderedDict = _alias(collections.OrderedDict, 2)
Counter = _alias(collections.Counter, 1)
ChainMap = _alias(collections.ChainMap, 2)
Generator = _alias(collections.abc.Generator, 3)
AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2)
Generator = _alias(collections.abc.Generator, 3, defaults=(types.NoneType, types.NoneType))
AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2, defaults=(types.NoneType,))
Type = _alias(type, 1, inst=False, name='Type')
Type.__doc__ = \
"""Deprecated alias to builtins.type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add type parameter defaults to :class:`typing.Generator` and
:class:`typing.AsyncGenerator`.
Loading