◐ Shell
reader mode source ↗
Skip to content

typevar as module#6834

Merged
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:typevar
Jan 22, 2026
Merged

typevar as module#6834
youknowone merged 1 commit into
RustPython:mainfrom
youknowone:typevar

Conversation

@youknowone

@youknowone youknowone commented Jan 22, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Refactor
    • Reorganized internal typing module structure for improved maintainability and code organization.
    • Enhanced error handling and validation for type parameter operations.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 22, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A significant refactoring reorganizes typing-related types into a dedicated typevar submodule within the VM stdlib. TypeVar, ParamSpec, TypeVarTuple, ParamSpecArgs, ParamSpecKwargs, and Generic types move from the typing module to the new typevar module with updated constructors, trait implementations, and module initialization wiring.

Changes

Cohort / File(s) Summary
Typevar Module Restructuring
crates/vm/src/stdlib/typevar.rs
Extracted typevar submodule with pub exports; reorganized TypeVar, ParamSpec, TypeVarTuple, ParamSpecArgs, ParamSpecKwargs, and Generic types. Added constructors with argument validation, trait implementations (Representable, Iterable, Comparable, Constructor), and helper functions (_call_typing_func_object, call_typing_args_kwargs, set_typeparam_default). Updated error handling for constructor validation across all new types.
Typing Module Wiring
crates/vm/src/stdlib/typing.rs
Updated pymodule attribute to include with(super::typevar::typevar) parameter. Removed module_exec initialization of TypeVar, ParamSpec, ParamSpecArgs/Kwargs, TypeVarTuple, and Generic; retained only NoDefault and Union exports. Removed class::PyClassImpl import.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Possibly related PRs

Suggested reviewers

  • arihant2math

Poem

🐰 Hop and reorganize, the typing types align,
TypeVar, ParamSpec, in their module so fine,
Submodules bustling with constructors anew,
Wiring them up—oh, what rabbits can do!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'typevar as module' clearly and directly describes the main structural change: converting typevar functionality into a dedicated module rather than keeping it as inline code.
Docstring Coverage ✅ Passed Docstring coverage is 80.82% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@youknowone youknowone marked this pull request as ready for review January 22, 2026 12:01

@coderabbitai coderabbitai Bot 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.

Hide comment

Actionable comments posted: 2

🤖 Fix all issues with AI agents
In `@crates/vm/src/stdlib/typevar.rs`:
- Around line 423-530: ParamSpec::__default__ currently evaluates
self.evaluate_default on every call but doesn't cache the result; modify the
method so that after calling the evaluate_default callable you store the result
into self.default_value (acquire the same lock as used for evaluate_default,
e.g., let mut default_value = self.default_value.lock(); *default_value =
evaluated_result.clone(); ) and then return that cached value; ensure the
initial NoDefault check still uses vm.ctx.typing_no_default and that you return
the cached default on subsequent calls instead of re-invoking evaluate_default.
- Around line 268-383: The TypeVar implementation (Constructor::py_new for
TypeVar) is not validating that the provided constraints are actual types nor
that the name is a string; likewise ParamSpec and TypeVarTuple constructors need
name validation and ParamSpec needs its bound validated with type_check. Fix: in
TypeVar::py_new ensure the extracted name is validated as a str (use vm.to_str
or equivalent/type_check for "name must be a str") and run type_check on each
constraint value (and on bound where present) before storing them; mirror this
logic in the ParamSpec constructor (validate name and call type_check on bound)
and in the TypeVarTuple constructor (validate name as str), referencing the
functions/constructors named py_new for TypeVar, ParamSpec, and TypeVarTuple and
the existing type_check helper to perform the validations and return TypeError
on invalid inputs.

Hide details View details @youknowone youknowone merged commit 8cbaf3c into RustPython:main Jan 22, 2026
13 checks passed
@youknowone youknowone deleted the typevar branch January 22, 2026 13:06
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.

1 participant