MPT-22046 reduce _process_value cognitive complexity below threshold#342
Conversation
… threshold Extract the dict- and list-conversion branches of BaseModel._process_value into module-level helpers (_build_model_from_dict and _resolve_list_model_class). This flattens the nested conditionals that drove cognitive complexity to 18, bringing the method under the allowed 15. Behaviour is unchanged; the redundant trailing isinstance(value, BaseModel) check folds into the final return. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository YAML (base), Organization UI (inherited) Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🔗 Linked repositories identifiedCodeRabbit considers these linked repositories for cross-repo context during reviews:
📜 Recent review details⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
🧰 Additional context used📓 Path-based instructions (2)**/*⚙️ CodeRabbit configuration file
Files:
**⚙️ CodeRabbit configuration file
Files:
🧠 Learnings (2)📚 Learning: 2026-02-17T10:04:00.873ZApplied to files:
📚 Learning: 2026-04-16T13:00:41.320ZApplied to files:
🪛 Ruff (0.15.15)mpt_api_client/models/model.py[warning] 129-129: Invalid rule code in Add non-Ruff rule codes to the (RUF102) 🔇 Additional comments (3)
📝 WalkthroughWalkthroughThis PR refactors the ChangesBaseModel nested value conversion refactoring
🎯 2 (Simple) | ⏱️ ~10 minutes 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|



What
Refactors
BaseModel._process_valueinmpt_api_client/models/model.pyto reduce its Cognitive Complexity from 18 to ~4, under the allowed 15 (SonarQube finding).The dict- and list-conversion branches are extracted into two flat module-level helpers:
_build_model_from_dict(value, target_class)— builds aBaseModel/subclass from a raw dict._resolve_list_model_class(target_class)— resolves the element model class for alist[...]-typed field via a guard clause instead of three levels of nesting._process_valueis now three flatifstatements.Why
The cost was concentrated in the list branch, where
if target_class → if origin is list → if args and …reached nesting level 3 (eachifcosts1 + nesting_depth). Flattening into helpers removes the nesting penalty. Helpers are kept module-level (matching the existingto_snake_case/to_camel_casepattern) to avoid pushingBaseModelpast WPS214's method-count limit.Notes for reviewers
if isinstance(value, BaseModel): return valueis folded into the finalreturn value(both returned the value unchanged).# noqa: WPS231 C901suppression on_process_valueis removed (no longer needed)._build_model_from_dictnarrowstarget_classthrough a typed local so mypy sees aBaseModelreturn rather thanAny.Verification
make checkpasses clean (ruff format,ruff check,flake8/WPS,mypy,uv lock --check); 118 model unit tests pass.Context
Split out of MPT-21922 — this refactor was originally committed on that bug's branch but is unrelated to it. Tracked under MPT-22046.
Closes MPT-22046
BaseModel._process_valueto reduce Cognitive Complexity from 18 to approximately 4 (below the SonarQube threshold of 15)_build_model_from_dict(value, target_class)_resolve_list_model_class(target_class)_process_valueto three flat if statements, improving code readability and maintainability_process_value