Skip to content

Generate type hints for python #675

@KOLANICH

Description

@KOLANICH

Python 3 (2 is EOL) allows to add some type hints. They are not enforced without special measures, but can be used by linters (like mypy), REPLs and langservers.

  1. import typing to the top of a file
  2. Output type of a function is shown by an -> before :
  3. Types of arguments are shown by adding : TypeOfArg right after arg name
  4. Union types are typing.Union[T1, T2]
  5. Lists are typing.List[T], or maybe even more generic typing.Iterable[T]
  6. type of self is usually not marked as a type of an arg
  7. If a func returns nothing, use -> None
  8. You need to set the types as strings containing their names if the type is not yet defined:
def a(b: "A") -> "A":
  return b
class A:
  def __init__(self, asdf: typing.Optional["A"] = None) -> None:
    pass
class B:
  def __init__(self, asdf: A) -> None:
    pass

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions