Detecting if state is possibly mutated by a function

I’m writing some meta-programming code, and for this I have a Reader Lock and a Writer Lock to guarantee concurrency safety. At the moment, I have to use the Writer Lock for all calls because some function calls could mutate state.

It would be great if there were a mechanism to report whether the function mutates external state, internal state, both internal state and external state, or does not mutate. By external state I mean state anywhere that isn’t like the function’s containing class or anywhere in the case of a module level function. By internal state, I mean something like the state on a class from one of that class’s functions. I’m not going to pretend like I have perfect clarity here. I don’t. But something like this would be nice.

Is there a way to accomplish this that I’m not aware of, prior work I’ve been unable to find, or would anything of the sort require something on the order of a PEP?

There was a interesting discussion about the complexity on pure functions on:

1 Like

The thread seems to really spiral, but it did provide some insight. From this, I believe a PEP introducing new builtins for pureread(), purewrite(), and purefunc() would meet what is needed. I’m going to start a new discussion about some things I’d like to do so I can gather some feedback on how they ought to be accomplished.

In the same scope but maybe slightly off topic, I made this thread a while ago

The innerscope decorator works as a flake8 plugin, it is not exactly a purity checker but is able to prevent outer access and also acts as a “hygienization refactoring multitool”.
I used it a bit and it was quite handy, yet I had problem of IDE slowing down when inspecting huge code and I switched to ruff, which does not supports external plugin creation.

What I mean here is that this way (the innerscope decorator) is perfectly doable, extendable, flexible, low-maintainance and paradox-free (regarding the uncertainties of the definition of ‘purity’). Its drawback is the cpu cost, I hope it might be possible in ruff some day.

1 Like