Skip to content

Covert to defaultdict instead of plain dict #519

@4l1fe

Description

@4l1fe
  • cattrs version: 23.2.3
  • Python version: 3.11.2
  • Operating System: Ubuntu 22.04

Description

I expect the method SelectorConfig.load() to convert the toml text to the defaultdict type in SelectorConfig.properties. Instead, it is converted as dict

How to configure structurizing to make it recognizing the exact type which is DefaultDict[str, LineStringProperties] ?

What I Did

config_converter = TomlkitConverter()

@attrs.define
class LineStringProperties:
    pinned: bool = False
    comment: str = ''
    theme_group: str = LIGHT_THEME


@attrs.define
class SelectorConfig:
    properties: DefaultDict[str, LineStringProperties] = attrs.field(factory=lambda: defaultdict(LineStringProperties))

   @staticmethod
    def load(config_path: Path) -> 'SelectorConfig':
        if not config_path.exists():
            return SelectorConfig()
        
        text = config_path.read_text()
        config = config_converter.loads(text, SelectorConfig)
        return config

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions