Skip to content

Introspection: use typing_extensions.Buffer instead of collections.abc.Buffer (py312+) - #6395

Merged
Tpt merged 3 commits into
PyO3:mainfrom
jorenham:introspection/typing_extensions.Buffer
Sep 9, 2026
Merged

Introspection: use typing_extensions.Buffer instead of collections.abc.Buffer (py312+)#6395
Tpt merged 3 commits into
PyO3:mainfrom
jorenham:introspection/typing_extensions.Buffer

Conversation

@jorenham

@jorenham jorenham commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

collections.abc.Buffer was added in Python 3.12 (docs), but PyO3 also supports older versions. By using typing_extensions.Buffer instead, this will no longer lead to typing issues. Note that within .pyi stubs typing_extensions is always available, even if not installed, because typing_extensions is bundled as part of the stdlib stubs through typeshed: https://github.com/python/typeshed/blob/main/stdlib/typing_extensions.pyi

@jorenham
jorenham force-pushed the introspection/typing_extensions.Buffer branch from 4eade08 to c8c7012 Compare September 8, 2026 11:48
@jonasdedden

jonasdedden commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

@jorenham just as a datapoint, I'm no PyO3 maintainer: In recently merged PR #6362, there was a bit of back and forth of how to deal with typing features not available to all Python versions.

In that PR, things converged to let it render, depending on Python version, to the variant with maximum portability or compatibility. In this specific instance, a disjoint_base pointer was used internally, but was rendered to typing.disjoint_base vs. typing_extensions.disjoint_base, where the former is only emitted in codebases targeting 3.15+.

Similarly, one could introduce a similar rule here, i.e. emit collections.abc.Buffer for Python 3.12+, and typing_extensions.Buffer for <3.12.

This is because using typing_extensions.Buffer would trigger ruff UP035 (deprecated-import, pyupgrade), so I would consider collections.abc.Buffer to be the more canonical variant of Buffer.

Verified empirically with ruff:

# test_buffer.py
from typing_extensions import Buffer
$ uvx ruff check --select UP035 --target-version py312 test_buffer.py --diff
-from typing_extensions import Buffer
+from collections.abc import Buffer
Would fix 1 error.

@jorenham

jorenham commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

Similarly, one could introduce a similar rule here, i.e. emit collections.abc.Buffer for Python 3.12+, and typing_extensions.Buffer for <3.12.

This is because using typing_extensions.Buffer would trigger ruff UP035 (deprecated-import, pyupgrade), so I would consider collections.abc.Buffer to be the more canonical variant of Buffer.

Ah yea, that's indeed a problem. Doing it as you suggest crossed my mind, but to be honest I was just being lazy and went with the easy solution :p

…on<3.12`

Co-authored-by: Jonas Dedden <mail@jonas-dedden.de>

@Tpt Tpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you! Just a test issue and some ideas where push backs are more than welcome

Comment thread src/buffer.rs Outdated
Comment thread guide/src/type-stub.md Outdated
Comment thread src/buffer.rs
…ffer` fix

Co-authored-by: Thomas Tanon <thomas@pellissier-tanon.fr>
@codspeed-hq

codspeed-hq Bot commented Sep 9, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 12.77%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 2 improved benchmarks
✅ 139 untouched benchmarks
⏩ 6 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
into_biguint_zero 1.4 µs 1.2 µs +13.49%
into_biguint_small 1.5 µs 1.4 µs +12.06%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing jorenham:introspection/typing_extensions.Buffer (20edb7e) with main (360f899)

Open in CodSpeed

Footnotes

  1. 6 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Tpt
Tpt enabled auto-merge September 9, 2026 13:00
@Tpt
Tpt added this pull request to the merge queue Sep 9, 2026
Merged via the queue into PyO3:main with commit da9da49 Sep 9, 2026
51 of 53 checks passed
@jorenham
jorenham deleted the introspection/typing_extensions.Buffer branch September 9, 2026 14:05
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.

3 participants