Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
WolfgangFahl opened this issue Jan 25, 2024 · 1 comment
Open

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

WolfgangFahl opened this issue Jan 25, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@WolfgangFahl
Copy link

WolfgangFahl commented Jan 25, 2024

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 WolfgangFahl added the bug Something isn't working label Jan 25, 2024
@WolfgangFahl
Copy link
Author

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

already helps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant