Skip to content

Forbid instantiating views and iterators with Py_TPFLAGS_DISALLOW_INSTANTIATION - #1595

Merged
asvetlov merged 1 commit into
aio-libs:masterfrom
asvetlov:claude/multidict-view-forbidden-new-b81642
Sep 25, 2026
Merged

asvetlov merged 1 commit into
aio-libs:masterfrom
asvetlov:claude/multidict-view-forbidden-new-b81642

Conversation

@asvetlov

Copy link
Copy Markdown
Member

What do these changes do?

The C view types (_ItemsView, _KeysView, _ValuesView) and the three iterator types blocked direct instantiation with hand-written tp_new slots that just raised TypeError. CPython has provided Py_TPFLAGS_DISALLOW_INSTANTIATION for exactly this since 3.10, the project minimum, so the slots and their helpers are replaced by the flag. The dead PY_VERSION_HEX >= 3.10 guards around Py_TPFLAGS_IMMUTABLETYPE in the same specs are dropped too.

The types were already final (no Py_TPFLAGS_BASETYPE); a new test locks that in. The pure-Python backend has no equivalent types, so it is unaffected.

Are there changes in behavior for the user?

Only the error text: cannot create '..._KeysView' instances directly became CPython's cannot create '..._KeysView' instances. On 3.10, T.__new__(T) is refused by object.__new__'s safety check with object.__new__(T) is not safe instead.

Is it a substantial burden for the maintainers to support this?

No, it removes code.

Related issue number

Follow-up to #1163 and #1377.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes (N/A, private types)
  • If you provide code modification, please add yourself to CONTRIBUTORS.txt (N/A)
  • Add a new news fragment into the CHANGES/ folder
  • make doc-spelling passes and any new technical words are added to docs/spelling_wordlist.txt

Drafted with Claude Code (Claude Opus 5.5); reviewed by @asvetlov.

Agent run details (optional, for reviewers)

Tests, rebased on 27fec2d:

  • C extension, CPython 3.10.17: 2771 passed, 170 skipped
  • C extension, CPython 3.14.7t: 2758 passed, 183 skipped
  • Pure Python (MULTIDICT_NO_EXTENSIONS=1 --no-c-extensions), 3.10.17, clean tree copy: 1301 passed, 17 skipped

Lint: pre-commit on the changed files passed. make doc-spelling passed.
Not verified locally: PyPy (whether cpyext honours Py_TPFLAGS_DISALLOW_INSTANTIATION).

…TANTIATION

Replace the custom tp_new slots that raised TypeError with the type flag
CPython provides for this since 3.10, and drop the dead 3.10 version
guards around Py_TPFLAGS_IMMUTABLETYPE. Add a test that the types stay
final.
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Sep 25, 2026
@asvetlov
asvetlov marked this pull request as ready for review September 25, 2026 20:35
@asvetlov
asvetlov requested a review from webknjaz as a code owner September 25, 2026 20:35
@codspeed

codspeed Bot commented Sep 25, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 310 untouched benchmarks


Comparing asvetlov:claude/multidict-view-forbidden-new-b81642 (a0ef877) with master (eb1eaf6)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (27fec2d) during the generation of this report, so eb1eaf6 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@greptile-apps

greptile-apps Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Replaces custom instantiation guards with a Python flag.

Safe to merge based on the tested paths; no blocking issue was found.

What we checked:

  • Built the parent-commit C extension separately and ran the same executable check against it and the PR extension, exercising six view and iterator types through direct calls, two new paths, and subclass creation in separate processes; all 24 cases on both revisions raised TypeError and produced no uninitialized objects or crashes. T-Rex
  • On CPython 3.11, ran the same executable check against both extensions and exercised keys, items, and values views and iterators for multidicts and proxies, including use-after-delete scenarios and yarl query operations; results matched across revisions, with 106 tests before and 112 after, and three skips on each side. T-Rex
  • Across all cases, every run raised TypeError; no uninitialized object was created and no subprocess crashed, and the changes touched removed Py_tp_new slots and replacement flags did not alter the tested restriction. T-Rex
  • The same executable smoke source produced matching results against both extensions; logs identify the extension loaded, the command, working directory, and exit code, supporting the CPython 3.11-specific hypothesis and not other runtimes. T-Rex

Reviews (1) · Last reviewed commit: "Forbid instantiating views and iterators..."

@asvetlov
asvetlov merged commit 91d5329 into aio-libs:master Sep 25, 2026
59 checks passed
@asvetlov
asvetlov deleted the claude/multidict-view-forbidden-new-b81642 branch September 25, 2026 20:47
@codecov

codecov Bot commented Sep 25, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (27fec2d) to head (a0ef877).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master     #1595   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           41        41           
  Lines         7856      7867   +11     
  Branches       652       653    +1     
=========================================
+ Hits          7856      7867   +11     
Flag Coverage Δ
CI-GHA 100.00% <100.00%> (ø)
pytest 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant