Skip to content

Commit e6026bd

Browse files
types: more type fixes
1 parent d336850 commit e6026bd

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

monty/exts/filters/codeblock/_cog.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def __init__(self, bot: Monty) -> None:
7272

7373
# Stores allowed channels plus epoch times since the last instructional messages sent.
7474
# TODO: remove or refactor this unused code
75-
self.channel_cooldowns = dict.fromkeys((), 0.0)
75+
self.channel_cooldowns: dict[int, float] = dict.fromkeys((), 0.0)
7676

7777
# Maps users' messages to the messages the bot sent with instructions.
7878
self.codeblock_message_ids = {}
@@ -104,7 +104,10 @@ async def get_sent_instructions(self, payload: disnake.RawMessageUpdateEvent) ->
104104
log.debug("Could not find instructions message; it was probably deleted.")
105105
return None
106106

107-
def is_on_cooldown(self, channel: Union[GuildMessageable, disnake.DMChannel]) -> bool:
107+
def is_on_cooldown(
108+
self,
109+
channel: disnake.DMChannel | disnake.GroupChannel | disnake.abc.GuildChannel | disnake.Thread,
110+
) -> bool:
108111
"""
109112
Return True if an embed was sent too recently for `channel`.
110113
@@ -115,10 +118,13 @@ def is_on_cooldown(self, channel: Union[GuildMessageable, disnake.DMChannel]) ->
115118
cooldown = constants.CodeBlock.cooldown_seconds
116119
return (time.time() - self.channel_cooldowns.get(channel.id, 0)) < cooldown
117120

118-
async def is_valid_channel(self, channel: Union[GuildMessageable, disnake.DMChannel]) -> bool:
121+
async def is_valid_channel(
122+
self,
123+
channel: disnake.DMChannel | disnake.GroupChannel | disnake.abc.GuildChannel | disnake.Thread,
124+
) -> bool:
119125
"""Return True if `channel` is a help channel, may be on a cooldown, or is whitelisted."""
120126
log.trace(f"Checking if #{channel} qualifies for code block detection.")
121-
if isinstance(channel, disnake.DMChannel):
127+
if not isinstance(channel, GuildMessageable):
122128
return False
123129
res = channel.guild and await self.bot.guild_has_feature(
124130
channel.guild, constants.Feature.CODEBLOCK_RECOMMENDATIONS

monty/exts/filters/codeblock/_instructions.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,18 @@ def _get_bad_lang_message(content: str) -> Optional[str]:
108108
f"There must not be any spaces after `{language}`."
109109
)
110110

111-
if lines:
112-
lines = " ".join(lines)
113-
example_blocks = _get_example(language)
114-
115-
# Note that _get_bad_ticks_message expects the first line to have two newlines.
116-
return (
117-
f"It looks like you incorrectly specified a language for your code block.\n\n{lines}"
118-
f"\n\n**Here is an example of how it should look:**\n{example_blocks}"
119-
)
120-
else:
111+
if not lines:
121112
log.trace("Nothing wrong with the language specifier; no instructions to return.")
113+
return
114+
115+
joined_lines = " ".join(lines)
116+
example_blocks = _get_example(language)
117+
118+
# Note that _get_bad_ticks_message expects the first line to have two newlines.
119+
return (
120+
f"It looks like you incorrectly specified a language for your code block.\n\n{joined_lines}"
121+
f"\n\n**Here is an example of how it should look:**\n{example_blocks}"
122+
)
122123

123124

124125
def _get_no_lang_message(content: str) -> Optional[str]:

monty/exts/meta/guild_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async def can_guild_set_config_option(bot: Monty, *, metadata: ConfigAttrMetadat
5353

5454

5555
@commands.register_injection
56-
async def config_option(inter: disnake.ApplicationCommandInteraction, option: str) -> tuple[str, ConfigAttrMetadata]:
56+
async def config_option(inter: disnake.GuildCommandInteraction, option: str) -> tuple[str, ConfigAttrMetadata]:
5757
"""
5858
Get a valid configuration option and its metadata.
5959

monty/utils/html_parsing.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from collections import namedtuple
77
from typing import TYPE_CHECKING, Collection, Iterable, Iterator, List, Optional, Union
88

9-
from bs4.element import NavigableString, Tag
9+
from bs4.element import NavigableString, PageElement, Tag
1010

1111
from monty.exts.info.docs import MAX_SIGNATURE_AMOUNT
1212
from monty.exts.info.docs._html import get_dd_description, get_general_description, get_signatures
@@ -137,7 +137,7 @@ def _truncate_signatures(signatures: Collection[str]) -> Union[List[str], Collec
137137

138138

139139
def _get_truncated_description(
140-
elements: Iterable[Union[Tag, NavigableString]] | Tag,
140+
elements: Iterable[Union[Tag, NavigableString, PageElement]] | Tag,
141141
markdown_converter: DocMarkdownConverter,
142142
max_length: int,
143143
max_lines: int,
@@ -153,21 +153,28 @@ def _get_truncated_description(
153153
rendered_length = 0
154154

155155
tag_end_index = 0
156+
156157
for element in elements:
157158
is_tag = isinstance(element, Tag)
159+
is_page_element = isinstance(element, PageElement)
158160
if is_tag:
159161
# remove links in headers
160162
# see also https://github.com/sphinx-doc/sphinx/blob/ba7408209e84ee413f240afc20f3c6b484a81f8f/sphinx/themes/basic/static/searchtools.js#L157
161163
for link in element.select(".headerlink"):
162164
link.decompose()
163-
element_length = len(element.text) if is_tag else len(element)
165+
166+
element_length = len(element.text)
167+
elif is_page_element:
168+
element_length = 0
169+
else:
170+
element_length = len(element)
164171

165172
if rendered_length + element_length >= max_length:
166173
break
167174
if is_tag:
168175
element_markdown = markdown_converter.process_tag(element, convert_as_inline=False)
169176
else:
170-
element_markdown = markdown_converter.process_text(element)
177+
element_markdown = markdown_converter.process_text(element.text)
171178

172179
rendered_length += element_length
173180
tag_end_index += len(element_markdown)

0 commit comments

Comments
 (0)