anki/docs/contributing.md

146 lines
5.0 KiB
Markdown
Raw Normal View History

# Contributing Code
2020-01-02 10:55:27 +01:00
For info on contributing things other than code, such as translations, decks
2020-12-06 21:45:57 +01:00
and add-ons, please see https://docs.ankiweb.net/#/contrib
2020-01-02 10:55:27 +01:00
2020-01-07 12:45:44 +01:00
With most users now on 2.1, it's time to start paying down some of the
technical debt that Anki's codebase has built up over the years. This is
not an easy task - the code is tightly coupled together, not fully covered
by unit tests, and mostly dynamically typed, meaning even small changes
carry the risk of regressions.
2020-05-11 06:30:56 +02:00
At the moment, the focus is on changes that will make future maintenance and
refactoring easier - migrating parts of the codebase to Rust, improving tooling
and linting, type hints in the Python code, and more unit tests.
2020-01-07 12:45:44 +01:00
2020-05-11 06:30:56 +02:00
New features are not currently the top priority, unless they are easy wins as
part of the refactoring process.
2020-01-07 12:45:44 +01:00
2020-05-11 06:30:56 +02:00
If you are planning to contribute any non-trivial changes, please reach out
2020-07-16 03:15:12 +02:00
on the support site before you begin work. Some areas (primarily pylib/) are
2020-05-11 06:30:56 +02:00
likely to change/conflict with other work, and larger changes will likely need
to wait until the refactoring process nears completion.
2020-01-02 10:55:27 +01:00
## Help wanted
2020-05-25 07:43:26 +02:00
If you'd like to contribute but don't know what to work on, please take a look
at the issues on the following repo. It's quite bare at the moment, but will
hopefully grow with time.
https://github.com/ankitects/help-wanted
## Type hints
2020-01-02 10:55:27 +01:00
2020-05-11 06:30:56 +02:00
Type hints have recently been added to parts of the Python codebase, mainly
using automated tools. At the moment, large parts of the codebase are still
missing type hints, and some of the hints that do exist are incorrect or too
general.
2020-12-09 10:59:06 +01:00
When running 'bazel test', Anki uses mypy to typecheck the code. Mypy is able to
infer some types by itself, but adding more type signatures to the code
increases the amount of code that mypy is able to analyze.
2020-01-15 05:26:50 +01:00
Patches that improve the type hints would be appreciated. And if you're
adding new functionality, please use type hints in the new code you write
where practical.
Parts of Anki's codebase use ad-hoc data structures like nested dictionaries
and lists, and they can be difficult to fully type. Don't worry too much about
getting the types perfect - even a partial type like Dict[str, Any] or
List[Tuple] is an improvement over no types at all.
2020-01-02 10:55:27 +01:00
2020-11-04 05:01:14 +01:00
Qt's stubs are not perfect, so you'll find when doing things like connecting
signals, you may have to add the following to the end of a line to silence the
spurious errors.
2020-01-02 10:55:27 +01:00
```
2020-01-02 10:55:27 +01:00
# type: ignore
```
2020-01-02 10:55:27 +01:00
2020-02-11 23:01:33 +01:00
In cases where you have two modules that reference each other, you can fix the
import cycle by using fully qualified names in the types, and enabling
annotations. For example, instead of
```
from aqt.browser import Browser
2020-02-11 23:01:33 +01:00
def myfunc(b: Browser) -> None:
pass
```
2020-02-11 23:01:33 +01:00
use the following instead:
```
from __future__ import annotations
2020-02-11 23:01:33 +01:00
import aqt
2020-02-11 23:01:33 +01:00
def myfunc(b: aqt.browser.Browser) -> None:
pass
```
2020-01-02 10:55:27 +01:00
## Hooks
2020-01-15 05:26:50 +01:00
If you're writing an add-on and would like to extend a function that doesn't
currently have a hook, a pull request that adds the required hooks would be
welcome. If you could mention your use case in the pull request, that would be
appreciated.
The hooks try to follow one of two formats:
2020-01-15 05:26:50 +01:00
[subject] [verb] - eg, note_type_added, card_will_render
2020-01-15 05:26:50 +01:00
[module] [verb] [subject] - eg, browser_did_change_row, editor_did_update_tags
The qt code tends to use the second form as the hooks tend to focus on
particular screens. The pylib code tends to use the first form, as the focus
is usually subjects like cards, notes, etc.
Using "did change" instead of the past test "changed" can seem awkward, but
makes it consistent with "will", and is similar to the naming style used in
iOS's libraries.
2020-01-15 05:26:50 +01:00
2020-05-25 07:43:26 +02:00
In most cases, hooks are better added in the GUI code than in pylib.
2020-01-15 05:26:50 +01:00
The hook code is automatically generated using the definitions in
pylib/tools/genhooks.py and qt/tools/genhooks_gui.py. Adding a new definition
2020-11-04 05:01:14 +01:00
in one of those files will update the generated files.
2020-01-15 05:26:50 +01:00
## Translations
For information on adding new translatable strings to Anki, please see
2020-12-09 10:59:06 +01:00
https://translating.ankiweb.net/#/anki/developers
## Tests Must Pass
2020-01-02 10:55:27 +01:00
2020-11-04 05:01:14 +01:00
Please make sure 'bazel test //...' completes successfully before submitting code.
2020-01-07 12:45:44 +01:00
You can do this automatically by adding the following into
.git/hooks/pre-commit or .git/hooks/pre-push and making it executable.
2020-01-02 10:55:27 +01:00
```sh
2020-01-02 10:55:27 +01:00
#!/bin/bash
2020-11-04 05:01:14 +01:00
bazel test //...
```
2020-01-02 10:55:27 +01:00
2020-12-09 10:59:06 +01:00
If your change is non-trivial and not covered by the existing unit tests, please
2020-01-02 10:55:27 +01:00
consider adding a unit test at the same time.
## Code Style
2020-01-02 10:55:27 +01:00
You are welcome to use snake_case variable names and functions in newly
2020-01-07 12:45:44 +01:00
introduced code, but please avoid renaming existing functions and global
variables that use camelCaps. Variables local to a function are safer to
rename, but please do so only when a function needs to be changed for other
reasons as well.
2020-01-02 10:55:27 +01:00
## Do One Thing
2020-01-02 10:55:27 +01:00
A patch or pull request should be the minimum necessary to address one issue.
Please don't make a pull request for a bunch of unrelated changes, as they are
difficult to review and will be rejected - split them up into separate
requests instead.
## License
2020-01-02 10:55:27 +01:00
2020-01-07 12:45:44 +01:00
Please add yourself to the CONTRIBUTORS file in your first pull request.