Show HN: Localscope–Limit scope of Python functions for reproducible execution (localscope.readthedocs.io)
localscope is a small Python package that disassembles functions to check if they access global variables they shouldn't. I wrote this a few years ago to detect scope bugs which are common in Jupyter notebooks. It's recently come in handy writing jax code (https://github.com/jax-ml/jax) because it requires pure functions. Thought I'd share.
31 comments
[ 3.0 ms ] story [ 85.7 ms ] threadYou see a problem, you fix it with library, and I applaud that. You have to wonder though, how many years does it take for a reproducible notebook environment to implement out of scope variable guards..
It's just a safeguard for well-intentioned people to prevent themselves from making mistakes with their existing tools, instead of changing to a completely different set of tools.
Luckily there are new approaches, e.g. Marimo and Pluto, that don't have the same root issue.
[1]: https://docs.hhvm.com/hack/contexts-and-capabilities/introdu...
I've hit it in both R and Python in interactive sessions. Otherwise it's generally a non issue.
I can definitely see this being helpful, particularly for the intended context. It would also be useful when you need to move notebook code to a different environment.
I completely see the value in this tool for a particular style of programming.
The decorator syntax is purely sugar:
is equivalent to except that `bar` only gets assigned once.It's possible to use metaprogramming to apply the decorator automatically to every function (by iterating over attributes of the module and filtering for functions).
This is actually a cleaner API so might switch my code to it, amazing work
Nope!
> Have you ever scratched your head because your code broke after restarting the Python kernel?
Also nope!
I'm technically a data scientist but I don't use notebooks very often. Even when I did, though, I can't recall having this error very often. Is this really necessary? Anyway, this kind of thing is only useful if people adopt it. The people who are likely to install this tool and use it probably already avoid this issue while the people who have this issue will never hear of this tool or will not consistently employ it.
I guess I'm just salty from years of dealing with people who give their Python code half-hearted incorrect type hints but never run a static type checker on their code.
I could see projects adopting this, or something like it, in their CI pipelines.
Both for open source projects, but also at companies with a lot of internal repositories with Python code – especially if it is common that other people with little experience for some repo contribute code to it.
In the case of an open source project if it’s a popular repo that a lot of people are users of and different people frequently contribute to for the first time.
And in the case of company internal repositories, if it’s code that different teams only touch now and then, with only a smaller group of people intimately familiar with all of the inner workings of the code and the other people being mainly focused on other repos in the company but still having to sometimes contribute changes because their code is relying on the code in that other repos.
Would be interesting to see how it compares to static analysis tools like mypy or linters—does it catch edge cases they might miss? Nice work!