You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/4. Validating/4.1. Linting.md
+43-28Lines changed: 43 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,40 +1,47 @@
1
1
---
2
-
description: Master the art of code linting with tools like Ruff to ensure code quality and maintainability. Discover how linting helps enforce coding conventions, enhances readability, and identifies potential bugs early in the development process.
2
+
description: Learn how to use linting tools like Ruff to improve code quality, enforce standards, and catch errors early. This guide covers configuration, best practices, and automation for maintaining a clean and reliable codebase.
3
3
---
4
4
5
5
# 4.1. Linting
6
6
7
7
## What is software linting?
8
8
9
-
[Linting](https://en.wikipedia.org/wiki/Lint_(software))involves utilizing a tool to analyze your code for errors and discrepancies against standard coding conventions. Its primary aim is to identify syntax and stylistic issues, along with other potential programming errors that may have been accidentally included.
9
+
[Linting](https://en.wikipedia.org/wiki/Lint_(software))is the process of using a static code analysis tool—a "linter"—to check source code for programmatic errors, bugs, stylistic inconsistencies, and other potential issues. It acts as an automated code reviewer, flagging problems without executing the program.
10
10
11
-
## Why should you use linter tools?
11
+
## Why are linters essential for development?
12
12
13
-
Linters are indispensable in Python development due to several key reasons:
13
+
Integrating linters into a development workflow provides significant benefits:
14
14
15
-
-**Code Quality**: They ensure adherence to best coding practices, crucial for teamwork.
16
-
-**Readability**: By enforcing a consistent style, they enhance the readability of the code.
17
-
-**Early Bug Detection**: They spot potential bugs early, reducing debugging and testing time.
18
-
-**Learning and Improvement**: They are great for developers, particularly novices, to learn and adhere to best practices, thus refining their coding skills.
15
+
-**Enforce Code Quality**: Linters automatically enforce coding standards (like PEP 8 for Python), ensuring consistency and maintainability, which is critical for collaborative projects.
16
+
-**Enhance Readability**: By standardizing style, linters make code easier to read and understand for all team members, streamlining code reviews and onboarding.
17
+
-**Prevent Bugs**: They detect common errors, such as syntax mistakes, undefined variables, or problematic patterns, catching bugs before they reach production.
18
+
-**Accelerate Learning**: For developers new to a language or a team, linters provide immediate feedback, helping them learn and adopt best practices quickly.
19
19
20
-
## Which tools should you use to lint your code?
20
+
## Which linting tool is recommended?
21
21
22
-
[Ruff](https://docs.astral.sh/ruff/) is a fast, modern linting tool that provides instant feedback, essential for efficient development workflows. The main benefit of ruff is its speed that outpaces many other linters, facilitating a quick fix cycle that doesn't hinder workflow. It also enforces a wide range of linting rules for code quality and consistency. There is also a [VS Code Extension for ruff](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) you can use to get linting message while you are typing you code.
22
+
[Ruff](https://docs.astral.sh/ruff/) is the recommended linter for modern Python projects. It is written in Rust and is exceptionally fast, often hundreds of times faster than other linters like Pylint. Its speed allows for real-time feedback in your editor without impacting performance.
23
+
24
+
Key advantages of Ruff include:
25
+
-**Speed**: Get instant feedback as you write code.
26
+
-**All-in-One**: It combines the functionality of multiple tools (e.g., `pylint`, `pyflakes`, `isort`) into a single, cohesive package.
27
+
-**Auto-Fixing**: Ruff can automatically fix many of the issues it detects, saving you time and effort.
28
+
-**VS Code Extension**: The official [Ruff VS Code extension](https://marketplace.visualstudio.com/items?itemName=charliermarsh.ruff) integrates these features directly into your editor.
23
29
24
30
```bash
25
-
#ruff installation (one shot)
31
+
#Install Ruff into your "check" dependency group
26
32
uv add --group check ruff
27
-
# ruff code base linting
28
-
uv run ruff src/ tests/
33
+
34
+
# Run Ruff to lint your codebase
35
+
uv run ruff check src/ tests/
29
36
```
30
37
31
-
Remember to exclude the `.ruff_cache/` directory in your `.gitignore` file to prevent the cache folder from being committed.
38
+
To keep your repository clean, remember to add the `.ruff_cache/` directory to your `.gitignore` file.
32
39
33
-
While there are other linting tools like [pylint](https://pylint.pycqa.org/) and [pyflakes](https://github.com/PyCQA/pyflakes), ruff is highlighted for its speed and comprehensive rule set.
40
+
## How do you configure a linter?
34
41
35
-
## How should you configure your linting tools?
42
+
Linter configurations are typically placed in the `pyproject.toml` file. This allows you to define project-wide rules, customize behavior, and ensure every developer uses the same settings.
36
43
37
-
Configure your linting tools within the `pyproject.toml` file to tailor their checks to your codebase's specific requirements:
44
+
Here is a sample configuration for Ruff:
38
45
39
46
```toml
40
47
[tool.ruff]
@@ -52,23 +59,31 @@ target-version = "py312"
52
59
"tests/*.py" = ["D100", "D103"]
53
60
```
54
61
55
-
If necessary, you can exclude linting rules either in your `pyproject.toml` file or inline if they are not relevant for your project:
62
+
If you need to ignore a specific rule for a single line, you can use an inline `noqa` (no quality assurance) comment:
56
63
57
64
```python
58
-
#ignore the error code F401 for the line below
59
-
fromabc.xyzimportfunction_name# noqa: F401
65
+
#Ignore the "unused import" error (F401) for this specific line
While often used together, linting and formatting have distinct purposes:
72
+
-**Linting** analyzes code for correctness and adherence to quality standards. It catches potential bugs and logical errors.
73
+
-**Formatting** focuses purely on style. It automatically rewrites code to enforce consistent layout, spacing, and line breaks, without changing its logic.
74
+
75
+
Tools like Ruff can perform both linting and formatting, providing a comprehensive solution for code quality and style consistency.
76
+
77
+
## What are the best practices for linting?
63
78
64
-
1.**Integrate with Development Workflow**: Make linting a staple of your development routine. Set up your IDE or code editor for immediate feedback.
65
-
2.**Customize Rules as Needed**: Tailor the linting rules to meet your project's unique needs, although default settings are a solid starting point.
66
-
3.**Regular Linting Sessions**: Promote frequent linting to prevent the accumulation of issues.
67
-
4.**Code Reviews and Linting**: Use linting reports during code reviews to ensure compliance with coding standards.
68
-
5.**Educate Team Members**: Ensure everyone understands the value of linting and knows how to use the tools efficiently.
69
-
6.**Balance Between Strictness and Flexibility**: Enforce rules on error-prone patterns strictly but allow flexibility for personal coding styles, provided they do not compromise code quality.
79
+
1.**Integrate Linting into Your Editor**: Configure your IDE or code editor to run the linter automatically, providing immediate feedback as you type.
80
+
2.**Automate with Pre-Commit Hooks**: Run the linter on staged files before they are committed. This practice catches issues early and keeps the main branch clean.
81
+
3.**Enforce Linting in CI/CD**: Add a linting step to your Continuous Integration (CI) pipeline to prevent code that violates standards from being merged.
82
+
4.**Start with Sensible Defaults**: Begin with the linter's default rule set and customize it over time by adding or ignoring rules that fit your project's specific needs.
83
+
5.**Use Linting in Code Reviews**: Make passing the linter a prerequisite for code review. This allows reviewers to focus on the logic and architecture instead of style debates.
84
+
6.**Keep Rules Consistent**: Ensure the entire team understands and adheres to the linting configuration to maintain a uniform codebase.
70
85
71
-
## Linting additional resources
86
+
## Additional Resources
72
87
73
88
-**[Linting configuration from the MLOps Python Package](https://github.com/fmind/mlops-python-package/blob/main/pyproject.toml)**
0 commit comments