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

prevent overwriting entity variables #11

Closed
basnijholt opened this issue Sep 2, 2020 · 5 comments
Closed

prevent overwriting entity variables #11

basnijholt opened this issue Sep 2, 2020 · 5 comments

Comments

@basnijholt
Copy link
Contributor

basnijholt commented Sep 2, 2020

I just accidentally did

light.living_room_lights = "off"

and now my lights in Home Assistant became unavailable.

Maybe it an idea to now allow overwriting entities?

The same goes for state, service, task, etc.

@craigbarratt
Copy link
Member

I don't know why they become unavailable. Is it because the attributes get cleared when you do this?

This seems related to issue #9.

@craigbarratt
Copy link
Member

With commit 712692f, state variable setting now preserves attributes.

@fleXible
Copy link
Contributor

fleXible commented Sep 9, 2020

While playing around I came across a different problem regarding overwriting variables.
I seems, that declared variables take presedence over provided variables/objects.

Sample code:

@time_trigger
def precendce_problem():
    log.info(f"precendce_problem: running")
    event = None
    event.fire("raises Exception")

precendce_problem: running
Exception in <jupyter_0.precendce_problem> line 35:
event.fire("raises Exception")
^
AttributeError: 'NoneType' object has no attribute 'fire'

I came across this, when doing a for loop and naming the variable "state" without thinking too much about it.
It wouldn't be too much of an issue, if variables would be scoped, but as they are not, weird things can happen if you don't watch out.

@craigbarratt
Copy link
Member

Variable scoping should be the same as Python, and I agree it would be a bug if it wasn't. If you create a local variable it will overwrite a previously defined function or object.

The example looks correct to me. This behaves exactly the same in Python and pyscript:

bytes.fromhex("1234")
bytes = None
bytes.fromhex("5678")

The first call works, and the second one fails with an error:

AttributeError: 'NoneType' object has no attribute 'fromhex'

Here's another example that also works the same in Python and pyscript:

import math
print(math.sqrt(2))
math = None
print(math.sqrt(2))

I agree there is a subtle difference in pyscript: yes, event or state don't exist as an object. But if it does (ie: you set them to something), that takes precedence over the event.fire or state.set functions, which is the same behavior as regular Python.

@fleXible
Copy link
Contributor

What you say makes sense, I just never happen to stumble upon it before. Issue can be closed.

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

3 participants