neovim / pynvim

Python client and plugin host for Nvim
http://pynvim.readthedocs.io/en/latest/
Apache License 2.0
1.48k stars 118 forks source link

Buffer class makes unexpected RPC calls #449

Open ms-jpq opened 4 years ago

ms-jpq commented 4 years ago

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.

blueyed commented 4 years ago

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__

ms-jpq commented 4 years ago

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

im not sure what our policy is on that though

tjdevries commented 3 years ago

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.

ms-jpq commented 3 years ago

ya, thats the case.

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

justinmk commented 3 years ago

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