Skip to content

Fix URLSafeSerializer.dumps to always return str#412

Closed
daniel7an wants to merge 1 commit intopallets:mainfrom
daniel7an:fix/issue-410-dumps-return-type
Closed

Fix URLSafeSerializer.dumps to always return str#412
daniel7an wants to merge 1 commit intopallets:mainfrom
daniel7an:fix/issue-410-dumps-return-type

Conversation

@daniel7an
Copy link

Summary

URLSafeSerializerMixin.dumps now always returns str regardless of the underlying serializer.

Problem

When using a non-text serializer (e.g. msgpack), URLSafeSerializer.dumps() returns bytes despite the type annotation declaring str as the return type (since URLSafeSerializer extends Serializer[str]):

>>> import msgpack
>>> from itsdangerous import URLSafeSerializer
>>> s = URLSafeSerializer('secret', serializer=msgpack)
>>> type(s.dumps({'id': 123}))
<class 'bytes'>

Fix

Override dumps in URLSafeSerializerMixin to always decode the result to str. Since the payload is base64-encoded, it is always valid UTF-8 and can safely be decoded regardless of the underlying serializer.

This also applies to URLSafeTimedSerializer through the mixin.

Fixes #410

URLSafeSerializerMixin.dumps now always returns str regardless of the
underlying serializer. Since the payload is base64-encoded, it is
always valid UTF-8 and can safely be decoded to str.

Previously, when using a non-text serializer (e.g. msgpack),
dumps() would return bytes despite the type annotation declaring
str as the return type.

Fixes pallets#410
@davidism davidism closed this Feb 9, 2026
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 24, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

URLSafeSerializer.dumps may return bytes

2 participants