Skip to content

Fix codecs.register_error callback type - #16518

Merged
AlexWaygood merged 1 commit into
python:mainfrom
stroxler:fix-codecs-register-error-handler-type
Oct 13, 2026
Merged

AlexWaygood merged 1 commit into
python:mainfrom
stroxler:fix-codecs-register-error-handler-type

Conversation

@stroxler

Copy link
Copy Markdown
Contributor

The callback receives one of the three concrete Unicode codec error types, not just UnicodeError

Model that union so contextual typing exposes their shared attributes. I found this because an engineer using Pyrefly ran into a bug where Pyrefly reported attribute errors on the use of e.start / e.end / e.object as in

import codecs

codecs.register_error(
    "zsh", lambda e: ("".join(f"<{b:02x}>" for b in e.object[e.start : e.end]), e.end)
)

@github-actions

This comment has been minimized.

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you!

Comment thread stdlib/@tests/test_cases/check_codecs.py Outdated
Comment thread stdlib/_codecs.pyi Outdated
The callback receives one of the three concrete Unicode codec error types. Model that union so contextual typing exposes their shared attributes.
@stroxler
stroxler force-pushed the fix-codecs-register-error-handler-type branch from 094b25f to a531872 Compare October 13, 2026 16:40
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

scipy-stubs (https://github.com/scipy/scipy-stubs)
- tests/misc/test_submodules_empty.pyi:3: note: See https://mypy.readthedocs.io/en/stable/running_mypy.html#missing-imports

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

thank you!

@AlexWaygood
AlexWaygood merged commit 37e9746 into python:main Oct 13, 2026
88 checks passed
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.

2 participants