custom-components / pyscript

Pyscript adds rich Python scripting to HASS
Apache License 2.0
873 stars 46 forks source link

Can't declare properties in class #605

Closed jmot205 closed 4 months ago

jmot205 commented 4 months ago

I have a module that contains a function with a @service decorator. This service function instantiates a class and runs a method of the object. However I received the following error if the class that is being instantiated contains a property: Exception in <file.light_store.light_store> line 246: entity_id=entity_id, invert_entity_list=invert_entity_list ^ TypeError: 'EvalFunc' object is not callable

Trimming out all the properties removes the error, and then adding even the following dummy property to the class results in the error:

@property
def test(self):
    return 1
ALERTua commented 4 months ago

This is a known language limitation. For simple properties you can use https://hacs-pyscript.readthedocs.io/en/stable/reference.html#pyscript-compile

jmot205 commented 4 months ago

Ah thanks for confirming that. It seems like this really should be listed in the language limitations section of the docs - I went through this first but when I didn't see it there I figured I was encountering a bug or something.

Unfortunately the properties I'm defining (not the dummy example shown above) are using pyscript functionality, so from the docs it sounds like the pyscript_compile decorator won't work for these.

In this case, I might just change them to be method calls instead of properties to get around this limitation.

ALERTua commented 4 months ago

you are free to create a pull request to cover this in documentation, but the author of this repository seems to have abandoned it for a while now. I also converted all my properties to methods. I guess you can close this issue now.

jmot205 commented 4 months ago

Shame that it does seem abandoned; I'll add to docs when I get the chance, though I guess it might not go anywhere. Cheers for the steer.