Skip to content

Specify how to annotate variable types #6

Closed

Description

@JukkaL

We should probably have a single convention for attribute and module-level variable annotations that would at least be recommended (even if other ways would also work).

Here are some potential approaches:

  • x = 0, x = '', etc. (at least for built-in types)
  • x = Undefined(int)
  • x = Undefined # type: int
  • # var x: int (a proposal for PEP 484, still under discussion)
  • x = ClassName()

Activity

  1. JukkaL commented on Apr 13, 2015

    @JukkaL
    ContributorAuthor

    Another option:

    • x = None # type: int (the type will be int, not Optional[int])
  2. JukkaL commented on Apr 13, 2015

    @JukkaL
    ContributorAuthor

    Yet another option:

    • x = cast(int, None)
  3. vlasovskikh commented on Jun 26, 2015

    @vlasovskikh
    Member

    The PEP now allows specifying the type of a variable without specifying its value:

    x = ...  # int

    For variables with known default values we could use:

    x = 42  # int
  4. gvanrossum commented on Jan 11, 2016

    @gvanrossum
    Member

    The PEP picked a syntax. It is used in many stubs, and mypy supports it. So I think this issue is fixed.

  5. added a commit that references this issue on Sep 4, 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

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions