Skip to content

Better type return value of json.detect_encoding#15609

Open
jonathandung wants to merge 2 commits intopython:mainfrom
jonathandung:patch-1
Open

Better type return value of json.detect_encoding#15609
jonathandung wants to merge 2 commits intopython:mainfrom
jonathandung:patch-1

Conversation

@jonathandung
Copy link
Copy Markdown
Contributor

It would be beneficial to annotate the return type of json.detect_encoding as the values that are actually possible, so that the user knows approximately what the function does just by looking at the stub. This would mean changing the signature from

def detect_encoding(b: bytes | bytearray) -> str: ...  # undocumented

to

def detect_encoding(b: bytes | bytearray) -> Literal['utf-8', 'utf-8-sig', 'utf-16', 'utf-16-be', 'utf-16-le', 'utf-32', 'utf-32-be', 'utf-32-le']: ...  # undocumented

.

See the relevant source.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 5, 2026

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

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.

1 participant