json.dump/dumps shouldn't allow `Any` when `default` is None
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 5.1k
- Fork
- 2.1k
- Merge medio
- 1g 19h
- PR unite (30g)
- 82
Descrizione
When default is None, we know what types are serialisable.
Thus,
import json
import types
import typing
def x(data: typing.Mapping[str, str]) -> str:
return json.dumps(data)
def y():
return json.dumps(types.MappingProxyType({}))
Shouldn't type-check.
Running this fails with:
File /opt/homebrew/Cellar/python@3.13/3.13.2/Frameworks/Python.framework/Versions/3.13/lib/python3.13/json/encoder.py:180, in JSONEncoder.default(self, o)
161 def default(self, o):
162 """Implement this method in a subclass such that it returns
163 a serializable object for ``o``, or calls the base implementation
164 (to raise a ``TypeError``).
(...) 178
179 """
--> 180 raise TypeError(f'Object of type {o.__class__.__name__} '
181 f'is not JSON serializable')
TypeError: Object of type mappingproxy is not JSON serializable
Type-checking JSONEncoder itself would be much harder, but the more-commonly-used functions should be doable via overloads.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Individua gli stub di json.dump e json.dumps e verifica come i relativi overload gestiscono l’argomento predefinito. Controlla gli esempi nell’issue rispetto ai tipi risultanti; il lavoro è completato quando le chiamate senza un argomento predefinito rifiutano gli input non supportati, mentre le chiamate con un argomento predefinito rimangono tipizzate correttamente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100