Skip to content

Commit 21808f7

Browse files
committed
Fix StringExprConverter signature
1 parent 72b08cc commit 21808f7

1 file changed

Lines changed: 25 additions & 23 deletions

File tree

snakecore/commands/converters.py

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,8 @@ def __init__(
433433
self.language = language
434434

435435
if inline and language is not None:
436-
raise ValueError(
437-
"a code block cannot be both inline and have a language"
438-
)
439-
436+
raise ValueError("a code block cannot be both inline and have a language")
437+
440438
self.inline = inline if inline is not None else not (language or "\n" in code)
441439

442440
@classmethod
@@ -457,12 +455,11 @@ async def convert(cls, ctx: commands.Context[_DECBotT], argument: str) -> Self:
457455
view.index = multiline_match.end()
458456

459457
argument = parsed_argument
460-
458+
461459
elif argument.startswith("`"):
462460
if not argument.endswith("`") or (
463461
argument.endswith("`") and argument == "`"
464462
):
465-
466463
parsed_argument = argument.strip("\n").strip()
467464
inline_match = cls._INLINE_PATTERN.match(
468465
view.buffer, pos=view.index - len(parsed_argument)
@@ -473,7 +470,7 @@ async def convert(cls, ctx: commands.Context[_DECBotT], argument: str) -> Self:
473470
view.index = inline_match.end()
474471

475472
argument = parsed_argument
476-
473+
477474
try:
478475
return cls.from_markdown(argument)
479476
except (TypeError, ValueError) as err:
@@ -488,7 +485,9 @@ def from_markdown(cls, markdown: str) -> Self:
488485
"argument 'markdown' must be of type 'str' containing a markdown code block, "
489486
f"not {markdown.__class__.__name__}"
490487
)
491-
elif markdown == "```" or not (markdown.startswith("`") and markdown.endswith("`") and len(markdown) > 2):
488+
elif markdown == "```" or not (
489+
markdown.startswith("`") and markdown.endswith("`") and len(markdown) > 2
490+
):
492491
raise ValueError(
493492
"argument 'markdown' does not contain a markdown code block"
494493
)
@@ -503,7 +502,7 @@ def from_markdown(cls, markdown: str) -> Self:
503502
raise ValueError(
504503
"argument 'markdown' does not contain a valid markdown code block"
505504
)
506-
505+
507506
language = match_.group(1) or None
508507
code = match_.group(2).replace(
509508
"\\```", "```"
@@ -769,9 +768,9 @@ def __repr__(self) -> str:
769768

770769

771770
class StringExprConverter(_StringConverter, Generic[Unpack[_TVT]]): # type: ignore
772-
def __init__(self, regex: str, examples: tuple[str, ...]) -> None:
771+
def __init__(self, regex: str = "", examples: tuple[str, ...] = ()) -> None:
773772
super().__init__()
774-
self.regex_pattern = re.compile(regex)
773+
self.regex_pattern = re.compile(regex if regex else r".*")
775774
self.examples = examples
776775

777776
def __class_getitem__(cls, regex_and_examples: str | tuple[str, ...]) -> Self:
@@ -806,7 +805,7 @@ async def convert(self, ctx: commands.Context[_DECBotT], argument: str) -> str:
806805

807806

808807
class StringExprMatchConverter(StringExprConverter, Generic[Unpack[_TVT]]): # type: ignore
809-
async def convert(
808+
async def convert( # type: ignore
810809
self, ctx: commands.Context[_DECBotT], argument: str
811810
) -> re.Match[str]:
812811
string = await super().convert(ctx, argument)
@@ -957,15 +956,17 @@ async def convert(
957956

958957
if (
959958
not (
960-
parens_slice := self._find_parenthesized_region( # assume that inital parsed argument is parenthesized
959+
parens_slice
960+
:= self._find_parenthesized_region( # assume that inital parsed argument is parenthesized
961961
parsed_argument,
962962
self.OPENING,
963963
self.CLOSING,
964964
)
965965
)
966966
and (splintered := True)
967967
and not (
968-
parens_slice := self._find_parenthesized_region( # if it isn't, assume that it at least starts with self.OPENING and try to find full region
968+
parens_slice
969+
:= self._find_parenthesized_region( # if it isn't, assume that it at least starts with self.OPENING and try to find full region
969970
view.buffer[view.index - len(argument) :],
970971
self.OPENING,
971972
self.CLOSING,
@@ -985,15 +986,13 @@ async def convert(
985986
fake_parameter = commands.parameter()
986987

987988
if splintered:
988-
989989
parens_slice = slice(
990990
parens_slice.start + view.index - len(argument),
991991
parens_slice.stop + view.index - len(argument),
992992
) # offset slice to match the view's buffer string
993993

994994
view.index -= len(argument)
995995
else:
996-
997996
parens_slice = slice(
998997
parens_slice.start + view.index - len(argument) - 1,
999998
parens_slice.stop + view.index - len(argument) - 1,
@@ -1011,7 +1010,6 @@ async def convert(
10111010
is_variadic = self.converters[-1] is Ellipsis
10121011

10131012
while True:
1014-
10151013
if view.index >= parens_slice.stop - 1:
10161014
break
10171015

@@ -1065,7 +1063,6 @@ async def convert(
10651063
previous_previous = view.previous
10661064
previous_index = view.index
10671065
try:
1068-
10691066
temp_previous = view.previous
10701067
temp_index = view.index
10711068
try:
@@ -1076,7 +1073,6 @@ async def convert(
10761073
ctx.current_argument = fake_argument = view.get_word()
10771074

10781075
except commands.ArgumentParsingError as a:
1079-
10801076
ctx.current_parameter = original_parameter
10811077
ctx.current_argument = argument
10821078
view.previous = old_previous
@@ -1099,7 +1095,10 @@ async def convert(
10991095
fake_argument = fake_argument[:-2]
11001096
# catch last argument ... that ended with ')' followed by '"' (or any other quote): '...)"'
11011097
transformed = await commands.run_converters(
1102-
ctx, converter, fake_argument, fake_parameter # type: ignore
1098+
ctx,
1099+
converter,
1100+
fake_argument,
1101+
fake_parameter, # type: ignore
11031102
)
11041103
outputs.append(transformed)
11051104

@@ -1130,7 +1129,7 @@ async def convert(
11301129
"Parsing parenthesized argument failed "
11311130
f"(at depth {self.parens_depth}): Failed to parse "
11321131
"parenthesized argument at position "
1133-
f"{converter_index+1}: {err!s}"
1132+
f"{converter_index + 1}: {err!s}"
11341133
) from err
11351134

11361135
if (
@@ -1212,6 +1211,7 @@ class ReferencedMessageConverter(commands.Converter[discord.Message]):
12121211
To work around this, you must at least specify one character (e.g. '.')
12131212
to be 'consumed' by this converter, even though that character wouldn't be parsed.
12141213
"""
1214+
12151215
async def convert(
12161216
self, ctx: commands.Context[_BotT], argument: str
12171217
) -> discord.Message:
@@ -1243,7 +1243,7 @@ async def convert(
12431243
else:
12441244
raise commands.UserInputError("Failed to retrieve the referenced message.")
12451245

1246-
if not ctx.view.eof: # don't backtrack if no other converters follow
1246+
if not ctx.view.eof: # don't backtrack if no other converters follow
12471247
ctx.view.undo() # backtrack to pass on argument to next converter
12481248

12491249
return message
@@ -1490,7 +1490,9 @@ async def _convert_flag(
14901490
return await _convert_flag(ctx, argument, flag, annotation)
14911491
elif origin is Union and type(None) in annotation.__args__:
14921492
# typing.Optional[x]
1493-
annotation = Union[tuple(arg for arg in annotation.__args__ if arg is not type(None))] # type: ignore
1493+
annotation = Union[
1494+
tuple(arg for arg in annotation.__args__ if arg is not type(None))
1495+
] # type: ignore
14941496
return await commands.run_converters(ctx, annotation, argument, param)
14951497
elif origin is dict:
14961498
# typing.Dict[K, V] -> typing.Tuple[K, V]

0 commit comments

Comments
 (0)