Skip to content

Implement Const type to indicate non-rebindable variables/attributes #1214

Closed

Description

@hashstat

As a Python developer, I would like a Const type so that I can indicate that a variable or attribute may not be rebound to another value. This would be especially useful for module and class constants, but could also be helpful for instance attributes that really shouldn't be modified. Not only does it help prevent accidentally overwriting constants, it would hopefully reduce the need to use @property on non-critical attributes. Here is a contrived example:

GRUNT = 0   # type: Const
MANAGER = 1   # type: Const


class Employee:
    kind = GRUNT   # type: Const

    def __init__(self, idnum: int, name: str):
        self.id = idnum   # type: Const
        self.name = name


class Manager(Employee):
    kind = MANAGER   # type: Const

kind = MANAGER # type: Const reqiures the Const type to rebind kind without an error just as a const variable in C requires a cast during reassignment to quiet compiler warnings/errors.

I'm not sure if it makes sense to allow Const to take parameters as the type can be inferred in most cases. One might want other variables, such as those passed into functions, to be const, in which case it makes sense to allow Const to take parameters. Here is an example:

def frobnicate(kind: Const[int]) -> None:
    kind = 5   # Error
    kind = 6   # type: Const  # Okay

Activity

  1. JukkaL commented on Feb 19, 2016

    @JukkaL
    Collaborator

    Early in the history of mypy I was actually planning to have something like this, but I guess I later forgot about it. This would likely require a PEP 484 (and potentially typing) change.

  2. added this to the milestone on Mar 1, 2016
  3. removed this from the milestone on Mar 29, 2017
  4. davidroeca commented on Apr 29, 2017

    @davidroeca

    Const in Python could have similar guarantees to something like JavaScript ES6's const, to prevent reassignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const

    A clever programmer would be able to get around this through methods that mutate, or by a shared reference, but the guard rails are still nice.

    Alternatively, it could only be allowed for immutable non-container primitives such as int or str, or maybe even extended to tuples/namedtuples of immutable primitives as well

  5. JukkaL commented on May 18, 2018

    @JukkaL
    Collaborator

    We are now considering something like this, perhaps spelled as Final. An interesting use case would be with literal types (another potential new feature) -- it would allow mypy to infer things like if X: never being taken if there is X: Final = False.

  6. added a commit that references this issue on Sep 11, 2018
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