Fix Python 3.14 SyntaxWarning: 'return' in 'finally' block#200
Merged
Conversation
…SyntaxWarning Fixes SyntaxWarning: 'return' in a 'finally' block (PEP 765) in: - vehicle/battery.py - vehicle/climate.py - vehicle/maintenance.py - vehicle/position.py - vehicle/running.py - vehicle/safety.py - vehicle/tires.py
Copilot
AI
changed the title
[WIP] Fix SyntaxWarning in finally block for Python 3.14
Fix Python 3.14 SyntaxWarning: 'return' in 'finally' block
Jun 7, 2026
DasBasti
approved these changes
Jun 7, 2026
There was a problem hiding this comment.
Pull request overview
This PR updates several vehicle data parsers to avoid Python 3.14 SyntaxWarning about return statements inside finally blocks, by moving the return retval to after the try/except so unexpected exceptions are no longer implicitly suppressed.
Changes:
- Removed
finally: return retvalpatterns in 7 vehicle parsers and replaced withreturn retvalafter thetry/except. - Ensured
KeyErrorcontinues to be handled gracefully while allowing other exceptions to propagate (with one noted inconsistency inbattery.py).
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| pysmarthashtag/vehicle/battery.py | Moves return retval out of finally; still has an except Exception catch affecting exception-propagation semantics. |
| pysmarthashtag/vehicle/climate.py | Moves return retval out of finally to avoid suppressing unexpected exceptions. |
| pysmarthashtag/vehicle/maintenance.py | Moves return retval out of finally to avoid suppressing unexpected exceptions. |
| pysmarthashtag/vehicle/position.py | Moves return retval out of finally to avoid suppressing unexpected exceptions. |
| pysmarthashtag/vehicle/running.py | Moves return retval out of finally to avoid suppressing unexpected exceptions. |
| pysmarthashtag/vehicle/safety.py | Moves return retval out of finally to avoid suppressing unexpected exceptions. |
| pysmarthashtag/vehicle/tires.py | Moves return retval out of finally to avoid suppressing unexpected exceptions. |
Comment on lines
272
to
+276
| except KeyError as e: | ||
| _LOGGER.info(f"Battery info not available: {e}") | ||
| except Exception as e: | ||
| _LOGGER.error(f"Error parsing battery data: {e}") | ||
| finally: | ||
| return retval | ||
| return retval |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Python 3.14 (PEP 765) emits
SyntaxWarningforreturninsidefinallyblocks, since it silently suppresses exceptions. Seven vehicle data parsers trigger this on every Home Assistant startup.Moved
return retvalfrom insidefinallyto after thetry/exceptblock in all affected files:vehicle/battery.pyvehicle/climate.pyvehicle/maintenance.pyvehicle/position.pyvehicle/running.pyvehicle/safety.pyvehicle/tires.pyBefore:
After:
Behavior is preserved —
retvalis always returned whether parsing succeeds or aKeyErroris caught. The difference is that unexpected exceptions (notKeyError) will now properly propagate instead of being silently swallowed.