pyproject.toml: Make ruff aware of MicroPython builtins. - #1156
Open
agatti wants to merge 1 commit into
Open
Conversation
This commit updates the project-wide configuration for `ruff`, in order to not let it flag usages of MicroPython-specific types and keywords when performing code checks. The tool currently has an exceptions list that covers almost all Python code that ships with MicroPython, however code placed in `ports/<port>/modules` (or elsewhere) by users for customisation purposes will end up raising errors for otherwise valid MicroPython-flavoured code. Changes only let `ruff` assume that it knows about the existence of the `micropython` module (used without an initial import) for decorators (eg. `native`, `viper`, `asm_...`, etc.), Viper pointer types (`ptr`, `ptr8`, `ptr16`, `ptr32`), the unsigned integer return type (`uint`), the `const` constructor, and the `execfile` builtin. For code that is placed elsewhere, these rules won't apply, so it might be an option to add further configuration instructions in the "getting started" documentation files. Signed-off-by: Alessandro Gatti <a.gatti@frob.it>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: This is the
micropython-libcounterpart of micropython/micropython#19501. I forgot to submit this at the same time as the other PR, but GitHub doesn't make cross-repo PRs that easy to submit... :(I've copied the PR's description here for completeness.
Summary
This PR updates the project-wide configuration for
ruff, in order to not let it flag usages of MicroPython-specific types and keywords when performing code checks.The tool currently has an exceptions list that covers almost all Python code that ships with MicroPython, however code placed in
ports/<port>/modules(or elsewhere in the source tree where they're not covered by the exceptions list) by users for customisation purposes will end up raising errors for otherwise valid MicroPython-flavoured code.Changes only let
ruffassume that it knows about the existence of themicropythonmodule (used without an initial import) for decorators (eg.native,viper,asm_..., etc.), Viper pointer types (ptr,ptr8,ptr16,ptr32), the unsigned integer return type (uint), theconstconstructor, and theexecfilebuiltin.For code that is placed elsewhere, these rules won't apply, so it might be an option to add further configuration instructions in the "getting started" documentation files.
Testing
Using this simple Python script:
Without these changes, running
ruff check <file>would yield this:With these changes, instead:
Trade-offs and Alternatives
This may potentially cause some false-negatives, like for example, allowing things like
ptrtypes in non@micropyhon.viper-decorated functions.ruffdoes not currently have any facility for plugins, otherwise it could be possible to traverse the AST being checked to easily see when MicroPython-specific constructs are safe to use.Generative AI
I did not use generative AI tools when creating this PR.