Основные изменения: - Добавлена иерархия исключений (17 классов) с кодами ошибок и контекстом - Улучшена обработка ошибок: детальные сообщения с подсказками - Добавлено 24 теста для экстремальных случаев (комбинаторика, циклы, async) - Добавлено 23 теста для системы обработки ошибок - Исправлен баг с optional-аргументами в renderer.py - Обновлены импорты в тестах (src.breakshaft → breakshaft) Документация: - ERROR_DESIGN.md — проектирование системы ошибок - COMMUTATIVITY_DESIGN.md — анализ проблемы некоммутативности (10 вариантов решений) Файлы: - src/breakshaft/exceptions.py (новый) — модуль исключений - tests/test_error_handling.py (новый) — тесты ошибок - tests/test_extreme_cases.py (новый) — экстремальные кейсы Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
71 lines
1.5 KiB
Python
71 lines
1.5 KiB
Python
from dataclasses import dataclass
|
|
|
|
from breakshaft.convertor import ConvRepo
|
|
|
|
|
|
@dataclass
|
|
class A:
|
|
a: int
|
|
|
|
|
|
@dataclass
|
|
class B:
|
|
b: float
|
|
|
|
|
|
def test_basic():
|
|
repo = ConvRepo()
|
|
|
|
@repo.mark_injector()
|
|
def b_to_a(b: B) -> A:
|
|
return A(int(b.b))
|
|
|
|
@repo.mark_injector()
|
|
def a_to_b(a: A) -> B:
|
|
return B(float(a.a))
|
|
|
|
@repo.mark_injector()
|
|
def int_to_a(i: int) -> A:
|
|
return A(i)
|
|
|
|
def consumer(dep: A) -> int:
|
|
return dep.a
|
|
|
|
fn1 = repo.get_conversion((B,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
|
dep = fn1(B(42.1))
|
|
assert dep == 42
|
|
|
|
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
|
dep = fn2(123)
|
|
assert dep == 123
|
|
|
|
|
|
def test_union_deps():
|
|
repo = ConvRepo()
|
|
|
|
@repo.mark_injector()
|
|
def b_to_a(b: B) -> A:
|
|
return A(int(b.b))
|
|
|
|
@repo.mark_injector()
|
|
def a_to_b(a: A) -> B:
|
|
return B(float(a.a))
|
|
|
|
@repo.mark_injector()
|
|
def int_to_a(i: int) -> A:
|
|
return A(i)
|
|
|
|
def consumer(dep: A | B) -> int:
|
|
if isinstance(dep, A):
|
|
return dep.a
|
|
else:
|
|
return int(dep.b)
|
|
|
|
fn1 = repo.get_conversion((B,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
|
dep = fn1(B(42.1))
|
|
assert dep == 42
|
|
|
|
fn2 = repo.get_conversion((int,), consumer, force_commutative=True, force_async=False, allow_async=False)
|
|
dep = fn2(123)
|
|
assert dep == 123
|