interp: compute __abstractmethods__ in _abc_init and propagate __isabstractmethod__#554
interp: compute __abstractmethods__ in _abc_init and propagate __isabstractmethod__#554fregataa wants to merge 2 commits into
Conversation
…stractmethod__ `_abc_init` only set up `_abc_registry` and never computed `__abstractmethods__`, so ABCs were freely instantiable and `inspect.isabstract` always returned False. Add `compute_abstract_methods` (mirrors `_abcmodule.c` / `app_abc.py`): collect the abstract members in `cls.__dict__` plus those inherited from bases and not overridden, and store them on `cls.__abstractmethods__`. Also wire up the abstract-method flag on the descriptor wrappers: - property gains an `__isabstractmethod__` getset (fget/fset/fdel). - staticmethod/classmethod `__new__` honour the subtype, so the legacy `abstractstaticmethod`/`abstractclassmethod` subclasses behave correctly. - `w_type_get_flags` reports `Py_TPFLAGS_IS_ABSTRACT` from `flag_abstract`, which `inspect.isabstract` reads. test_abc: 24 failures + 8 errors -> 11 failures, 0 errors. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Warning Review limit reached
Next review available in: 34 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
WalkthroughABC initialization now derives inherited and local abstract methods, descriptors expose abstractness, subclass descriptor constructors preserve their type, and type flags include the abstract-class bit. ChangesAbstract class support
Estimated code review effort: 3 (Moderate) | ~25 minutes Sequence Diagram(s)sequenceDiagram
participant abc_init
participant compute_abstract_methods
participant ClassDictionary
participant BaseClasses
abc_init->>compute_abstract_methods: compute cls.__abstractmethods__
compute_abstract_methods->>ClassDictionary: inspect class members
compute_abstract_methods->>BaseClasses: incorporate inherited abstract names
compute_abstract_methods-->>abc_init: assign frozenset
Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e6b02d35b6
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| let base_abstracts = match crate::baseobjspace::getattr_str(base, "__abstractmethods__") { | ||
| Ok(v) if !v.is_null() => v, | ||
| _ => continue, | ||
| }; | ||
| for name_obj in unsafe { w_set_items(base_abstracts) } { |
There was a problem hiding this comment.
Iterate inherited abstracts through Python protocol
When a base class has a user-assigned __abstractmethods__ such as None/a list, or a metaclass lookup raises a non-AttributeError, _py_abc.py's for name in getattr(base, "__abstractmethods__", set()) uses normal iteration and propagates non-missing errors. This branch instead treats every error as missing and then unconditionally casts whatever value was returned with w_set_items, whose safety contract requires a real set object; subclass creation can silently ignore lookup failures or dereference a non-set object rather than raising/iterating correctly.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@pyre/pyre-interpreter/src/module/_abc/mod.rs`:
- Around line 35-43: Update is_abstract to delegate directly to the existing
baseobjspace::isabstractmethod_w helper, preserving its Result<bool, PyError>
behavior. Remove the duplicated getattr, truthiness, and AttributeError handling
while leaving the null guards at both call sites unchanged.
- Around line 65-92: Guard the inherited __abstractmethods__ value before
iterating in the abstract-method resolution loop: validate it with the existing
set-or-frozenset check before calling w_set_items, and validate each name object
with the existing string check before calling w_str_get_value_opt. Skip invalid
values, matching the safety pattern used by abstract_instantiation_error.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: cd855e8c-f1f5-4c52-bbd4-07d1144182f4
📒 Files selected for processing (3)
pyre/pyre-interpreter/src/module/_abc/mod.rspyre/pyre-interpreter/src/typedef.rspyre/pyre-object/src/typeobject.rs
| // `getattr(value, "__isabstractmethod__", False)` truthiness (`app_abc.py`). | ||
| fn is_abstract(value: PyObjectRef) -> Result<bool, crate::PyError> { | ||
| match crate::baseobjspace::getattr_str(value, "__isabstractmethod__") { | ||
| Ok(v) if !v.is_null() => crate::baseobjspace::is_true(v), | ||
| Ok(_) => Ok(false), | ||
| Err(e) if e.kind == crate::PyErrorKind::AttributeError => Ok(false), | ||
| Err(e) => Err(e), | ||
| } | ||
| } |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win
Reimplements baseobjspace::isabstractmethod_w instead of calling it.
Both call sites (lines 59, 87) already null-guard value before invoking is_abstract, so this can just delegate to the existing canonical helper rather than re-encoding the same getattr + truthiness + AttributeError-swallow logic a second time.
♻️ Proposed simplification
fn is_abstract(value: PyObjectRef) -> Result<bool, crate::PyError> {
- match crate::baseobjspace::getattr_str(value, "__isabstractmethod__") {
- Ok(v) if !v.is_null() => crate::baseobjspace::is_true(v),
- Ok(_) => Ok(false),
- Err(e) if e.kind == crate::PyErrorKind::AttributeError => Ok(false),
- Err(e) => Err(e),
- }
+ crate::baseobjspace::isabstractmethod_w(value)
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // `getattr(value, "__isabstractmethod__", False)` truthiness (`app_abc.py`). | |
| fn is_abstract(value: PyObjectRef) -> Result<bool, crate::PyError> { | |
| match crate::baseobjspace::getattr_str(value, "__isabstractmethod__") { | |
| Ok(v) if !v.is_null() => crate::baseobjspace::is_true(v), | |
| Ok(_) => Ok(false), | |
| Err(e) if e.kind == crate::PyErrorKind::AttributeError => Ok(false), | |
| Err(e) => Err(e), | |
| } | |
| } | |
| // `getattr(value, "__isabstractmethod__", False)` truthiness (`app_abc.py`). | |
| fn is_abstract(value: PyObjectRef) -> Result<bool, crate::PyError> { | |
| crate::baseobjspace::isabstractmethod_w(value) | |
| } |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@pyre/pyre-interpreter/src/module/_abc/mod.rs` around lines 35 - 43, Update
is_abstract to delegate directly to the existing
baseobjspace::isabstractmethod_w helper, preserving its Result<bool, PyError>
behavior. Remove the duplicated getattr, truthiness, and AttributeError handling
while leaving the null guards at both call sites unchanged.
compute_abstract_methods' Stage 2 fed base.__abstractmethods__ straight into w_set_items / w_str_get_value_opt, which cast without a type check. Since the __abstractmethods__ setter marks any truthy value abstract, a non-set value (e.g. `C.__abstractmethods__ = [1, 2, 3]`) type-confused and aborted when a subclass was created. Guard with is_set_or_frozenset / is_str, matching the existing abstract_instantiation_error pattern. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Summary
_abc_initonly set up_abc_registryand never computed__abstractmethods__, so ABCs were freely instantiable andinspect.isabstractalways returned False. This PR completes the ABC abstract-method machinery._abc_initcomputes__abstractmethods__— newcompute_abstract_methods(mirrors_abcmodule.c/app_abc.py): the abstract members incls.__dict__plus those inherited from bases and not overridden with a concrete member.property.__isabstractmethod__getset descriptor (isabstract(fget) or fset or fdel).staticmethod/classmethod__new__honour the subtype, so the legacyabstractstaticmethod/abstractclassmethodsubclasses behave correctly (subtype instance returned,__init__runs, class-level__isabstractmethod__ = Trueis visible).w_type_get_flagsreportsPy_TPFLAGS_IS_ABSTRACTfrom the existingflag_abstract, whichinspect.isabstractreads.Test
python -m test test_abc: 24 failures + 8 errors → 11 failures, 0 errors.Remaining
test_abcfailures are unrelated to this change:test_abstractproperty_basics/test_descriptors_with_abstractmethodare blocked by a separatesuper().<property>bug (property__get__is not invoked throughsuper), reproducible independently ofabc.test_issubclass_bad_argumentsis a pre-existingissubclass()argument-validation gap.🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Bug Fixes