Skip to content

Fix Python 3.14 SyntaxWarning: 'return' in 'finally' block#200

Merged
DasBasti merged 2 commits into
mainfrom
copilot/fix-syntax-warning-finally-block
Jun 7, 2026
Merged

Fix Python 3.14 SyntaxWarning: 'return' in 'finally' block#200
DasBasti merged 2 commits into
mainfrom
copilot/fix-syntax-warning-finally-block

Conversation

Copilot AI commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Python 3.14 (PEP 765) emits SyntaxWarning for return inside finally blocks, since it silently suppresses exceptions. Seven vehicle data parsers trigger this on every Home Assistant startup.

Moved return retval from inside finally to after the try/except block in all affected files:

  • vehicle/battery.py
  • vehicle/climate.py
  • vehicle/maintenance.py
  • vehicle/position.py
  • vehicle/running.py
  • vehicle/safety.py
  • vehicle/tires.py

Before:

try:
    ...
except KeyError as e:
    _LOGGER.info(f"... info not available: {e}")
finally:
    return retval

After:

try:
    ...
except KeyError as e:
    _LOGGER.info(f"... info not available: {e}")
return retval

Behavior is preserved — retval is always returned whether parsing succeeds or a KeyError is caught. The difference is that unexpected exceptions (not KeyError) will now properly propagate instead of being silently swallowed.

…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
Copilot AI requested a review from DasBasti June 7, 2026 11:56
@DasBasti DasBasti marked this pull request as ready for review June 7, 2026 12:48
Copilot AI review requested due to automatic review settings June 7, 2026 12:48
@DasBasti DasBasti merged commit a11c8a3 into main Jun 7, 2026
10 checks passed
@DasBasti DasBasti deleted the copilot/fix-syntax-warning-finally-block branch June 7, 2026 12:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 retval patterns in 7 vehicle parsers and replaced with return retval after the try/except.
  • Ensured KeyError continues to be handled gracefully while allowing other exceptions to propagate (with one noted inconsistency in battery.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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SyntaxWarning: 'return' in a 'finally' block with Python 3.14

3 participants