|
| 1 | +## Contributor's Guidelines & Code Conventions |
| 2 | + |
| 3 | +micropython-lib follows the same general conventions as the [main MicroPython |
| 4 | +repository](https://github.com/micropython/micropython). Please see |
| 5 | +[micropython/CONTRIBUTING.md](https://github.com/micropython/micropython/blob/master/CONTRIBUTING.md) |
| 6 | +and [micropython/CODECONVENTIONS.md](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md). |
| 7 | + |
| 8 | +### Raising issues |
| 9 | + |
| 10 | +Please include enough information for someone to reproduce the issue you are |
| 11 | +describing. This will typically include: |
| 12 | + |
| 13 | +* The version of MicroPython you are using (e.g. the firmware filename, git |
| 14 | + hash, or version info printed by the startup message). |
| 15 | +* What board/device you are running MicroPython on. |
| 16 | +* Which package you have installed, how you installed it, and what version. |
| 17 | + When installed via `mip`, all packages will have a `__version__` |
| 18 | + attribute. |
| 19 | +* A simple code snippet that demonstrates the issue. |
| 20 | + |
| 21 | +If you have a how-to question or are looking for help with using MicroPython |
| 22 | +or packages from micropython-lib, please post at the |
| 23 | +[discussion forum](https://github.com/orgs/micropython/discussions) instead. |
| 24 | + |
| 25 | +### Pull requests |
| 26 | + |
| 27 | +The same rules for commit messages, signing-off commits, and commit structure |
| 28 | +apply [as for the main MicroPython repository](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md). |
| 29 | + |
| 30 | +All Python code is formatted using the [black](https://github.com/psf/black) |
| 31 | +tool. You can run [`tools/codeformat.py`](tools/codeformat.py) to apply |
| 32 | +`black` automatically before submitting a PR. The GitHub CI will also run the |
| 33 | +[ruff](https://github.com/astral-sh/ruff) tool to apply further "linting" |
| 34 | +checks. |
| 35 | + |
| 36 | +Similar to the main repository, a configuration is provided for the |
| 37 | +[pre-commit](https://pre-commit.com/) tool to apply `black` code formatting |
| 38 | +rules and run `ruff` automatically. See the documentation for using pre-commit |
| 39 | +in [the code conventions document](https://github.com/micropython/micropython/blob/master/CODECONVENTIONS.md#automatic-pre-commit-hooks) |
| 40 | + |
| 41 | +In addition to the conventions from the main repository, there are some |
| 42 | +specific conventions and guidelines for micropython-lib: |
| 43 | + |
| 44 | +* The first line of the commit message should start with the name of the |
| 45 | + package, followed by a short description of the commit. Package names are |
| 46 | + globally unique in the micropython-lib directory structure. |
| 47 | + |
| 48 | + For example: `shutil: Add disk_usage function.` |
| 49 | + |
| 50 | +* Although we encourage keeping the code short and minimal, please still use |
| 51 | + comments in your code. Typically, packages will be installed via |
| 52 | + `mip` and so they will be compiled to bytecode where comments will |
| 53 | + _not_ contribute to the installed size. |
| 54 | + |
| 55 | +* All packages must include a `manifest.py`, including a `metadata()` line |
| 56 | + with at least a description and a version. |
| 57 | + |
| 58 | +* Prefer to break larger packages up into smaller chunks, so that just the |
| 59 | + required functionality can be installed. The way to do this is to have a |
| 60 | + base package, e.g. `mypackage` containing `mypackage/__init__.py`, and then |
| 61 | + an "extension" package, e.g. `mypackage-ext` containing additional files |
| 62 | + e.g. `mypackage/ext.py`. See |
| 63 | + [`collections-defaultdict`](python-stdlib/collections-defaultdict) as an |
| 64 | + example. |
| 65 | + |
| 66 | +* If you think a package might be extended in this way in the future, prefer |
| 67 | + to create a package directory with `package/__init__.py`, rather than a |
| 68 | + single `module.py`. |
| 69 | + |
| 70 | +* Packages in the python-stdlib directory should be CPython compatible and |
| 71 | + implement a subset of the CPython equivalent. Avoid adding |
| 72 | + MicroPython-specific extensions. Please include a link to the corresponding |
| 73 | + CPython docs in the PR. |
| 74 | + |
| 75 | +* Include tests (ideally using the `unittest` package) as `test_*.py`. |
| 76 | + Otherwise, provide examples as `example_*.py`. When porting CPython |
| 77 | + packages, prefer to use the existing tests rather than writing new ones |
| 78 | + from scratch. |
| 79 | + |
| 80 | +* When porting an existing third-party package, please ensure that the source |
| 81 | + license is compatible. |
| 82 | + |
| 83 | +* To make it easier for others to install packages directly from your PR before |
| 84 | + it is merged, consider opting-in to automatic package publishing (see |
| 85 | + [Publishing packages from forks](#publishing-packages-from-forks)). If you do |
| 86 | + this, consider quoting the [commands to install |
| 87 | + packages](README.md#installing-packages-from-forks) in your Pull Request |
| 88 | + description. |
| 89 | + |
| 90 | +### Publishing packages from forks |
| 91 | + |
| 92 | +You can easily publish the packages from your micropython-lib |
| 93 | +[fork](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/working-with-forks/about-forks) |
| 94 | +by opting in to a system based on [GitHub |
| 95 | +Actions](https://docs.github.com/en/actions) and [GitHub |
| 96 | +Pages](https://docs.github.com/en/pages): |
| 97 | + |
| 98 | +1. Open your fork's repository in the GitHub web interface. |
| 99 | +2. Navigate to "Settings" -> "Secrets and variables" -> "Actions" -> "Variables". |
| 100 | +3. Click "New repository variable" |
| 101 | +4. Create a variable named `MICROPY_PUBLISH_MIP_INDEX` with value `true` (or any |
| 102 | + "truthy" value). |
| 103 | +5. The settings for GitHub Actions and GitHub Pages features should not need to |
| 104 | + be changed from the repository defaults, unless you've explicitly disabled |
| 105 | + Actions or Pages in your fork. |
| 106 | + |
| 107 | +The next time you push commits to a branch in your fork, GitHub Actions will run |
| 108 | +an additional step in the "Build All Packages" workflow named "Publish Packages |
| 109 | +for branch". This step runs in *your fork*, but if you open a pull request then |
| 110 | +this workflow is not shown in the Pull Request's "Checks". These run in the |
| 111 | +upstream repository. Navigate to your fork's Actions tab in order to see |
| 112 | +the additional "Publish Packages for branch" step. |
| 113 | + |
| 114 | +Anyone can then install these packages as described under [Installing packages |
| 115 | +from forks](README.md#installing-packages-from-forks). |
| 116 | + |
| 117 | +The exact command is also quoted in the GitHub Actions log in your fork's |
| 118 | +Actions for the "Publish Packages for branch" step of "Build All Packages". |
| 119 | + |
| 120 | +#### Opting Back Out |
| 121 | + |
| 122 | +To opt-out again, delete the `MICROPY_PUBLISH_MIP_INDEX` variable and |
| 123 | +(optionally) delete the `gh-pages` branch from your fork. |
| 124 | + |
| 125 | +*Note*: While enabled, all micropython-lib packages will be published each time |
| 126 | +a change is pushed to any branch in your fork. A commit is added to the |
| 127 | +`gh-pages` branch each time. In a busy repository, the `gh-pages` branch may |
| 128 | +become quite large. The actual `.git` directory size on disk should still be |
| 129 | +quite small, as most of the content will be duplicated. If you're worried that |
| 130 | +the `gh-pages` branch has become too large then you can always delete this |
| 131 | +branch from GitHub. GitHub Actions will create a new `gh-pages` branch the next |
| 132 | +time you push a change. |
0 commit comments