-
Notifications
You must be signed in to change notification settings - Fork 9
Feature/mypy #569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feature/mypy #569
Conversation
…lly:
[tool.mypy]
strict = false
disable_error_code = [
"arg-type", "return-value", "assignment", "union-attr",
"index", "operator", "dict-item", "attr-defined",
"override", "var-annotated", "misc", "type-var",
"type-arg", "list-item", "call-overload", "return",
"import-untyped"
]
Code fixes made:
1. flixopt/structure.py: Fixed callable → Callable[..., Any] | None type hint
2. flixopt/flow_system.py: Fixed __eq__ method signature (Liskov substitution)
Dependencies added:
- mypy==1.16.0
- pandas-stubs==2.2.3.250527
- types-PyYAML==6.0.12.20250516
- types-tqdm==4.67.0.20250417
Current status:
Success: no issues found in 31 source files
Next steps to incrementally enable stricter checks:
1. Enable one error code at a time (e.g., remove "var-annotated" from disabled list)
2. Fix the resulting errors
3. Repeat until desired strictness is achieved
The most impactful ones to enable first would be:
- var-annotated - Add type annotations to empty collections
- union-attr - Add None checks for Optional values
- override - Fix method signature mismatches
- effects.py: __contains__(self, item: object) - fixed Liskov substitution - results.py: _get_container_groups() -> dict[str, ContainerMixin[Any]] - flow_system.py: _get_container_groups() -> dict[str, ContainerMixin[Any]]
- Added cast to imports
- Line 569: Changed (comparison).any() to np.any(comparison) for scalar compatibility
- Lines 879-889: Used cast('InvestParameters', ...) when with_investment is True to access .mandatory, .minimum_or_fixed_size, .maximum_or_fixed_size
- Lines 1082-1087: Refactored previous_status property to explicitly check for None on flow.submodel, flow.submodel.status, and ._previous_status
components.py:
- Added cast to imports
- Lines 573-575: Used cast('xr.DataArray', ...) for charge state values before calling .isel()
- Line 600: Changed (comparison).any() to np.any(comparison) for scalar compatibility
- Lines 1115-1116: Used cast('xr.DataArray', ...) for charge state bounds
- Lines 1494, 1540: Used cast('xr.DataArray', ...) for relative_loss_per_hour before calling .mean()
Results:
- mypy: Success: no issues found in 31 source files
- ruff: All checks passed!
- tests: 52 passed
Changes made to fix the 58 union-attr errors:
1. structure.py: Added if submodel is not None guards in dict comprehensions for results_structure
2. optimization.py: Added and calc.model is not None to generator expressions
3. features.py:
- Added if self._model.effects is None: raise RuntimeError(...) checks before accessing effects
- Used isinstance(effects_dict, dict) guards before calling .items() on Effect_PS types
- Added and self.startup is not None guard for startup variable access
4. effects.py:
- Added None checks for objective/penalty submodels before accessing .total
- Added isinstance(share_from_temporal, dict) and isinstance(share_from_periodic, dict) guards
- Added None checks for source submodels and their properties
5. results.py: Fixed return type annotation for plot_heatmap function
Tests: All 52 tests pass
Ruff: All checks passed
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 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 |
Mypy Status: - ✅ uv run mypy flixopt → Success: no issues found in 31 source files - ✅ uv run ruff check flixopt/ → All checks passed - ✅ Tests: 1563 passed, 3 skipped Type Annotations Added: ┌──────────────────────┬──────────────┐ │ File │ Functions │ ├──────────────────────┼──────────────┤ │ structure.py │ 27 │ ├──────────────────────┼──────────────┤ │ components.py │ 20 │ ├──────────────────────┼──────────────┤ │ elements.py │ 18 │ ├──────────────────────┼──────────────┤ │ linear_converters.py │ 14 │ ├──────────────────────┼──────────────┤ │ io.py │ 12 │ ├──────────────────────┼──────────────┤ │ comparison.py │ 12 │ ├──────────────────────┼──────────────┤ │ network_app.py │ 13 │ ├──────────────────────┼──────────────┤ │ plotting.py │ 1 (draw_pie) │ ├──────────────────────┼──────────────┤ │ Total │ 117 │ └──────────────────────┴──────────────┘ Key Fixes Applied: - Added None checks for submodels, investments, and effects (42+ union-attr errors fixed) - Used isinstance guards for InvestParameters attribute access - Added cast() for post-transform DataArray values - Fixed matplotlib stubs limitation with # type: ignore[union-attr] for Text.set_weight - Fixed set.add() in list comprehension issue in elements.py - Moved Generator import to TYPE_CHECKING block in io.py
Description
Brief description of the changes in this PR.
Type of Change
Related Issues
Closes #(issue number)
Testing
Checklist