[Python-ideas] Statically checking purity of functions
Christoph Groth
christoph at grothesque.org
Fri Jun 10 06:10:37 EDT 2016
Paul Moore wrote:
> It's quite similar to Perl's "taint checks" that track whether
> data is affected by external input (although the latter marks
> *data*, not functions).
Indeed! Thanks for the pointer, I haven't heard of taint checks
before.
> I've not looked into mypy yet, so all I know about it is from
> skimming some of the typing discussions here. But if it's
> possible to write a plugin for mypy to do something like this,
> that would be pretty awesome.
>
> I'm not sure how much a capability like that would affect Python
> itself, though. (In fact, I'd say it ought not to - being able
> to do something like this without any changes to the language
> would be ideal).
Testing for purity, like testing for type correctness, could be
kept separate from the language. (Although I have the gut feeling
that one day there will be a switch to CPython that enables
mypy-like testing at runtime.) There is one exception: there must
be some syntax to declare (non-)purity.
Function annotations (used by mypy for type checking) set the
__annotations__ attribute. In the same way there could be, for
example, a __pure__ attribute that, if present, can be True or
False. It could be, for example, set by the decorators
"typing.pure" and "typing.impure".
The problem with using decorators, as compared to having a
dedicated syntax, is that the name of a decorator is just a name,
and can be assigned some different value. But I do not see a clear
majority for adding syntax to Python for declaring purity right
now... ;-)
Perhaps this is going too far, but I note that there are other
traits of functions that could be tracked as well. For example,
one could track whether a function is potentially blocking. If
such alternative traits are judged important enough (I do not
consider "blocking" important enough, it's just an example of the
concept), one could also store several such traits in a __traits__
attribute.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 818 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160610/4443549a/attachment.sig>
More information about the Python-ideas
mailing list