From 566818fc81a5e0d8e2b20577c7ab2c28371cd6ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Riegel?= Date: Wed, 24 Jun 2026 11:24:09 +0200 Subject: [PATCH] [2026/spec]: improve type hints --- 2026/spec/main.py | 6 +++--- 2026/spec/rules.py | 16 +++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/2026/spec/main.py b/2026/spec/main.py index cd8312e8..0723ee3a 100644 --- a/2026/spec/main.py +++ b/2026/spec/main.py @@ -45,17 +45,17 @@ def has_override(u: User) -> bool: @rule -def account_older_than(days: int, u: User) -> bool: +def account_older_than(u: User, days: int) -> bool: return u.account_age > days @rule -def from_country(countries: Iterable[str], u: User) -> bool: +def from_country(u: User, countries: Iterable[str]) -> bool: return u.country in countries @rule -def credit_score_above(threshold: int, u: User) -> bool: +def credit_score_above(u: User, threshold: int) -> bool: return u.credit_score > threshold diff --git a/2026/spec/rules.py b/2026/spec/rules.py index 922e344c..bfdbcab7 100644 --- a/2026/spec/rules.py +++ b/2026/spec/rules.py @@ -1,21 +1,23 @@ +from __future__ import annotations + import json from functools import wraps -from typing import Any, Callable +from typing import Any, Callable, Concatenate # ------------------------------------------------------------ # Generic Types # ------------------------------------------------------------ type PredicateFn[T] = Callable[[T], bool] -type RuleDef = Callable[..., bool] -type PredicateFactory[T] = Callable[..., Predicate[T]] +type RuleDef[T, **P] = Callable[Concatenate[T, P], bool] +type PredicateFactory[T, **P] = Callable[P, Predicate[T]] # ------------------------------------------------------------ # Global Rule Registry # ------------------------------------------------------------ -RULES: dict[str, PredicateFactory[Any]] = {} +RULES: dict[str, PredicateFactory[Any, ...]] = {} # ------------------------------------------------------------ @@ -59,10 +61,10 @@ def wrapper(obj: T) -> bool: -def rule[T](fn: RuleDef) -> PredicateFactory[Any]: +def rule[T, **P](fn: RuleDef[T, P]) -> PredicateFactory[T, P]: @wraps(fn) - def wrapper(*args: Any, **kwargs: Any) -> Predicate[T]: - return Predicate(lambda obj: fn(*args, obj, **kwargs)) + def wrapper(*args: P.args, **kwargs: P.kwargs) -> Predicate[T]: + return Predicate(lambda obj: fn(obj, *args, **kwargs)) RULES[fn.__name__] = wrapper return wrapper