Pyhass / Pyhiveapi

A python library to interface with the hive home api
MIT License
24 stars 18 forks source link

Add unasync rule to convert 'asycio.Lock' to 'threading.Lock' in session.py #75

Closed gormanb closed 9 months ago

gormanb commented 10 months ago

This patch addresses the issue described in #74. When pyhiveapi is packaged via unasync, it removes the await from this asyncio.Lock acquisition in HiveSession.updateData but otherwise leaves the asyncio.Lock in place. As a result, calling updateData generates the following warning:

/path/to/session.py:328: RuntimeWarning: coroutine 'Lock.acquire' was never awaited
  self.updateLock.acquire()

... and can throw the following exception if it attempts to release the lock without having acquired it:

[1/13/2024, 2:20:09 PM] Error: Python exception: Lock is not acquired.

This patch adds an unasync rule to convert the asyncio.Lock in session.py to threading.Lock instead. No other code is affected by this change.

gormanb commented 9 months ago

@KJonline: The unasync rule for pyhiveapi/api is to ensure that the existing additional_replacements are maintained unchanged for that directory. unasync unfortunately doesn't appear to allow replacements at any granularity other than per-directory, nor does it allow replacing tokens like asyncio.Lock, but it does allow you to overrule the replacements specified on higher-level directories with rules for their subdirectories.

So the first rule now says "change apyhiveapi to pyhiveapi and asyncio to threading everywhere," while the second rule says "except in pyhiveapi/api, where we do not apply the asyncio > threading replacement." The result is that the only place where asyncio is changed to threading is in session.py. Aside from that, the process produces exactly the same output as before.