Skip to content
Myr

contributing.md

This file

Path

docs/contributing.md

305 lines · 7.7 KiB · Markdown

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 4c21293 point at it.

📝 Update release notesgithub-actions[bot]before adoption4c21293

lines 1–305why, one step back
1# Development - Contributing
23First, you might want to see the basic ways to [help Typer and get help](help-typer.md){.internal-link target=_blank}.45## Developing67If you already cloned the <a href="https://github.com/fastapi/typer" class="external-link" target="_blank">typer repository</a> and you want to deep dive in the code, here are some guidelines to set up your environment.89### Virtual Environment1011Follow the instructions to create and activate a [virtual environment](virtual-environments.md){.internal-link target=_blank} for the internal code of `typer`.1213### Install Requirements Using `pip`1415After activating the environment, install the required packages:1617<div class="termy">1819```console20$ pip install -r requirements.txt2122---> 100%23```2425</div>2627It will install all the dependencies and your local Typer in your local environment.2829### Using your Local Typer3031If you create a Python file that imports and uses Typer, and run it with the Python from your local environment, it will use your cloned local Typer source code.3233And if you update that local Typer source code when you run that Python file again, it will use the fresh version of Typer you just edited.3435That way, you don't have to "install" your local version to be able to test every change.3637/// note | "Technical Details"3839This only happens when you install using this included `requirements.txt` instead of running `pip install typer` directly.4041That is because inside the `requirements.txt` file, the local version of Typer is marked to be installed in "editable" mode, with the `-e` option.4243///4445### Format4647There is a script that you can run that will format and clean all your code:4849<div class="termy">5051```console52$ bash scripts/format.sh53```5455</div>5657It will also auto-sort all your imports.5859## Tests6061There is a script that you can run locally to test all the code and generate coverage reports in HTML:6263<div class="termy">6465```console66$ bash scripts/test-cov-html.sh67```6869</div>7071This command generates a directory `./htmlcov/`, if you open the file `./htmlcov/index.html` in your browser, you can explore interactively the regions of code that are covered by the tests, and notice if there is any region missing.7273## Completion7475To try and test the completion for different shells and check that they are working you can use a Docker container.7677There's a `Dockerfile` and a Docker Compose file `compose.yaml` at `./scripts/docker/`.7879It has installed `bash`, `zsh`, `fish`, and `pwsh` (PowerShell for Linux).8081It also has installed `nano` and `vim`, so that you can check the modified configuration files for the shells (for example `.bashrc`, `.zshrc`, etc).8283It also has `uv` installed, so you can install the dependencies and the project quickly.8485The Docker Compose file mounts the main directory as `/code` inside the container, so you can change things and try them out.8687Go to the `./scripts/docker/` directory:8889```console90$ cd scripts/docker/91```9293Then run an interactive session with `bash` inside the container:9495```console96$ docker compose run typer bash9798root@79c4b9b70cbe:/code#99```100101Then inside the container, you can install `typer` with:102103```console104$ uv pip install -r requirements.txt105```106107Then, you can start the shell you want to use, the one where you want to try out completion:108109* `bash`110* `fish`111* `pwsh`112* `zsh`113114For example:115116```console117$ zsh118```119120Then install `typer` completion:121122```console123$ typer --install-completion124```125126/// info127128In `pwsh` you will probably get a warning of:129130```plaintext131Set-ExecutionPolicy: Operation is not supported on this platform.132```133134this is because that configuration is only available in Windows (and needed there), not in PowerShell for Linux.135136///137138For completion to take effect, you need to restart the shell. So, exit the current shell:139140```console141$ exit142```143144and start a new shell (for the same shell you installed completion in) again. For example:145146```console147$ zsh148```149150Now you could create a demo file on the same Typer directory in your editor, for example `demo.py`:151152```python153import typer154155app = typer.Typer()156157158@app.command()159def hello():160 print("Hello")161162163@app.command()164def goodbye():165 print("Goodbye")166167168if __name__ == "__main__":169 app()170```171172Because the directory is mounted as a volume, you will be able to access the file from inside the container.173174So, you can try running it with the `typer` command, that will use the installed shell completion:175176```console177$ typer demo.py <TAB>178```179180And you should see the completion working:181182```console183run -- Run the provided Typer app.184utils -- Extra utility commands for Typer apps.185```186187And the same for the commands in your `demo.py` file:188189```console190$ typer demo.py run <TAB>191192hello goodbye193```194195You can also check the configuration file using `nano` or `vim`, for example:196197```bash198nano ~/.zshrc199```200201It will show some content like:202203```bash204fpath+=~/.zfunc; autoload -Uz compinit; compinit205206207zstyle ':completion:*' menu select208```209210If you exit from the container, you can start a new one, you will probably have to install the packages again and install completion again.211212Using this process, you can test all the shells, with their completions, being able to start from scratch quickly in a fresh container, and verifying that everything works as expected.213214## Docs215216First, make sure you set up your environment as described above, that will install all the requirements.217218### Docs live219220During local development, there is a script that builds the site and checks for any changes, live-reloading:221222<div class="termy">223224```console225$ python ./scripts/docs.py live226227<span style="color: green;">[INFO]</span> Serving on http://127.0.0.1:8008228<span style="color: green;">[INFO]</span> Start watching changes229<span style="color: green;">[INFO]</span> Start detecting changes230```231232</div>233234It will serve the documentation on `http://127.0.0.1:8008`.235236That way, you can edit the documentation/source files and see the changes live.237238/// tip239240Alternatively, you can perform the same steps that script does manually.241242Go into the docs directory at `docs/`:243244```console245$ cd docs/246```247248Then run `mkdocs` in that directory:249250```console251$ mkdocs serve --dev-addr 8008252```253254///255256#### Typer CLI (optional)257258The instructions here show you how to use the script at `./scripts/docs.py` with the `python` program directly.259260But you can also use <a href="https://typer.tiangolo.com/typer-cli/" class="external-link" target="_blank">Typer CLI</a>, and you will get autocompletion in your terminal for the commands after installing completion.261262If you install Typer CLI, you can install completion with:263264<div class="termy">265266```console267$ typer --install-completion268269zsh completion installed in /home/user/.bashrc.270Completion will take effect once you restart the terminal.271```272273</div>274275### Docs Structure276277The documentation uses <a href="https://www.mkdocs.org/" class="external-link" target="_blank">MkDocs</a>.278279And there are extra tools/scripts in place in `./scripts/docs.py`.280281/// tip282283You don't need to see the code in `./scripts/docs.py`, you just use it in the command line.284285///286287All the documentation is in Markdown format in the directory `./docs`.288289Many of the tutorials have blocks of code.290291In most of the cases, these blocks of code are actual complete applications that can be run as is.292293In fact, those blocks of code are not written inside the Markdown, they are Python files in the `./docs_src/` directory.294295And those Python files are included/injected in the documentation when generating the site.296297### Docs for Tests298299Most of the tests actually run against the example source files in the documentation.300301This helps to make sure that:302303* The documentation is up-to-date.304* The documentation examples can be run as is.305* Most of the features are covered by the documentation, ensured by test coverage.