← Back to DAGDA | Support | Source: https://github.com/python/mypy/issues/4468
Skip to content

Adding a 'pure' function type annotation? #4468

Closed

Description

@wizzardx

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

Activity

  1. ilevkivskyi commented on Jan 14, 2018

    @ilevkivskyi
    Member

    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.

  2. wizzardx commented on Jan 14, 2018

    @wizzardx
    ContributorAuthor

    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".

  3. gvanrossum commented on Jan 15, 2018

    @gvanrossum
    Member
  4. wizzardx commented on Jan 16, 2018

    @wizzardx
    ContributorAuthor

    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?

  5. gvanrossum commented on Jan 16, 2018

    @gvanrossum
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions