Skip to content

Commit

Permalink
Update contribution guidelines around type annotations (#1063)
Browse files Browse the repository at this point in the history
Since #1061 has merged rustworkx is now fully typed with mypy. This
means that adding type annotations is no longer optional and is now
required for all new function. This commit updates the contribution
guidelines to indicate type annotations are required now. The details
are also updated a bit to provide a bit more detail on what's required
when adding type annotations to rustworkx.

Co-authored-by: Ivan Carvalho <[email protected]>
  • Loading branch information
mtreinish and IvanIsCoding authored Jan 21, 2024
1 parent 87cb008 commit ddb0cda
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,41 @@ cargo doc --open
### Type Annotations
If you have added new methods or changed method signatures, we encourage you to add annotations for
those methods in stub files. The stub files are in the `rustworkx` directory and have a `.pyi` file extension.
They contain annotated signatures for Python functions, stripped of their implementation.
While this step is optional, it is very helpful for end-users. Adding annotations lets users type check
their code with [mypy](http://mypy-lang.org/), which can be helpful for finding bugs.
If you have added new methods, functions, or classes, and/or changed any
signatures, type anotations for Python are required to be included in a pull
request. Type annotations are added using type
[stub files](https://typing.readthedocs.io/en/latest/source/stubs.html) which
provide type annotations to python tooling which use type annotations. The stub
files are in the `rustworkx/` directory and have a `.pyi` file extension. They
contain annotated signatures for Python functions, stripped of their
implementation. You can find more details on typing in Python at:
* https://mypy.readthedocs.io/en/stable/
* https://typing.readthedocs.io/en/latest/
* https://docs.python.org/3/library/typing.html
Having type annotations is very helpful for Python end-users. Adding
annotations lets users type check their code with [mypy](http://mypy-lang.org/),
which can be helpful for finding bugs when using rustworkx.
Just like with tests for the code, annotations are also tested via tox.
```
tox -estubs
```
One important thing to note is that if you're adding a new function to the Rust
module you will need to ensure that the signature with annotations is added to
`rustworkx/rustworkx.pyi`. Then it is also necessary to re-export the annotation
by adding an import line to `rustworkx/__init__.pyi` in the form:
```python
from .rustworkx import foo as foo
```
which ensures that mypy is able to find the type annotations when users import
from the root `rustworkx` package (which is the most common access pattern).
### Release Notes
It is important to document any end user facing changes when we release a new
Expand Down

0 comments on commit ddb0cda

Please sign in to comment.