Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Type-annotate all function arguments and internal variables #8351

Closed

Description

@ShadowJonathan

Description:

Related to #8347, (partially) blocked till 3.5 is deprecated

The goal is to;

  • Convert existing name = ... # type: comments to name: type = .... (see "com2ann progress" below)
  • Put type annotations on every function's parameters and return type.
  • Put name: type annotations on;
    • Every class variable.
    • Every instance variable (declared in __init__() functions), unless;
      • They're constructing another instance (self.thing = Thing()) (dubious, see Use inline type hints in handlers/ and rest/ #10382 (comment))
      • They're getting assigned a non-container literal (1, b"", "", etc. but not None)
      • The assignment comes from a function/method call (self.thing = hs.get_another_thing()), given that the function signature is known and inferrable.

com2ann progress

(com2ann changes comment type annotations to inline type annotations, name = ... # type: to name: type = ..., this is possible since python 3.6, and standard across the python ecosystem.)

Activity

  1. clokep commented on Sep 18, 2020

    @clokep
    Member

    This could probably finally allow a mypy linter to run with CI, to catch future inconsistencies.

    mypy runs in CI on a subset of the code-base, see https://github.com/matrix-org/synapse/blob/develop/mypy.ini#L9

  2. added
    Z-Help-WantedWe know exactly how to fix this issue, and would be grateful for any contribution
    z-p3(Deprecated Label)
    on Sep 18, 2020
  3. clokep commented on Sep 18, 2020

    @clokep
    Member

    blocked till 3.5 is deprecated

    This isn't blocked? Python 3.5 supports type annotations.

  4. ShadowJonathan commented on Sep 18, 2020

    @ShadowJonathan
    ContributorAuthor

    blocked till 3.5 is deprecated

    This isn't blocked? Python 3.5 supports type annotations.

    not variable type annotations, but you're right about it not being completely blocked

  5. ShadowJonathan commented on Apr 5, 2021

    @ShadowJonathan
    ContributorAuthor

    A large portion of this can be fixed with com2ann, see #9131 (comment)

  6. added
    T-TaskRefactoring, removal, replacement, enabling or disabling functionality, other engineering tasks.
    and removed
    Z-Help-WantedWe know exactly how to fix this issue, and would be grateful for any contribution
    z-p3(Deprecated Label)
    on Apr 5, 2021
  7. ShadowJonathan commented on Jul 9, 2021

    @ShadowJonathan
    ContributorAuthor

    Updated description, the previous one was vague and also implied setting type annotations for every variable inside non-init functions, which i think is overdoing it a little bit, type-annotating all other places noted should already correctly resolve those spots.

  8. ShadowJonathan commented on Jul 15, 2021

    @ShadowJonathan
    ContributorAuthor

    I found some interesting mypy optional errors that could be enabled to enforce type annotations;

    https://mypy.readthedocs.io/en/stable/error_code_list2.html

    • Check that type arguments exist [type-arg]
      (Errors on List, requires List[<type>])

    • Check that every function has an annotation [no-untyped-def]

    • Check that comparisons are overlapping [comparison-overlap]

      def is_magic(x: bytes) -> bool:
        # Error: Non-overlapping equality check 
        #        (left operand type: "bytes", right operand type: "str")  [comparison-overlap]
        return x == 'magic'
    • (maybe) Check that no untyped functions are called [no-untyped-call]

    • Check that function does not return Any value [no-any-return]

  9. clokep commented on Aug 15, 2022

    @clokep
    Member

    I think this is essentially a duplicate of #11271.

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

    T-TaskRefactoring, removal, replacement, enabling or disabling functionality, other engineering tasks.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions