Skip to content

Commit dddeb46

Browse files
committed
Extract function
1 parent dd6a551 commit dddeb46

1 file changed

Lines changed: 21 additions & 15 deletions

File tree

disnake/ext/commands/params.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,23 @@ def _range_to_str_len(min_value: int, max_value: int) -> Tuple[int, int]:
191191
return min(min_, max_), max(min_, max_)
192192

193193

194+
def _unbound_range_to_str_len(
195+
min_value: Optional[int], max_value: Optional[int]
196+
) -> Tuple[Optional[int], Optional[int]]:
197+
if min_value is not None and max_value is not None:
198+
return _range_to_str_len(min_value, max_value)
199+
200+
elif min_value is not None and min_value > 0:
201+
# 0 < min_value <= max_value == inf
202+
return _int_to_str_len(min_value), None
203+
204+
elif max_value is not None and max_value < 0:
205+
# -inf == min_value <= max_value < 0
206+
return None, _int_to_str_len(max_value)
207+
208+
return None, None
209+
210+
194211
class Injection(Generic[P, T_]):
195212
"""Represents a slash command injection.
196213
@@ -792,21 +809,10 @@ def parse_annotation(self, annotation: Any, converter_mode: bool = False) -> boo
792809
if annotation is not int:
793810
raise TypeError("Large integers must be annotated with int or LargeInt")
794811
self.type = str
795-
796-
if self.min_value is not None and self.max_value is not None:
797-
# honestly would rather assert than type ignore these
798-
self.min_length, self.max_length = _range_to_str_len(
799-
self.min_value, # pyright: ignore
800-
self.max_value, # pyright: ignore
801-
)
802-
803-
elif self.min_value is not None and self.min_value > 0:
804-
# 0 < min_value <= max_value == inf
805-
self.min_length = _int_to_str_len(self.min_value) # pyright: ignore
806-
807-
elif self.max_value is not None and self.max_value < 0:
808-
# -inf == max_value <= min_value < 0
809-
self.max_length = _int_to_str_len(self.max_value) # pyright: ignore
812+
self.min_length, self.max_length = _unbound_range_to_str_len(
813+
self.min_value, # type: ignore
814+
self.max_value, # type: ignore
815+
)
810816

811817
elif annotation in self.TYPES:
812818
self.type = annotation

0 commit comments

Comments
 (0)