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

Should it work with Dict[sometype, sometype]? #247

Open
belonesox opened this issue Oct 16, 2023 · 0 comments
Open

Should it work with Dict[sometype, sometype]? #247

belonesox opened this issue Oct 16, 2023 · 0 comments
Labels
bug Something isn't working

Comments

@belonesox
Copy link

Consider a sample:

import dataclasses as dc
import dacite
from typing import List, Dict


@dc.dataclass
class TestProfileSpec:
    distro:   str = ''
    setup:    str = ''

class TestProfiles(Dict[str, TestProfileSpec]):
    ...

@dc.dataclass
class TestsSpec:
    profiles: TestProfiles = dc.field(default_factory=TestProfiles)


adict = {'profiles': 
            {'debian': 
                {'distro': 'debian:12', 
                 'setup': 'sudo apt-get install firefox-esr -y'
                }
            }, 
}    

parsed_ = dacite.from_dict(
        data_class=TestsSpec, 
        data=adict, 
        config=dacite.Config(cast=[TestProfiles, TestProfileSpec]))

print(type(parsed_))  # OK → <class '__main__.TestsSpec'>
print(type(parsed_.profiles)) # OK → <class '__main__.TestProfiles'>

print(type(parsed_.profiles['debian'])) # Not OK, → <class 'dict'> , wanted TestProfileSpec

See last line, I want to get TestProfileSpec type, but get standard dict.

I am not sure, if it bug or feature (I have not see any examples of dacite with «typed Dicts»),
but I want any idea how to get it working (to get right class in last line).

@belonesox belonesox added the bug Something isn't working label Oct 16, 2023
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