Try statements - fixes for ruff rule PLW0717#865
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors several code paths to comply with Ruff rule PLW0717 (“too many statements in try clause”), primarily by shrinking try-block bodies and extracting helper functions, addressing #864.
Changes:
- Refactored
summaryplotinteractive menu handling and vector/datafile splitting to reduce try-clause size. - Extracted restart unpack/slice/repack logic into a helper function in
restartthinner. - Refactored
fmuobs.autoparse_file()into small_try_parse_*helpers and simplified parser selection flow. - Adjusted a permission-handling test to move setup/assertions out of a large try block.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| tests/test_fmu_copy_revision.py | Moves setup and assertions out of a large try block; adds a small assertion helper. |
| src/subscript/summaryplot/summaryplot.py | Shrinks try scope; extracts interactive menu loop into a helper function. |
| src/subscript/restartthinner/restartthinner.py | Extracts unpack/slice/repack operations into a helper to reduce try size. |
| src/subscript/fmuobs/fmuobs.py | Splits format detection into _try_parse_* helpers to reduce try sizes and improve flow. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #865 +/- ##
==========================================
+ Coverage 84.51% 84.55% +0.03%
==========================================
Files 49 49
Lines 7349 7374 +25
==========================================
+ Hits 6211 6235 +24
- Misses 1138 1139 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
- Break loop on empty stdin read (EOF condition) - Join terminated plot process with timeout before restarting - Force kill process if it doesn't terminate within 5 seconds
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
Comments suppressed due to low confidence (2)
src/subscript/fmuobs/fmuobs.py:217
pd.to_datetime(dframe["DATE"])is now outside thetry/except ValueError. If a file looks like the internal CSV format but contains an invalid DATE value, this will raise and stopautoparse_fileinstead of falling through to other parsers. Wrap the DATE conversion in the same try/except (or useerrors="coerce"and validate) to avoid breaking format auto-detection.
)
if "DATE" in dframe:
dframe["DATE"] = pd.to_datetime(dframe["DATE"])
return ("csv", dframe)
src/subscript/fmuobs/fmuobs.py:236
obsdict2df(obsdict)is now outside thetry/exceptguarding YAML parsing. If YAML loads successfully but conversion to dataframe fails with ValueError,autoparse_filewill now error out rather than trying other formats. Consider including the conversion step in the protected block (or catching conversion errors) to keep auto-detection resilient.
if isinstance(obsdict, dict) and (
obsdict.get("smry", None) or obsdict.get("rft", None)
):
logger.info("Parsed %s as a YAML file with observations", filename)
return ("yaml", obsdict2df(obsdict))
- Wrap terminal mode change and interactive menu in try/finally - Use filedesc consistently instead of calling fileno() twice - Add proper subprocess cleanup with timeout and forced kill
…error handling - Move format-checking logic inside try blocks to catch all related exceptions - Broaden exception handling to include OSError alongside ValueError - Add debug logging for failed parse attempts - Initialize dframe variable before try block in _try_parse_ert - Fix grammar in test docstring
| datafiles.append(vecdata) | ||
| summaryfiles.append(sumfn) |
alifbe
left a comment
There was a problem hiding this comment.
Nice 👍🙏
Looks good to me.
Closes #864