Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Code Style & Guidelines Demo

This demo covers Python code formatting, linting, and type checking using modern tools.

Learning Objectives

  • Understand PEP8 style guidelines
  • Use Ruff for linting and formatting
  • Apply type hints and use mypy for type checking

1. Introduction to PEP8

PEP8 is the official Python style guide. Key points:

  • Indentation: Use 4 spaces per indentation level
  • Line length: Maximum 79-88 characters
  • Imports: One per line, grouped (standard library, third-party, local)
  • Whitespace: Around operators, after commas, not inside brackets
  • Naming: snake_case for functions/variables, PascalCase for classes

2. Ruff: The All-in-One Tool

Ruff is an extremely fast Python linter and formatter that replaces multiple tools (flake8, black, isort, etc.).

IDE Integration

With the Ruff VS Code extension installed:

  1. Automatic formatting on save - Code is formatted when you save
  2. Inline error highlighting - Problems appear as you type
  3. Quick fixes - Click the lightbulb icon for auto-fixes

Command Line Usage

# Check for linting issues
ruff check .

# Check a specific file
ruff check demos/01-code-style/bad_example.py

# Auto-fix issues where possible
ruff check --fix .

# Format code
ruff format .

# Format a specific file
ruff format demos/01-code-style/bad_example.py

# Check what would change without applying
ruff format --check .

3. Demo: Bad vs Good Code

Open these two files side by side to see the difference:

File Description
bad_example.py Code with PEP8 violations
good_example.py The same code, properly formatted

Try it yourself:

# See all issues in the bad example
ruff check demos/01-code-style/bad_example.py

# Format it and see the difference
ruff format demos/01-code-style/bad_example.py

4. Type Checking with mypy

Type hints make code more readable and catch bugs early.

Basic Type Hints

def greet(name: str) -> str:
    return f"Hello, {name}!"

def calculate_average(numbers: list[float]) -> float:
    return sum(numbers) / len(numbers)

Running mypy

# Check a file
mypy demos/01-code-style/type_checking_demo.py

# Check with strict mode
mypy --strict demos/01-code-style/type_checking_demo.py

Demo File

See type_checking_demo.py for examples of:

  • Basic type hints
  • Optional types
  • Collection types
  • Type aliases
  • What mypy catches

5. Configuration

All tool settings are in the root pyproject.toml:

[tool.ruff]
line-length = 88
target-version = "py312"

[tool.ruff.lint]
select = ["E", "W", "F", "I", "B", "C4", "UP"]

[tool.mypy]
python_version = "3.12"
disallow_untyped_defs = true

Key Takeaways

  1. Consistency matters - Follow PEP8 for readable code
  2. Automate formatting - Let tools handle style, focus on logic
  3. Use type hints - Catch bugs before runtime
  4. Configure once - pyproject.toml stores all settings

Next Steps

Complete the task in tasks/01-code-style/ to practice refactoring messy code.