Skip to content

Conversation

@sobolevn
Copy link
Member

@sobolevn sobolevn commented Oct 30, 2022

dumps and detect_encoding support bytearray:

>>> import json >>> json.loads(bytearray(b'false')) False >>> json.detect_encoding(bytearray(b'')) 'utf-8'

But, not memoryview:

>>> json.loads(memoryview(b'false')) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/sobolev/.pyenv/versions/3.10.0/lib/python3.10/json/__init__.py", line 339, in loads raise TypeError(f'the JSON object must be str, bytes or bytearray, ' TypeError: the JSON object must be str, bytes or bytearray, not memoryview
@github-actions
Copy link
Contributor

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

optuna (https://github.com/optuna/optuna) - optuna/storages/_rdb/storage.py:388: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes]" + optuna/storages/_rdb/storage.py:388: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes, bytearray]" - optuna/storages/_rdb/storage.py:398: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes]" + optuna/storages/_rdb/storage.py:398: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes, bytearray]" - optuna/storages/_rdb/storage.py:409: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes]" + optuna/storages/_rdb/storage.py:409: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes, bytearray]" - optuna/storages/_rdb/storage.py:420: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes]" + optuna/storages/_rdb/storage.py:420: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes, bytearray]" - optuna/storages/_rdb/storage.py:454: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes]" + optuna/storages/_rdb/storage.py:454: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes, bytearray]" - optuna/storages/_rdb/storage.py:455: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes]" + optuna/storages/_rdb/storage.py:455: error: Argument 1 to "loads" has incompatible type "Column"; expected "Union[str, bytes, bytearray]" 
Copy link
Collaborator

@hauntsaninja hauntsaninja left a comment

Choose a reason for hiding this comment

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

Lgtm

Copy link
Member

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

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

loads explicitly checks for str | bytes | bytearray. detect_encoding does .startswith which doesn't exist on any other well-known buffer type.

@JelleZijlstra JelleZijlstra merged commit 6569dea into python:master Oct 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

3 participants