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

ValueError: dictionary update sequence element #0 has length 1; 2 is required #243

Closed
rafaelfalcaro opened this issue Sep 14, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@rafaelfalcaro
Copy link

Describe the bug
I'm trying to create a class object of PLCProgram type from a dict, but this error is returned: "ValueError: dictionary update sequence element #0 has length 1; 2 is required"

To Reproduce
Json file with dict:

{
  "plc_programs": [
    {
      "id": 0,
      "type": "iocstop",
      "cs_motors": {
        "1": [1, 2, 3],
        "2": [4, 5, 6, 7]
      },
      "motors": [10, 11, 12]
    },
    {
      "id": 1,
      "type": "iocstop",
      "cs_motors": {
        "2": [1, 2, 3]
      }
    }
  ]
}

The code:

from dacite import from_dict, Config
from dataclasses import dataclass
from enum import Enum
from typing import Optional
import json

class PLCProgramType(str, Enum):
    """Enumerate PLC Program types available."""
    IOCSTOP = "iocstop"

@dataclass
class PLCProgram:
    """PLC Program data structure."""
    id: int
    type: PLCProgramType
    cs_motors: Optional[list[dict]] = None
    motors: Optional[list[int]] = None


with open("data.json", "r", encoding="utf-8") as f:
        data = json.load(f)

plc_progs = []

for plc_prog in data["plc_programs"]:
  config = Config(type_hooks={PLCProgramType: PLCProgramType})
  plc_progs.append(from_dict(PLCProgram, plc_prog, config))

print(plc_progs)

Environment

  • Python version: 3.11.4
  • dacite version: 1.81
@rafaelfalcaro rafaelfalcaro added the bug Something isn't working label Sep 14, 2023
@rafaelfalcaro
Copy link
Author

I've found my error:

+ cs_motors: Optional[list[dict]] = None
- cs_motors: Optional[dict] = None

The opened issue is not a bug of dacite lib.

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