admin管理员组文章数量:1434913
So my problem is the following
from typing import ClassVar, TypeVar
from hypothesis import strategies as st
T = TypeVar("T", bound="FixedUint")
class FixedUint(int):
MAX_VALUE: ClassVar["FixedUint"]
def __init__(self: T, value: int) -> None:
if not isinstance(value, int):
raise TypeError()
if value < 0 or value > self.MAX_VALUE:
raise OverflowError()
class U256(FixedUint):
pass
U256.MAX_VALUE = int.__new__(U256, (2**256) - 1)
class U64(FixedUint):
pass
U64.MAX_VALUE = int.__new__(U64, (2**64) - 1)
st.from_type(U64).example()
# TypeError: 'value' is an invalid keyword argument for int()
ie that hypothesis cannot build from type because the __init__
of FixedUint
has a value
kwarg for validation, but U64(value=1234)
is actually not valid.
How to make this work?
So my problem is the following
from typing import ClassVar, TypeVar
from hypothesis import strategies as st
T = TypeVar("T", bound="FixedUint")
class FixedUint(int):
MAX_VALUE: ClassVar["FixedUint"]
def __init__(self: T, value: int) -> None:
if not isinstance(value, int):
raise TypeError()
if value < 0 or value > self.MAX_VALUE:
raise OverflowError()
class U256(FixedUint):
pass
U256.MAX_VALUE = int.__new__(U256, (2**256) - 1)
class U64(FixedUint):
pass
U64.MAX_VALUE = int.__new__(U64, (2**64) - 1)
st.from_type(U64).example()
# TypeError: 'value' is an invalid keyword argument for int()
ie that hypothesis cannot build from type because the __init__
of FixedUint
has a value
kwarg for validation, but U64(value=1234)
is actually not valid.
How to make this work?
Share Improve this question asked Nov 18, 2024 at 9:34 ClementWalterClementWalter 5,3442 gold badges38 silver badges62 bronze badges1 Answer
Reset to default 0It turned out that we needed some small changes, which have been released as Hypothesis 6.123.0; once you update to that version the complete fix is to make all arguments to __init__
positional-only, i.e.
def __init__(self: T, value: int, /) -> None:
# ^^^
# add this bit
本文标签: pythonHypothesis cannot build from type when type is int subclassStack Overflow
版权声明:本文标题:python - Hypothesis cannot build from type when type is int subclass - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745629156a2667163.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论