-
I want to use my custom converter that needs to be constructed with the Union type: @commands.command()
async def command(ctx, number: Union[Literal[False], ClampedNumber(1, 100)):
... but this raises an error:
What's the best solution for this problem? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
I think the best solution here would be to add a method called def __call__(self):
... This will allow https://github.com/python/cpython/blob/bc3961485639cc73de7c4c7eed1b56f3c74939bf/Lib/typing.py#L173 to pass successfully, and allow your converter to be used as expected. |
Beta Was this translation helpful? Give feedback.
-
Without knowledge of how your ClampedNumber class is implemented, it's a bit hard to make any recommendations here. If the goal is just a number from 1-100 that plays nicely with typing's ecosystem class PositiveUnder101:
@classmethod
async def convert(cls, ctx, arg) -> int:
# validate and return an int or raise here Is simple and not subject to being broken by unknown future changes to python's typing ecosystem (some of which is currently up in the air with deferred evaluation of annotations being postponed) There are more options available, but this |
Beta Was this translation helpful? Give feedback.
I think the best solution here would be to add a method called
__call__
to your ClampedNumber class which should look likeThis will allow https://github.com/python/cpython/blob/bc3961485639cc73de7c4c7eed1b56f3c74939bf/Lib/typing.py#L173 to pass successfully, and allow your converter to be used as expected.