Skip to content

gh-137597: Clarify flattening and bugfix for itertools.tee #137599

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Aniketsy
Copy link

@Aniketsy Aniketsy commented Aug 9, 2025

(GH#137597)

This PR updates the documentation for itertools.tee to clarify its flattening behavior when called on existing tee objects. It adds a "Changed in version 3.13" note describing the bugfix for re-teeing the first iterator, and includes a doctest example to illustrate that repeated tee calls do not cause deep nesting.

Please let me know if this fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thankyou!


📚 Documentation preview 📚: https://cpython-previews--137599.org.readthedocs.build/

@Aniketsy Aniketsy requested a review from rhettinger as a code owner August 9, 2025 22:36
@bedevere-app bedevere-app bot added awaiting review docs Documentation in the Doc dir skip news labels Aug 9, 2025
@github-project-automation github-project-automation bot moved this to Todo in Docs PRs Aug 9, 2025
@Aniketsy Aniketsy changed the title Doc: Clarify flattening and bugfix for itertools.tee in 3.13 Doc: Clarify flattening and bugfix for itertools.tee in 3.13 gh-137597 Aug 9, 2025

:func:`tee` automatically "flattens" existing tee objects,
sharing the same underlying buffer instead of nesting them, to avoid
performance degradation. This flattening behavior has existed since Python 3.7.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you say since 3.7? It existed in Python 2 already.

Copy link
Author

Choose a reason for hiding this comment

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

Apologies for the oversight. Would it be fine if I remove this sentence and just describe the behavior without referencing a specific version history?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think so. It's described in the current documentation already.


.. versionchanged:: 3.13
Fixed a bug where re-teeing the first iterator did not correctly flatten
the iterator chain in all cases. Previously, this could lead to unnecessary
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see how that bug led to unnecessary nesting and performance degradation. I think you misunderstood it.

Copy link
Author

Choose a reason for hiding this comment

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

.. versionchanged:: 3.13
Fixed a bug where re-teeing a tee iterator did not always return independent iterators.
Previously, the first iterator in the result could be the same object as the
input, causing inconsistent behavior.

Is this description accurate, or have I misunderstood the nature of the bug?
Please let me know if I’m mistaken.

Thank you!

Copy link
Contributor

Choose a reason for hiding this comment

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

That description is accurate and you seem to have misunderstood it. Before jumping to a PR, I suggest to first wait for Trey to clarify what he means, and possibly for Raymond to comment on it. I suspect Trey is talking about that bug(fix) and Raymond didn't forget to add a note but intentionally didn't add one.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for pointing that out! I’ll pause work on this and wait for clarification .

>>> list(c)
[1, 2, 3]
>>> list(d)
[1, 2, 3]
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the purpose of this? It has nothing to do with the bug and its fix, and seems to just redundantly show what the existing peeking example shows already.

@StanFromIreland StanFromIreland changed the title Doc: Clarify flattening and bugfix for itertools.tee in 3.13 gh-137597 gh-137597: Clarify flattening and bugfix for itertools.tee Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

2 participants