Skip to content

Instantly share code, notes, and snippets.

View atemate-dh's full-sized avatar

Artem Yushkovskiy atemate-dh

View GitHub Profile
@atemate-dh
atemate-dh / traits_demo.py
Last active July 21, 2025 21:42
Traits in Python with Protocol and Pydantic
from typing import Protocol, runtime_checkable
from pydantic import BaseModel, BeforeValidator
import inspect
from functools import wraps
from typing import Annotated, Optional, get_type_hints, Protocol, runtime_checkable
def enforce_protocol(func):
sig = inspect.signature(func)
hints = get_type_hints(func)
@wraps(func)