pynvim
pynvim copied to clipboard
Buffer class makes unexpected RPC calls
The Buffer class makes unexpected RPC calls.
This is due to the __len__ method being called during boolean tests.
For example, the following code will print 2.
class Example:
def __len__(self) -> int:
print(2)
return 0
example = Example()
if example:
pass
This is very surprising behaviour, and there is absoutely no way to fix it without breaking backwards compatibility.
But it should be documentated, because this will actually break your code if you are using Buffer class from another thread.
It took me a quite a bit of effort to figure out that an or statement was causing crashes in my plugin.
It could also implement __bool__ itself (returning True always), couldn't it? https://docs.python.org/3.8/reference/datamodel.html?highlight=del#object.bool
I think thats an excellent idea, even though it would be a breaking change.
im not sure what our policy is on that though
Are there any circumstances that you would the buffer to be false-y?
I think it seems reasonable to override __bool__. It seems unlikely to break anything? Unless it returns false when there are no lines and people are relying on that.
ya, thats the case.
a very minor breaking change, but then again i wouldn't be surprised if people actually relied on it.
PR welcome. We monkey-patch various things, this is more or less idiomatic in python land.