__doc__ handing in the right way by youknowone · Pull Request #6390 · RustPython/RustPython
Walkthrough
Class codegen no longer emits a __doc__ entry when no docstring is provided; the load_docstring helper was removed. PyType construction now always creates member descriptors for __slots__ and prefers the type's own dict for __doc__ resolution, setting __doc__ in the type dict when absent.
Changes
| Cohort / File(s) | Summary |
|---|---|
Class compilation / docstring crates/codegen/src/compile.rs |
Removed fn load_docstring(&mut self, doc_str: Option<String>); emit __doc__ only when Some(doc) is present (omit write when no docstring) |
Type initialization & lookup crates/vm/src/builtins/type.rs |
Always create and assign PyMemberDescriptor entries for __slots__ members (overriding inherited attrs); set __doc__ in the type dict if absent; change __doc__ read to use the type's own dict (get_direct_attr) and use descriptor __get__ when present |
Test update extra_tests/snippets/syntax_class.py |
Change assertion from A.__doc__ == None to A.__doc__ is None (identity check, clearer failure message) |
Estimated code review effort
🎯 3 (Moderate) | ⏱️ ~25 minutes
- Pay attention to:
crates/vm/src/builtins/type.rs— verify overriding semantics for slot descriptors and any unintended shadowing of inherited attributes.crates/codegen/src/compile.rs— ensure bytecode emitted when omitting__doc__matches VM expectations.- Integration between codegen and VM
__doc__behavior (absent vs explicitNonein type dict).
Possibly related PRs
- type.__dict__ #5957 — Related edits to
vm/src/builtins/type.rsaffecting type attribute insertion and slot handling. - Fix __dict__ getset type #6010 — Overlapping changes around slot-based descriptors and type initialization behavior.
- Fix PyFunction doc behavior #5827 — Prior adjustments to
__doc__handling and descriptor/lookup semantics.
Poem
🐇 I tuck my docstrings neat and light,
Only store them when they’re right.
Slots wear badges, plainly shown,
Own-dict truths are safely known.
Hop, compile — the garden's bright!
Pre-merge checks and finishing touches
❌ Failed checks (1 warning, 1 inconclusive)
| Check name | Status | Explanation | Resolution |
|---|---|---|---|
| Docstring Coverage | Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. | You can run @coderabbitai generate docstrings to improve docstring coverage. |
|
| Title check | ❓ Inconclusive | The title contains a typo ('handing' instead of 'handling') and is vague about the specific improvements made to doc handling across the codebase. | Correct the typo and clarify the title to better describe the changes, such as 'Fix doc handling in class compilation and type initialization' or 'Make doc handling conditional and align with CPython behavior'. |
✅ Passed checks (1 passed)
| Check name | Status | Explanation |
|---|---|---|
| Description Check | ✅ Passed | Check skipped - CodeRabbit’s high-level summary is enabled. |
✨ Finishing touches
- 📝 Generate docstrings
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
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.
Comment @coderabbitai help to get the list of available commands and usage tips.