anki/docs/contributing.md

131 lines
4.8 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
2021-06-03 08:48:20 +02:00
and add-ons, please see https://docs.ankiweb.net/contrib
2020-01-02 10:55:27 +01:00
2021-02-03 05:54:12 +01:00
With most users now on 2.1, the past year has been focused on paying down some
of the technical debt that Anki's codebase has built up over the years, and making
changes that will make future maintenance and refactoring easier. A lot of Anki's
"business logic" has been migrated to Rust, which AnkiMobile and AnkiDroid
can also take advantage of - previously a lot of effort was wasted writing the same
code for each platform, and then debugging differences in the implementations.
Considerable effort has also been put into improving the Python side of things,
with type hints added to the majority of the codebase, automatic linting/formatting,
and refactoring of parts of the code.
The scheduling code and import/export code remains to be done, and this will likely
take a number of months to work through. Until that is complete, new features
will not be 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
2021-02-03 05:54:12 +01:00
to wait until the refactoring process is done.
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
2021-02-03 05:54:12 +01:00
at the [issues tab](https://github.com/ankitects/anki/issues) of the Anki repo
on GitHub.
2020-05-25 07:43:26 +02:00
## Type hints
2020-01-02 10:55:27 +01:00
2021-02-03 05:54:12 +01:00
Most of Anki's Python code now has type hints, which improve code completion,
and make it easier to discover errors during development. When adding new
code, please make sure you add type hints as well, or the tests will fail.
2020-01-15 05:26:50 +01:00
2021-02-03 05:54:12 +01:00
Qt's stubs are not perfect, so you may sometimes need to use cast(), or silence
a type error. When connecting signals, there's a qconnect() helper in aqt.utils
that can be used to work around the type warnings without obscuring other errors
such as a mistyped variable.
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
2021-02-03 05:54:12 +01:00
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.
2021-09-30 02:59:57 +02:00
Using "did change" instead of the past tense "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
2021-06-03 08:48:20 +02: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
2021-02-03 05:54:12 +01:00
You may want to explicitly set PATH to your normal shell PATH in that script,
as pre-commit does not use a login shell, and if your path differs Bazel will
end up recompiling things unnecessarily.
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
2021-02-03 05:54:12 +01:00
Please use standard Python snake_case variable names and functions in newly
introduced code. Because add-ons often rely on existing function names, if
renaming an existing function, please add a legacy alias to the old function.
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.