Title: `itertools.compress(x)` calls `x.__iter__()` before reporting the missing `selectors` argument · Issue #9506 · RustPython/RustPython · GitHub
Open Graph Title: `itertools.compress(x)` calls `x.__iter__()` before reporting the missing `selectors` argument · Issue #9506 · RustPython/RustPython
X Title: `itertools.compress(x)` calls `x.__iter__()` before reporting the missing `selectors` argument · Issue #9506 · RustPython/RustPython
Description: Feature A call that leaves out a required argument converts the arguments it did pass first, so an exception from that conversion replaces the TypeError for the missing one. CPython counts the arguments before converting any. Reproductio...
Open Graph Description: Feature A call that leaves out a required argument converts the arguments it did pass first, so an exception from that conversion replaces the TypeError for the missing one. CPython counts the argu...
X Description: Feature A call that leaves out a required argument converts the arguments it did pass first, so an exception from that conversion replaces the TypeError for the missing one. CPython counts the argu...
Opengraph URL: https://github.com/RustPython/RustPython/issues/9506
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"`itertools.compress(x)` calls `x.__iter__()` before reporting the missing `selectors` argument","articleBody":"## Feature\n\nA call that leaves out a required argument converts the arguments it did pass first, so an exception from that conversion replaces the `TypeError` for the missing one. CPython counts the arguments before converting any.\n\n### Reproduction\n\n```python\nimport itertools\n\n\nclass RaisingIter:\n def __iter__(self):\n raise RuntimeError(\"__iter__ called\")\n\n\nitertools.compress(RaisingIter())\n```\n\n**RustPython:**\n\n```\nRuntimeError: __iter__ called\n```\n\n**CPython:**\n\n```\nTypeError: compress() missing required argument 'selectors' (pos 2)\n```\n\n`itertools.compress(data=RaisingIter())` behaves the same.\n\n### Root cause\n\n[`CompressNewArgs`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/stdlib/itertools.rs#L152-L158) declares `data` as `PyIter`, and the `FromArgs` derive converts each field as soon as it takes it and reports a missing one only when it reaches that field ([`from_args.rs`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/derive-impl/src/from_args.rs#L247-L258)), so `data.__iter__()` runs first. [`FuncArgs::bind_for`](https://github.com/RustPython/RustPython/blob/713e14cfc78d06372c68a1804240de52a8340548/crates/vm/src/function/argument.rs#L310-L326) checks only the upper bound before `T::from_args`, while CPython's `_PyArg_UnpackKeywords` checks both bounds before `itertools_compress_impl` calls `PyObject_GetIter`.\n\n### Fix\n\nIn `bind_for`, before `T::from_args`, find the first required positional parameter in `T::PARAMS` that no positional or keyword argument fills and raise its error, next to the surplus check from #9242.\n\n### Environment\n\n- RustPython [713e14c](https://github.com/RustPython/RustPython/tree/713e14cfc78d06372c68a1804240de52a8340548) (Python 3.15.0a), also reproduced on 0.4.0 and 0.6.1\n- CPython v3.15.2, also v3.14.4 and v3.12.13\n- OS: macOS arm64 (713e14c), Debian 12 (0.4.0, 0.6.1)\n\n## Python Documentation or reference to CPython source code\n\n- CPython `itertools_compress`: [`Modules/clinic/itertoolsmodule.c.h#L860-L867`](https://github.com/python/cpython/blob/v3.15.2/Modules/clinic/itertoolsmodule.c.h#L860-L867) settles the argument count before calling the implementation:\n\n ```c\n fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser,\n /*minpos*/ 2, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);\n if (!fastargs) {\n goto exit;\n }\n seq1 = fastargs[0];\n seq2 = fastargs[1];\n return_value = itertools_compress_impl(type, seq1, seq2);\n ```\n\n- CPython `itertools_compress_impl`: [`Modules/itertoolsmodule.c#L3251-L3254`](https://github.com/python/cpython/blob/v3.15.2/Modules/itertoolsmodule.c#L3251-L3254) calls `iter()` on each argument only after that.\n\n- Documentation: [`itertools.compress`](https://docs.python.org/3.15/library/itertools.html#itertools.compress)\n\nAI disclosure: this report was drafted with Claude Code (claude-opus-5-5) and reviewed by me before submission. The divergence comes from a differential-testing run against CPython 3.12.13, and every source reference above was checked against the linked revisions.","author":{"url":"https://github.com/jseop-lim","@type":"Person","name":"jseop-lim"},"datePublished":"2026-11-01T03:43:17Z","url":"https://github.com/RustPython/RustPython/issues/9506"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:9c0df3f6-c21c-0ce1-1870-7c8e020f4a98 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 991E:161320:13B38EA:1A1EA30:6B01F2F8 |
| html-safe-nonce | 8893d1c25b5dd6f7da819c5ea385241685a0a6fe2ff1391a1def47b1f36d7f50 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5OTFFOjE2MTMyMDoxM0IzOEVBOjFBMUVBMzA6NkIwMUYyRjgiLCJ2aXNpdG9yX2lkIjoiNTk0ODYwODgyODY0MjgxNjc2MCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 8763d59a4074271c8b2a16f1a3e56c725da5a4c466eff6b2efe51c1d792f3f72 |
| hovercard-subject-tag | issue:6142893082 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/9506/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8744fcf0505809c0975012f6775ebb63d81597cd0f6b33af04518ab15eb8fe6f/RustPython/RustPython/issues/9506 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8744fcf0505809c0975012f6775ebb63d81597cd0f6b33af04518ab15eb8fe6f/RustPython/RustPython/issues/9506 |
| og:image:alt | Feature A call that leaves out a required argument converts the arguments it did pass first, so an exception from that conversion replaces the TypeError for the missing one. CPython counts the argu... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jseop-lim |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width