Harden all_cells parameter filtering, return-type handling, and packing defaults#668
Conversation
all_cells parameter filtering, return-type handling, and packing defaults
Reviewer's GuideTightens how qpdk.samples.all_cells introspects cell callables, handles non-Component return types, and configures default packing behavior, with focused regression tests to prevent regressions. Flow diagram for updated all_cells cell handling and packing defaultsflowchart TD
A[Iterate over cell_funcs] --> B[Call cell_func]
B --> C{_is_qpdk_cell returns True?}
C -->|No| A
C -->|Yes| D{cell is gf.Component?}
D -->|Yes| G[Append cell to cells]
D -->|No| E{cell is gf.ComponentReference?}
E -->|No| F[warnings.warn and skip]
F --> A
E -->|Yes| H[Create gf.Component name=name_wrap]
H --> I[c_wrap.add_ref_off_grid cell]
I --> J[Set cell to c_wrap]
J --> G
G --> K{More cell_funcs?}
K -->|Yes| A
K -->|No| L{cells list empty?}
L -->|Yes| M[Return Component empty_all_cells]
L -->|No| N[kwargs.setdefault max_size to None None]
N --> O[gf.pack cells spacing=spacing with kwargs]
O --> P[Return packed Component]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/test_all_cells_sample.py" line_range="18-27" />
<code_context>
+ return func
+
+
+def test_all_cells_does_not_treat_var_keyword_as_required(recwarn, monkeypatch) -> None:
+ """Ensure variadic keyword parameters are not treated as required arguments."""
+
+ @_mark_as_qpdk_cell
+ def accepts_var_keyword(**options):
+ _ = options
+ return gf.Component()
+
+ @_mark_as_qpdk_cell
+ def requires_argument(length):
+ _ = length
+ return gf.Component()
+
+ monkeypatch.setattr(
+ qpdk,
+ "PDK",
+ SimpleNamespace(cells={"accepts_var_keyword": accepts_var_keyword, "requires_argument": requires_argument}),
+ )
+
+ _ = all_cells(spacing=201)
+
+ warning_messages = [str(w.message) for w in recwarn]
+ assert any("requires arguments ['length']" in msg for msg in warning_messages)
+ assert not any("requires arguments ['options']" in msg for msg in warning_messages)
+
+
</code_context>
<issue_to_address>
**suggestion (testing):** Consider asserting on the resulting component to fully validate the `**kwargs` handling path.
You already verify that `**options` isn’t treated as a required arg via warnings. To strengthen this test, also assert something about the `all_cells` result—for instance, that it returns a non-empty component set or a specific number of placed cells—so we confirm the var-keyword-only path is functionally correct, not just warning-free.
Suggested implementation:
```python
result = all_cells(spacing=201)
assert isinstance(result, gf.Component)
assert len(result.references) == 2
warning_messages = [str(w.message) for w in recwarn]
assert any("requires arguments ['length']" in msg for msg in warning_messages)
assert not any("requires arguments ['options']" in msg for msg in warning_messages)
```
If `all_cells` returns a different structure than a single `gf.Component` (e.g., a container object or a list of components), update the assertions accordingly:
1. Change `isinstance(result, gf.Component)` to match the actual return type.
2. Replace `len(result.references) == 2` with an assertion that checks the expected number of placed cells/components in the returned structure, still validating that both `accepts_var_keyword` and `requires_argument` were instantiated.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| def test_all_cells_does_not_treat_var_keyword_as_required(recwarn, monkeypatch) -> None: | ||
| """Ensure variadic keyword parameters are not treated as required arguments.""" | ||
|
|
||
| @_mark_as_qpdk_cell | ||
| def accepts_var_keyword(**options): | ||
| _ = options | ||
| return gf.Component() | ||
|
|
||
| @_mark_as_qpdk_cell | ||
| def requires_argument(length): |
There was a problem hiding this comment.
suggestion (testing): Consider asserting on the resulting component to fully validate the **kwargs handling path.
You already verify that **options isn’t treated as a required arg via warnings. To strengthen this test, also assert something about the all_cells result—for instance, that it returns a non-empty component set or a specific number of placed cells—so we confirm the var-keyword-only path is functionally correct, not just warning-free.
Suggested implementation:
result = all_cells(spacing=201)
assert isinstance(result, gf.Component)
assert len(result.references) == 2
warning_messages = [str(w.message) for w in recwarn]
assert any("requires arguments ['length']" in msg for msg in warning_messages)
assert not any("requires arguments ['options']" in msg for msg in warning_messages)If all_cells returns a different structure than a single gf.Component (e.g., a container object or a list of components), update the assertions accordingly:
- Change
isinstance(result, gf.Component)to match the actual return type. - Replace
len(result.references) == 2with an assertion that checks the expected number of placed cells/components in the returned structure, still validating that bothaccepts_var_keywordandrequires_argumentwere instantiated.
Co-authored-by: nikosavola <7860886+nikosavola@users.noreply.github.com>
ddb17ea to
49cd49a
Compare
Code Coverage OverviewLanguages: Python Python / code-coverage/pytestThe overall coverage in the branch remains at 90%, unchanged from the branch. Show a code coverage summary of the most impacted files.
Code Coverage is in Public Preview. Learn more and provide us with your feedback. |
qpdk/samples/all_cells.pyhad three correctness/readability gaps: required-parameter detection relied on parameter name matching, non-Componentreturn handling assumedadd_ref_off_gridcompatibility, and the unconstrainedmax_sizedefault was undocumented. This PR tightens parameter/return-type checks and documents default packing behavior explicitly.Parameter introspection correctness
**kwargswith kind-based filtering (inspect.Parameter.VAR_KEYWORD) when computing required params.Safer handling of non-
Componentcell returnsgf.Component→ used directlygf.ComponentReference→ wrapped viaadd_ref_off_gridAttributeError-style failures on unsupported return objects.Documented unconstrained packing default
**kwargsto explain defaultmax_size=(None, None).setdefaultcall to make the intent clear at the point of behavior.Focused regression coverage
tests/test_all_cells_sample.pyto cover:**kwargsnot treated as required parametersmax_sizedefaults to unconstrained and respects explicit overrideOriginal prompt
Summary by Sourcery
Harden all_cells sample parameter detection, return-type handling, and default packing behavior.
Bug Fixes:
Enhancements:
Tests: