Repository metrics
- Stars
- (3,775 stars)
- PR merge metrics
- (PR metrics pending)
Description
We would like to type check all files in the package. Adding types helps to document the code and makes it easier to use.
How to work on this issue
- Go to pyproject.toml to the
excludesection of the mypy settings. There should be a list of files like this:
exclude = '''(?x)(
lightly/cli/version_cli.py |
lightly/cli/crop_cli.py |
lightly/cli/serve_cli.py |
lightly/cli/embed_cli.py |
lightly/cli/lightly_cli.py |
...
- Pick a file you would like to type. Best are files from
lightly/loss,lightly/data,lightly/utils,lightly/models/modules, and the corresponding test files. Do not type check deprecated model files inlightly/modelslikelightly/models/barlow_twins.py. - Run
mypy <filename>(make sure that you have installed lightly following the contribution guide). This should show a list with all the missing types/errors. - Add types until no more errors are shown. This is a good example on how a typed file should look like: https://github.com/lightly-ai/lightly/blob/master/lightly/models/modules/memory_bank.py
- Go to
pyproject.tomland remove the filename of the file you just typed from the mypyexcludelist. - If the file was the last file in a subdirectory that was missing types, then also remove the subdirectory name from the skip import list in pyprojec.toml
- Create a new PR named
Add types for <filename>and push your changes. Make sure all Github actions pass.
Important
Lightly still supports old Python versions (including 3.7, 3.8, and 3.9) that do not work with new typing features introduced in Python 3.10. To use the new Python 3.10 syntax you have to add from __future__ import annotations at the top of the file. The following typing features changed in 3.10:
- Union types can now be written as
str | intinstead ofUnion[str, int] - Optional types can now be written as
str | Noneinstead ofOptional[str] - Tuples, lists, and dicts can now be typed as
tuple[str, int],list[str],dict[str, int]instead ofTuple[str, int],List[str],Dict[str, int].
Please use from __future__ import annotations with the new types whenever possible. This will save a lot of refactoring in the future. Also feel free to update files with old types to the new syntax.