lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 150 forks source link

AttributeError: 'list' object has no attribute 'items' #510

Open WolfgangFahl opened 5 months ago

WolfgangFahl commented 5 months ago

Description

almost the same as #507

culprit code: File "...dataclasses_json/core.py", line 150, in _decode_dataclass kvs = {decode_names.get(k, k): v for k, v in kvs.items()} AttributeError: 'list' object has no attribute 'items'

Code snippet that reproduces the issue

see https://github.com/WolfgangFahl/pyLoDStorage/blob/66cc1f9312d106c2d4c460a76d4757257627f296/lodstorage/sample2.py#L135

@dataclass_json
@dataclass
class Country:
    """
    Represents a country with its details.

    Attributes:
        name (str): The name of the country.
        country_code (str): The country code.
        capital (str): The capital city of the country.
        timezones (List[str]): List of timezones in the country.
        latlng (List[float]): Latitude and longitude of the country.
    """
    name: str
    country_code: str
    capital: str
    timezones: List[str]
    latlng: List[float]

@dataclass_json
@dataclass
class Countries:
    """
    Represents a collection of country instances.

    Attributes:
        countries (List[Country]): A list of Country instances.
    """
    countries: List[Country]

    @classmethod 
    def get_countries_erdem(cls)->'Countries':
        """
        get Erdem Ozkol's country list
        """
        country_json_url = "https://gist.githubusercontent.com/erdem/8c7d26765831d0f9a8c62f02782ae00d/raw/248037cd701af0a4957cce340dabb0fd04e38f4c/countries.json"
        instance=cls.load_from_json_url(country_json_url)
        return instance

    @classmethod
    def get_samples(cls) -> dict[str, "Erdem Ozkol's Country list"]:
        """
        Returns a dictionary of named samples
        for 'specification by example' style
        requirements management.

        Returns:
            dict: A dictionary with keys as sample names 
            and values as `Countries` instances.
        """
        samples = {"Erdem Ozkol's country list":cls.get_countries_erdem()}
        return samples

Describe the results you expected

successfully reading in the dataclass or giving a reasonable error message where the problem is

Python version you are using

Python 3.10.13

Environment description

dataclasses-json==0.6.3

WolfgangFahl commented 5 months ago
  if not isinstance(kvs, dict):
        raise ValueError(f"Input 'kvs' for class {cls.__name__} must be a dictionary, but received {type(kvs)}")

already helps