pynvim icon indicating copy to clipboard operation
pynvim copied to clipboard

Buffer class makes unexpected RPC calls

Open ms-jpq opened this issue 5 years ago • 5 comments

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.

ms-jpq avatar Jul 13 '20 08:07 ms-jpq

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

blueyed avatar Jul 13 '20 11:07 blueyed

I think thats an excellent idea, even though it would be a breaking change.

im not sure what our policy is on that though

ms-jpq avatar Jul 13 '20 16:07 ms-jpq

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.

tjdevries avatar Aug 08 '20 19:08 tjdevries

ya, thats the case.

a very minor breaking change, but then again i wouldn't be surprised if people actually relied on it.

ms-jpq avatar Aug 08 '20 21:08 ms-jpq

PR welcome. We monkey-patch various things, this is more or less idiomatic in python land.

justinmk avatar Aug 30 '20 19:08 justinmk