Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
gh-118647: Add defaults to typing.Generator and typing.AsyncGenerator
  • Loading branch information
JelleZijlstra committed May 6, 2024
commit 9ddde9d69b54f7d7de101fcc935c3501623cc2ac
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`` default to :const:`!None`::
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated

def infinite_stream(start: int) -> Generator[int]:
while True:
yield start
start += 1
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated

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
19 changes: 15 additions & 4 deletions Lib/typing.py
Original file line number Diff line number Diff line change
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=(type(None), type(None)))
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
AsyncGenerator = _alias(collections.abc.AsyncGenerator, 2, defaults=(type(None),))
Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
Type = _alias(type, 1, inst=False, name='Type')
Type.__doc__ = \
"""Deprecated alias to builtins.type.
Expand Down