Lintkit
Official lintkit
API documentation.
General¶
lintkit
is a Python library allowing you to quickly create custom linters, while being flexible enough to be used in a complex settings.
Warning
Start with tutorials to get a feel of the framework.
Core modules¶
When creating custom linter(s) you will be (likely) interested in these core modules:
lintkit.settings
- global settings (e.g. name, hownoqa
s should be named etc.)lintkit.rule
- core class for creating linting ruleslintkit.loader
- file loaders mixins (e.g.python
orYAML
), tailoring rules to datalintkit.check
- what ischeck
ed by a rule
and the following functionalities from lintkit
:
Value
- define rule output in a reusable mannerrun
- run all (or subset of) rules on a given set of files
Tip
Roam around the docs to get a better feel of what's available.
lintkit.Value ¶
Value(value=None, start_line=None, start_column=None, end_line=None, end_column=None, comment=None, **kwargs)
Bases: ObjectProxy
, Generic[T]
Value
used by rules for verification.
Note
Instance of this type should always be returned from lintkit.rule.Rule.values
Tip
You should use objects of this class just like you would use the value
directly. as it is a "perfect proxy". Its other functionalities are used internally (e.g. Pointer
)
Can be essentially anything (e.g. dict
from parsed JSON
, string
value from that dict
or some other rule created value).
It is later used by the pipeline to verify whether this value complies with the Rule
itself.
Tip
Use creation static methods ( lintkit.Value.from_python
, lintkit.Value.from_toml
, or lintkit.Value.from_json
) when returning values from rules inheriting from lintkit.loader.Python
, lintkit.loader.TOML
or lintkit.loader.JSON
respectively.
Caution
YAML
is already wrapped by lintkit.Value
during when using lintkit.loader.YAML
, no need to process them within values
function.
Note
This class
acts as a "perfect proxy" for end users by utilising wrapt
⧉ (which means wrapped value
should be usable just like the original one).
Attributes:
Name | Type | Description |
---|---|---|
value | Any | Value to check against the rules. |
comment | str | None | Source code comment related to the object, if any. Used internally |
start_line | Pointer | Line number (represented as a |
start_column | Pointer | Column number (represented as a |
end_line | Pointer | End line number (represented as a |
end_column | Pointer | End column number (represented as a |
Source code in src/lintkit/_value.py
lintkit.Value.from_python staticmethod
¶
Create a Value
from Python's ast.AST
node.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | T | Some | required |
node | AST | Python's | required |
Returns:
Type | Description |
---|---|
Value[T] | Provided value with its respective Python node. |
Source code in src/lintkit/_value.py
lintkit.Value.from_json staticmethod
¶
Create a Value
from JSON
values.
Note
As JSON
does not support comments, only value
is necessary.
Warning
Due to no comments, all ignore lines are currently ignored and only file exclusions are available.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value | T | Some object, usually plain | required |
Returns:
Type | Description |
---|---|
Value[T] |
|
Source code in src/lintkit/_value.py
lintkit.Value.from_toml staticmethod
¶
Create a Value
from tomlkit
Item
.
Warning
Multiline ignore
s or skips are not supported for TOML
due to the lack of line numbers.
Warning
Value
will contain no line/column info (as it is unavailable in tomlkit
⧉), but propagates comment
field to other elements of the system which allows it to be used for line ignoring.
Returns:
Type | Description |
---|---|
Value[Any] |
|
Source code in src/lintkit/_value.py
lintkit.run ¶
Run all the rules on a given file.
Caution
This function has two modes; one returns bool
indicating whether any rule raised any error (the default), the second one returns all rules and their error codes via an iterator
.
Tip
Use output=False
if you create a custom linter and only want to return the appropriate exit code (most common usage)
An example of minimal linter:
Example
An example of iteration:
Example
Tip
output=True
(iteration mode) allows to gather general statistics from each rule and adjust the output to your liking.
Warning
exclude_codes
takes precedence over include_codes
!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
files | Path | str | Files to lint. | () |
include_codes | Iterable[int] | None | A set of rule codes to include. If | None |
exclude_codes | Iterable[int] | None | A set of rule codes to ignore. If | None |
end_mode | Literal['first', 'all'] | Whether to stop after the first error or run all rules. By default runs all rules. | 'all' |
output | bool | If | False |
Returns:
Type | Description |
---|---|
Iterator[tuple[bool, Rule]] | bool | An iterator over all rules and their outputs OR a boolean indicating whether any rule raised an error. |