json.dump/dumps shouldn't allow `Any` when `default` is None
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 5.1k
- Forks
- 2.1k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 82
Beschreibung
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.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Finde die Stubs von json.dump und json.dumps und untersuche, wie ihre Overloads mit dem Standardargument umgehen. Vergleiche die Beispiele im Issue mit den resultierenden Typen; abgeschlossen ist die Aufgabe, wenn Aufrufe ohne ein Standardargument nicht unterstützte Eingaben ablehnen, während Aufrufe mit einem Standardargument weiterhin angemessen typisiert sind.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100