Mypy: Adding a 'pure' function type annotation?

Created on 14 Jan 2018  Â·  5Comments  Â·  Source: python/mypy

This is a feature request.

If a function is annotated as 'pure', then it's checked that it doesn't operate against global state. ie, always returns inputs based only on arguments passed to it, and it doesn't do IO.

Also, functions annotated as 'pure' can only call other functions annotated as 'pure'

More or less, how the D lang describes them:

https://dlang.org/spec/function.html#pure-functions

feature priority-2-low

All 5 comments

It looks like it is possible to implement this in mypy (although not an easy task due to many implicit calls in Python, like __iter__() is called by for cycles). The problematic part is updating all the typeshed stubs marking all pure functions as @pure (hundreds files), otherwise such decorator will be quite useless.

Therefore I think this is very low priority.

The (eventual) implementation would probably work similar to "Any". ie, "purity of function is not known". You'd turn on a "strict" option to enable those warnings.

(same deal with most of typeshed, it's using an implicit Any in a huge amount of places, until pull requests come along which fix those).

The distinction you're mentioning there is described in the D wiki article, as being the difference between "pure" and "strong pure".

With just "pure", functions can modify their arguments (eg: self, for things like __iter__).

Functions that are annotated with both "pure" and only accept "Const" args or return a "Const" result, would be closer to "strong pure".

I don't think this is worth the complexity. It smells similar to tracked
exceptions.

On Jan 14, 2018 2:37 AM, "wizzardx" notifications@github.com wrote:

The (eventual) implementation would probably work similar to "Any". ie,
"purity of function is not known". You'd turn on a "strict" option to
enable those warnings.

(same deal with most of typeshed, it's using an implicit Any in a huge
amount of places, until pull requests come along which fix those).

The distinction you're mentioning there is described in the D wiki
article, as being the difference between "pure" and "strong pure".

With just "pure", functions can modify their arguments (eg: self, for
things like iter), or other vars (not covered by "Const", which is in
separate issue/milestone).

Functions that are annotated with both "pure" and only accept "Const" args
or return a "Const" result, would be closer to "strong pure".

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/python/mypy/issues/4468#issuecomment-357502549, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ACwrMuO3Xm3DborHpID1uGF6AO8uZfX2ks5tKdj5gaJpZM4RdhZN
.

One system that generalises these: effect systems.

Some details of how it's handled in Nim:

https://nim-lang.org/docs/manual.html#effect-system

Is that perhaps worthwhile to add to mypy, or also too complicated?

I'm not excited, but then again I've not had time to read those docs. But unless you've implemented such a system yourself for a real language I'd suggest that perhaps you have no idea how much work it would be for mypy. If you have implemented such a thing before, I recommend that you have a look at the mypy source code and think about how hard it would be to add it there. You'd also have to come up with a detailed design for how to write this without changing Python's core syntax. If you're not interested in doing this, feel free to close the issue now -- nobody on the mypy team has the time either.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arquolo picture arquolo  Â·  3Comments

ericnchen picture ericnchen  Â·  3Comments

mxr picture mxr  Â·  3Comments

Stiivi picture Stiivi  Â·  3Comments

ambv picture ambv  Â·  3Comments