Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Python type hints #690

Closed
osma opened this issue Apr 14, 2023 · 3 comments · Fixed by #708
Closed

Use Python type hints #690

osma opened this issue Apr 14, 2023 · 3 comments · Fixed by #708
Assignees
Milestone

Comments

@osma
Copy link
Member

osma commented Apr 14, 2023

Since Python 3.5 it has been possible to add type hints for variables and function/method signatures (PEP 484).

We should do this within the Annif codebase as well. Here is a good basic tutorial about the available types and how to use them. Quoting from the post:

Type hints bring an additional layer of abstraction on top of your code: they help to document it, clarify the assumptions about inputs/outputs and prevent insidious and silent mistakes when static code analysis (👋 mypy) is performed on top.

Eventually we should also enforce the type hints with mypy, but let's leave that out of scope for this issue so that it can be closed once we have the hints in place at least for a reasonable proportion of the codebase.

@osma osma added this to the 1.0 milestone Apr 14, 2023
@juhoinkinen
Copy link
Member

A few tools have been mentioned that can help by (semi)automatically generating the type annotations:

Sure, there are some projects out there that can help out.

Instagram developed the MonkeyType tool to pick up runtime type information. These are output as stubs files, but you can use the tools included to apply those to your source files as inline annotations, or use the retype project to do the same.
Dropbox has a similar tool called pyannotate; this tool can generate either Python 2 type hint comments or annotations.

Neither project will produce perfect type hints; always take their output as a starting point, not the authoritative final annotations.

@juhoinkinen
Copy link
Member

There is also a pytest-monkeytype plugin that can be used with monkeytype for recording the variable types.

@juhoinkinen
Copy link
Member

As instructed, I run py.test --monkeytype-output=./monkeytype.sqlite3 to trace and record the variable types to/from functions based on pytest run. This data covers also the unit tests.

Then I run monkeytype apply <module> --pep_563 to all modules (not unit tests) to insert the type annotations to function signatures. The --pep_563 option makes monkeytype to insert also from __future__ import annotations to altered files (needed in Python <3.11) and to wrap imports needed for signatures in if TYPE_CHECKING: conditional blocks.

PEP563 is for solving two problems:

  • forward references: when a type hint contains names that have not been defined yet, that definition needs to be expressed as a string literal;
  • type hints are executed at module import time, which is not computationally free.

These changes by monkeytype can be used as a starting point for manual editing.

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

Successfully merging a pull request may close this issue.

2 participants