Skip to content

python/json-without-type

Rule json-without-type
Pack Python
Severity warning
Fix none, edit by hand
Library versions every version

Returning the result of json.loads gives the caller Any, so nothing names the shape of the data. Read the fields where you parse them and return a NamedTuple.

import json
from typing import NamedTuple
def quote(body: bytes) -> dict:
return json.loads(body)
class Quote(NamedTuple):
price: float
def quote(body: bytes) -> Quote:
return Quote(**json.loads(body))

The rule reports a function that returns the result of json.loads or json.load, or a key of that result. It leaves alone a result that the function reads into a typed value first.

xpd has no automatic fix, so make the change by hand.

json.loads returns Any, so the type checker accepts every use of the result. When a function returns it, a wrong key raises far from the place where the data came in, or doesn't raise at all. The standard library has no decoder that checks the type of each value. Quote(**json.loads(body)) raises a TypeError on a missing key and on a key that Quote does not have, and the callers get named fields. When the JSON has more keys than the function needs, read those keys by name and pass them to the NamedTuple.

xpd prints this message with the issue. A name that starts with $ stands for code from your file.

This returns Any to the caller, so nothing names the shape of the data. Read the fields here, and return a NamedTuple, such as Quote(**json.loads(body)).

Browse the other rules of the Python pack.