Skip to content

Update pep-0718.rst#1

Open
PabloRuizCuevas wants to merge 5 commits into
Gobot1234:pep-718-even-more-updatesfrom
PabloRuizCuevas:patch-3
Open

Update pep-0718.rst#1
PabloRuizCuevas wants to merge 5 commits into
Gobot1234:pep-718-even-more-updatesfrom
PabloRuizCuevas:patch-3

Conversation

@PabloRuizCuevas
Copy link
Copy Markdown

  • Extended motivation:
  1. highlighting the importance of consistency with current syntax examples
  2. Putting the Generators as one of the main beneficiaries of this feature
  3. Extended overload explanation

Still is a bit WIP, but you can start reviewing if you wish

PabloRuizCuevas and others added 2 commits November 17, 2025 19:07
Added more motivation and other small changes
@PabloRuizCuevas
Copy link
Copy Markdown
Author

@Gobot1234 maybe is better that you give me merge rights

@Gobot1234
Copy link
Copy Markdown
Owner

Sorry I somehow missed this PR being opened. Will review in a few

Copy link
Copy Markdown
Owner

@Gobot1234 Gobot1234 left a comment

Choose a reason for hiding this comment

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

Thanks for this :)

Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated



At runtime ``list[int]`` returns a ``GenericAlias`` that is later evaluated to an
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
At runtime ``list[int]`` returns a ``GenericAlias`` that is later evaluated to an
At runtime ``list[int]`` returns a ``GenericAlias`` that can be later called, returning an

Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
This PEP opens the door to overloading based on type variables:

.. code-block:: python
@overload
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
@overload
@overload

needs this to render properly

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

todo, don´t see it in my editor

Comment thread peps/pep-0718.rst Outdated
For overloading resolution a new step will be required previous to any other, where the resolver
will match only the overloads where the subscription may succeed. For instance
if we have an overload make[*Ts], one with make[T], and one with just make, then a call to make[int]
will only consider the first and second, and a call to make[int, str] will only consider the first.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This should go after the definition I think. Also add back ticks here

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

todo

Comment thread peps/pep-0718.rst
Copy link
Copy Markdown
Author

@PabloRuizCuevas PabloRuizCuevas left a comment

Choose a reason for hiding this comment

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

Fixes

Comment thread peps/pep-0718.rst
Generators would benefit inmensely from this feature as they can't be properly typed in many scenarios,
specially when they make use of the method ``.send``:

For example in a function that accumulates different types:
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

better?

Comment thread peps/pep-0718.rst Outdated
Comment on lines +105 to +107
from typing import Generator, cast, Protocol, TypeVar

T = TypeVar("T")
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i think is required for SupportAdd, mind that is not a container type so SupportsAdd[T] is not what we want.
or at least my linter like this version better

Comment thread peps/pep-0718.rst
Currently you have to use an assignment to provide a precise type:
def make_accumulator[T: SupportsAdd]() -> Generator[T, T]:
""" adds all elemenst using the addition operator """
acc = yield cast(T, None)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

None? is not really yielded in any usefull maner is for priming the generator only , that's why the cast is there

Comment thread peps/pep-0718.rst Outdated
functions where their signature is narrowed to different paths in different
parts of the code.

Another use case would be bounding direclty at the ``.send``
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

but may be generic after this pep ?

Comment thread peps/pep-0718.rst Outdated
functions where their signature is narrowed to different paths in different
parts of the code.

Another use case would be bounding direclty at the ``.send``
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

maybe doesn´t make much sense now that i'm thinking about it...

Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
This PEP opens the door to overloading based on type variables:

.. code-block:: python
@overload
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

todo, don´t see it in my editor

Comment thread peps/pep-0718.rst Outdated
For overloading resolution a new step will be required previous to any other, where the resolver
will match only the overloads where the subscription may succeed. For instance
if we have an overload make[*Ts], one with make[T], and one with just make, then a call to make[int]
will only consider the first and second, and a call to make[int, str] will only consider the first.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

todo

Comment thread peps/pep-0718.rst
@Gobot1234
Copy link
Copy Markdown
Owner

Gobot1234 commented Dec 1, 2025

Thanks, will have a look at the rest in the morning (utc)

Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst Outdated
Comment thread peps/pep-0718.rst
Currently you have to use an assignment to provide a precise type:
def make_accumulator[T: SupportsAdd]() -> Generator[T, T]:
""" adds all elemenst using the addition operator """
acc = yield cast(T, None)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I don't think this is a good example if you have to use cast, there's no harm in specifying it's Generator[T | None, T]

Comment thread peps/pep-0718.rst
Comment on lines +134 to +135
def log_and_yield[T: SupportsAdd](log: Callable) -> Generator[T, T]:
value = yield cast(T, None)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Suggested change
def log_and_yield[T: SupportsAdd](log: Callable) -> Generator[T, T]:
value = yield cast(T, None)
def log_and_yield[T: SupportsAdd](log: Callable[[T], Any]) -> Generator[T | None, T]:
value = yield None

@Gobot1234
Copy link
Copy Markdown
Owner

I've made some updates to the code. I do have some reservations about the generator section as it seems kind of contrived, do you have any full examples where this would be useful. Looking at the code it seems like you could just wait to create the generators after you have a first value to feed into it to avoid the cast

@PabloRuizCuevas
Copy link
Copy Markdown
Author

PabloRuizCuevas commented Apr 21, 2026

sorry, I was a bit disconnected this months, i saw there is recently some updates on the pep, let me know if you need help, probably this pr can be closed as you already included the changes, and btw many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants