Skip to content

Allow errors keyword argument in codecs _Encoder, _Decoder, and stream protocols - #16697

Closed
hrithiksaini99 wants to merge 1 commit into
python:mainfrom
hrithiksaini99:fix-codecs-decoder-errors-kwarg
Closed

hrithiksaini99 wants to merge 1 commit into
python:mainfrom
hrithiksaini99:fix-codecs-decoder-errors-kwarg

Conversation

@hrithiksaini99

Copy link
Copy Markdown
Contributor

Fixes #16441.

In stdlib/codecs.pyi, _Encoder, _Decoder, _StreamReader, and _StreamWriter were annotated with trailing /, making all parameters (including errors: str = ...) positional-only.

However, codecs.Codec.encode, codecs.Codec.decode, and encoder/decoder implementations accept errors by keyword (e.g. codecs.lookup('utf-8').decode(b'potato', errors='replace')). This caused mypy to issue false positive errors:

error: Unexpected keyword argument "errors" for "__call__" of "_Decoder" [call-arg]

By placing the / after input / stream (def __call__(self, input: ReadableBuffer, /, errors: str = ...) -> tuple[str, int]: ...), the first argument remains positional-only for flexible parameter naming across implementations, while errors is accessible both positionally and by keyword.

Added type assertion tests in stdlib/@tests/test_cases/check_codecs.py covering keyword errors calls on info.decode and info.encode.

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please remove the tests. In typeshed, we only add regression tests for functions and classes which are known to have caused complex problems in the past, or where stubs are difficult to get right. 100% test coverage for typeshed is neither necessary nor desirable, as it would lead to code duplication.

See tests/REGRESSION.md for more information.

@JelleZijlstra

Copy link
Copy Markdown
Member

Pretty sure this is wrong, see #16447.

@hrithiksaini99

Copy link
Copy Markdown
Contributor Author

Thanks both. @JelleZijlstra is right: the _Encoder/_Decoder protocols describe all codecs, and several of them (e.g. codecs.lookup("ascii").decode(b"abc", errors="replace") -> TypeError: _codecs.ascii_decode() takes no keyword arguments) reject errors as a keyword, so making it keyword-accessible in the shared protocol is incorrect.

#16447 takes the correct narrower approach by special-casing the UTF-8 codec info, so I'll close this PR rather than duplicate that work.

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.

false alarm on decode call

3 participants