Skip to content

fix: KeyError swallowing in default_serializer#4420

Merged
provinzkraut merged 2 commits intomainfrom
sobolevn-patch-3
Oct 11, 2025
Merged

fix: KeyError swallowing in default_serializer#4420
provinzkraut merged 2 commits intomainfrom
sobolevn-patch-3

Conversation

@sobolevn
Copy link
Copy Markdown
Member

It used to do several things that are not quite right:

  1. KeyError raised in encoder(value) call was swallowed, it could potentially lead to a very hard to debug problems, when a KeyError bug in encoder would just select another one
  2. .get + if is None is a bit tiny faster than [] with except KeyError in some cases

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 11, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.83%. Comparing base (e09e7b0) to head (3a8ea88).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4420   +/-   ##
=======================================
  Coverage   97.83%   97.83%           
=======================================
  Files         296      296           
  Lines       15288    15288           
  Branches     1711     1711           
=======================================
  Hits        14957    14957           
  Misses        189      189           
  Partials      142      142           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@provinzkraut
Copy link
Copy Markdown
Member

provinzkraut commented Oct 11, 2025

  1. .get + if is None is a bit tiny faster than [] with except KeyError in some cases

Really? Do you know why? I was under the impression that since 3.11 (I think it was this version?), KeyError should be faster in the happy path, i.e. if not exception is raised, because of the reduced overhead from exception handlers

@sobolevn
Copy link
Copy Markdown
Member Author

Yeap, you are right :)

KeyError is faster. Sorry!

» pyperf timeit -s 'encoders = {str: str}' '          
. try:
.    encoders[str]
. except KeyError:
.    pass
. '
.....................
Mean +- std dev: 11.9 ns +- 0.1 ns

vs

» pyperf timeit -s 'encoders = {str: str}' '
enc = encoders.get(str)
if enc is not None: ...
'
.....................
Mean +- std dev: 17.4 ns +- 0.2 ns

I will update my PR.

@provinzkraut provinzkraut changed the title fix: speed up and remove KeyError swallowing in default_serializer fix: KeyError swallowing in default_serializer Oct 11, 2025
@provinzkraut provinzkraut enabled auto-merge (squash) October 11, 2025 10:22
It used to do several things that are not quite right:
1. `KeyError` raised in `encoder(value)` call was swallowed, it could potentially lead to a very hard to debug problems, when a `KeyError` bug in encoder would just select another one
2. `.get` is a bit tiny faster than `[]` with `except KeyError` in some cases
@provinzkraut provinzkraut merged commit 673c1e9 into main Oct 11, 2025
27 checks passed
@provinzkraut provinzkraut deleted the sobolevn-patch-3 branch October 11, 2025 10:26
@github-actions
Copy link
Copy Markdown

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4420

provinzkraut pushed a commit that referenced this pull request Dec 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants