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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

It 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