Код: Выделить всё
from dataclasses import dataclass
@dataclass
class Child:
f1: int
f2: int | None
@dataclass
class Parent:
child: Child
Для запуска ручных тестов (не pytest, не CI) , я хочу создать экземпляр Parent со случайными значениями.
Код: Выделить всё
from typing import TypeVar
import hypothesis
import hypothesis.strategies
T = TypeVar("T")
def generate(cls: type[T], seed: int) -> T:
objects = []
@hypothesis.seed(seed)
@hypothesis.given(hypothesis.strategies.from_type(cls))
@hypothesis.settings(max_examples=10)
def f(o):
objects.append(o)
f()
# Firts example tends to be a trivial one so return the last one instead
return objects[-1]
print(generate(Parent))
print(generate(Parent))
Код: Выделить всё
Parent(child=Child(f1=1234, f2=None))
Parent(child=Child(f1=-10, f2=4567))
Как мне обновить фрагмент, чтобы указать гипотезе никогда не выбирать None?
Подробнее здесь: https://stackoverflow.com/questions/793 ... nal-fields
Мобильная версия