最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

python - Hypothesis cannot build from type when type is int subclass - Stack Overflow

matteradmin5PV0评论

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
Post a comment

comment list (0)

  1. No comments so far