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

Dataclasses are not working in scripts #537

Closed
binaryaaron opened this issue Oct 18, 2023 · 5 comments
Closed

Dataclasses are not working in scripts #537

binaryaaron opened this issue Oct 18, 2023 · 5 comments

Comments

@binaryaaron
Copy link

binaryaaron commented Oct 18, 2023

Hi! First off, it's am amazing framework and I love using it instead of a billion yaml files to run my house. Thanks for your efforts.

I just noticed that a simple dataclass doesn't work, likely due to lack of an async def ast_annassign(...) function in eval.py.

e.g., i've had an existing script that broke after upgrading to 1.5 + HASS 2023.10.0

from dataclasses import dataclass

...

@dataclass
class HVACSettings:
    mode: str
    temp: int
    fan_speed: str
    time_trigger: str

and in my logs:

2023-10-18 14:47:57.010 ERROR (MainThread) [custom_components.pyscript.file.hvac] Exception in </config/pyscript/hvac.py> line 53:
        mode: str
        ^
NotImplementedError: file.hvac: not implemented ast ast_annassign

Curious if this was ever supposed to work or something else has changed. Could probably make a PR here to fix if you think adding the ast_anassign method is "right".

@craigbarratt
Copy link
Member

It would be great if you could submit a PR with support for ast_annassign.

craigbarratt added a commit that referenced this issue Oct 20, 2023
@craigbarratt
Copy link
Member

I went ahead and added ast_annassign.

@binaryaaron
Copy link
Author

Thanks!

@phyrwork
Copy link

Is there a release scheduled? I have this problem and would appreciate this fix.

@contagon
Copy link
Contributor

contagon commented May 23, 2024

I'm also unsure if this is actually working. Running off the master branch allows it to load with error, but when running it appears the attributes are never set.

from dataclasses import dataclass

@dataclass
class MyClass:
    name: str

c = MyClass("Foo")
log.warning(c.name)

Results in

Exception in </config/pyscript/myfile.py> line 11:
    log.warning(c.name)
                ^
AttributeError: 'MyClass' object has no attribute 'name'

If anyone knows a workaround, I'm all ears!

EDIT: Figured out my own problem, similar solution to using enums,

from dataclasses import make_dataclass

MyClass = make_dataclass("MyClass", [("name", int)])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants