Skip to content

Commit 4476abc

Browse files
committed
Update Chapter 4
1 parent d112635 commit 4476abc

File tree

8 files changed

+477
-368
lines changed

8 files changed

+477
-368
lines changed

docs/4. Validating/4.0. Typing.md

Lines changed: 72 additions & 71 deletions
Large diffs are not rendered by default.

docs/4. Validating/4.1. Linting.md

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
---
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.
33
---
44

55
# 4.1. Linting
66

77
## What is software linting?
88

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.
1010

11-
## Why should you use linter tools?
11+
## Why are linters essential for development?
1212

13-
Linters are indispensable in Python development due to several key reasons:
13+
Integrating linters into a development workflow provides significant benefits:
1414

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.
1919

20-
## Which tools should you use to lint your code?
20+
## Which linting tool is recommended?
2121

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.
2329

2430
```bash
25-
# ruff installation (one shot)
31+
# Install Ruff into your "check" dependency group
2632
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/
2936
```
3037

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.
3239

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?
3441

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.
3643

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:
3845

3946
```toml
4047
[tool.ruff]
@@ -52,23 +59,31 @@ target-version = "py312"
5259
"tests/*.py" = ["D100", "D103"]
5360
```
5461

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:
5663

5764
```python
58-
# ignore the error code F401 for the line below
59-
from abc.xyz import function_name # noqa: F401
65+
# Ignore the "unused import" error (F401) for this specific line
66+
from project.module import specific_import # noqa: F401
6067
```
6168

62-
## What are the best practices for linting code?
69+
## How does linting differ from formatting?
70+
71+
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?
6378

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.
7085

71-
## Linting additional resources
86+
## Additional Resources
7287

7388
- **[Linting configuration from the MLOps Python Package](https://github.com/fmind/mlops-python-package/blob/main/pyproject.toml)**
7489
- [Ruff Tutorial](https://docs.astral.sh/ruff/tutorial/)

0 commit comments

Comments
 (0)