Modernisation Python 3.11+

VérifiéSûr

Appliquez les meilleures pratiques Python modernes avec les types, DRY et les patterns framework. Utilisez pour réviser du code Python en modernisation ou écrire du nouveau code avec les idiomes actuels.

Spar Skills Guide Bot
DeveloppementIntermédiaire
2002/06/2026
Claude Code
#modern-python#type-hints#best-practices#refactoring#code-quality

Recommandé pour

Notre avis

Ce guide applique les meilleures pratiques Python 3.11+ avec des types modernes, DRY, SRP et des patterns de frameworks pour la révision ou l'écriture de code Python.

Points forts

  • Automatise l'application des normes modernes (PEP 585, 604, 673, 678)
  • Propose des transformations concrètes avec des exemples avant/après
  • Couvre les tests avec pytest-mock et les interfaces CLI avec Typer
  • Intègre des principes d'architecture propre comme l'injection de dépendances

Limites

  • Ne remplace pas une revue manuelle pour des décisions architecturales complexes
  • Peut nécessiter une adaptation pour des bases de code très anciennes (Python < 3.11)
  • Se concentre sur la syntaxe et les patterns, pas sur l'optimisation algorithmique
Quand l'utiliser

Utilisez ce guide pour moderniser du code Python legacy, rédiger de nouveaux modules avec les standards actuels, ou revoir du code avec un œil sur les PEP récentes.

Quand l'éviter

Évitez ce guide si votre code cible Python 3.10 ou antérieur, ou si vous travaillez sur un projet qui impose des styles de codage plus anciens.

Analyse de sécurité

Sûr
Score qualité85/100

This skill provides purely advisory guidance on Python coding best practices and does not instruct any actions that could compromise security. No tools are declared or execution steps that could be dangerous.

Aucun point d'attention détecté

Exemples

Modernize a Python file
Modernize the following Python file to use Python 3.11+ patterns: main.py
Explain match-case
Explain how to use match-case in Python 3.11 with before/after examples for elif chains.
Refactor legacy code
Refactor this code to use type hints, walrus operator, and the Self type: src/models.py

name: modernpython description: Apply modern Python 3.11+ best practices with proper types, DRY, SRP, and framework patterns. Use when reviewing Python code for modernization opportunities, when writing new Python code to ensure modern patterns, or when refactoring legacy Python code to use current idioms. user-invocable: true argument-hint: "[file-paths-or-topic]"

Python Modernization Guide

The model applies modern Python 3.11+ patterns when writing or reviewing Python code.

Arguments

$ARGUMENTS

Instructions

If file paths provided:

  1. Read each file
  2. Identify legacy patterns
  3. Apply modern transformations from the reference guide
  4. Report changes made or recommended

If topic provided (e.g., "typing", "match-case"):

  1. Provide guidance on that specific topic
  2. Show before/after examples

If no arguments:

  1. Ask what code to review or what topic to explain

Quick Reference: Modern Patterns

Type Hints (PEP 585, 604)

# Legacy (NEVER use)
from typing import List, Dict, Optional, Union

# Modern (ALWAYS use)
items: list[str]
config: dict[str, int] | None
value: int | str

Walrus Operator (PEP 572)

# Legacy
data = fetch_data()
if data:
    process(data)

# Modern
if data := fetch_data():
    process(data)

Match-Case (PEP 634)

Use match-case when using elif. Use if/elif only for inequalities or boolean operators.

# Modern (for any elif pattern)
match status_code:
    case 200: return "OK"
    case 404: return "Not Found"
    case _: return "Unknown"

Self Type (PEP 673)

from typing import Self

class Builder:
    def add(self, x: int) -> Self:
        self.value += x
        return self

Exception Notes (PEP 678)

except FileNotFoundError as e:
    e.add_note(f"Attempted path: {path}")
    raise

StrEnum (Python 3.11+)

from enum import StrEnum

class Status(StrEnum):
    PENDING = "pending"
    RUNNING = "running"

TOML Support (Python 3.11+)

import tomllib
from pathlib import Path

config = tomllib.load(Path("pyproject.toml").open("rb"))

Testing Patterns

ALWAYS use pytest-mock, NEVER unittest.mock:

# Legacy (NEVER use)
from unittest.mock import Mock, patch

# Modern (ALWAYS use)
from pytest_mock import MockerFixture

def test_feature(mocker: MockerFixture) -> None:
    mock_func = mocker.patch('module.function', return_value=42)

Framework Patterns

Typer CLI

ALWAYS use Annotated syntax:

from typing import Annotated
import typer

@app.command()
def process(
    input_file: Annotated[Path, typer.Argument(help="Input file")],
    verbose: Annotated[bool, typer.Option("--verbose", "-v")] = False,
) -> None:
    """Process input file."""
    pass

Rich Tables

Use explicit width control for production CLIs:

from rich.console import Console
from rich.table import Table
from rich.measure import Measurement

def _get_table_width(table: Table) -> int:
    temp_console = Console(width=9999)
    measurement = Measurement.get(temp_console, temp_console.options, table)
    return int(measurement.maximum)

Detailed Reference

For complete transformation rules, PEP references, and framework patterns, see:

Complete Modernization Guide


Core Principles

  1. Use Python 3.11+ as minimum baseline
  2. Leverage built-in generics (PEP 585) and pipe unions (PEP 604) exclusively
  3. Apply walrus operator to reduce line count
  4. Use match-case for elif patterns
  5. Implement comprehensive type hints with Protocol, TypeVar, TypeGuard
  6. Use Self type (PEP 673) for fluent APIs
  7. Follow Typer patterns with Annotated syntax for CLIs
  8. Use Rich for terminal output with proper width handling
  9. Write pytest tests with pytest-mock and AAA pattern
  10. Apply clean architecture with dependency injection

References

Skills similaires