Skip to content

Commit

Permalink
Merge branch 'feat/entitlements' of github.com:Snipy7374/disnake into…
Browse files Browse the repository at this point in the history
… feat/entitlements
  • Loading branch information
Snipy7374 committed Nov 26, 2024
2 parents e6b3063 + 10a59b6 commit 55f2fd4
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ repos:
name: "run black in all files"

- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.3.4
rev: v0.8.0
hooks:
- id: ruff
args: [--fix, --fixable=I]
1 change: 1 addition & 0 deletions changelog/1247.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement the new :meth:`.Guild.fetch_role` API method.
2 changes: 1 addition & 1 deletion disnake/app_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ def localize(self, store: LocalizationProtocol) -> None:
o.localize(store)


class ApplicationCommand(ABC):
class ApplicationCommand(ABC): # noqa: B024 # this will get refactored eventually
"""The base class for application commands.
The following classes implement this ABC:
Expand Down
2 changes: 1 addition & 1 deletion disnake/ext/commands/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1187,7 +1187,7 @@ def get_converter(param: inspect.Parameter) -> Any:


def is_generic_type(tp: Any, *, _GenericAlias: Type = _GenericAlias) -> bool:
return isinstance(tp, type) and issubclass(tp, Generic) or isinstance(tp, _GenericAlias)
return (isinstance(tp, type) and issubclass(tp, Generic)) or isinstance(tp, _GenericAlias)


CONVERTER_MAPPING: Dict[Type[Any], Type[Converter]] = {
Expand Down
2 changes: 1 addition & 1 deletion disnake/ext/commands/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ def parse_parameter(self, param: inspect.Parameter) -> None:
self.param_name = param.name

def parse_doc(self, doc: disnake.utils._DocstringParam) -> None:
if self.type == str and doc["type"] is not None:
if self.type is str and doc["type"] is not None:
self.parse_annotation(doc["type"])

self.description = self.description or doc["description"]
Expand Down
2 changes: 1 addition & 1 deletion disnake/ext/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def next_iteration(self) -> Optional[datetime.datetime]:
"""
if self._task is MISSING:
return None
elif self._task and self._task.done() or self._stop_next_iteration:
elif (self._task and self._task.done()) or self._stop_next_iteration:
return None
return self._next_iteration

Expand Down
31 changes: 31 additions & 0 deletions disnake/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -3581,6 +3581,37 @@ async def delete_emoji(self, emoji: Snowflake, *, reason: Optional[str] = None)
"""
await self._state.http.delete_custom_emoji(self.id, emoji.id, reason=reason)

async def fetch_role(self, role_id: int, /) -> Role:
"""|coro|
Retrieve a :class:`Role`.
.. note::
This method is an API call. For general usage, consider :meth:`get_role` or :attr:`roles` instead.
.. versionadded:: 2.10
Parameters
----------
role_id: :class:`int`
The ID of the role to retrieve.
Raises
------
NotFound
The role requested could not be found.
HTTPException
Retrieving the role failed.
Returns
-------
:class:`Role`
The retrieved role.
"""
data = await self._state.http.get_role(self.id, role_id=role_id)
return Role(guild=self, state=self._state, data=data)

async def fetch_roles(self) -> List[Role]:
"""|coro|
Expand Down
5 changes: 5 additions & 0 deletions disnake/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,11 @@ def delete_invite(self, invite_id: str, *, reason: Optional[str] = None) -> Resp

# Role management

def get_role(self, guild_id: Snowflake, role_id: Snowflake) -> Response[role.Role]:
return self.request(
Route("GET", "/guilds/{guild_id}/roles/{role_id}", guild_id=guild_id, role_id=role_id)
)

def get_roles(self, guild_id: Snowflake) -> Response[List[role.Role]]:
return self.request(Route("GET", "/guilds/{guild_id}/roles", guild_id=guild_id))

Expand Down
23 changes: 9 additions & 14 deletions disnake/interactions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,10 @@ def __init__(self, *, data: InteractionPayload, state: ConnectionState) -> None:
self.author = (
isinstance(guild_fallback, Guild)
and guild_fallback.get_member(int(member["user"]["id"]))
or Member(
state=self._state,
guild=guild_fallback, # type: ignore # may be `Object`
data=member,
)
) or Member(
state=self._state,
guild=guild_fallback, # type: ignore # may be `Object`
data=member,
)
self._permissions = int(member.get("permissions", 0))
elif user := data.get("user"):
Expand Down Expand Up @@ -1915,15 +1914,11 @@ def __init__(
user_id = int(str_id)
member = members.get(str_id)
if member is not None:
self.members[user_id] = (
guild
and guild.get_member(user_id)
or Member(
data=member,
user_data=user,
guild=guild_fallback, # type: ignore
state=state,
)
self.members[user_id] = (guild and guild.get_member(user_id)) or Member(
data=member,
user_data=user,
guild=guild_fallback, # type: ignore
state=state,
)
else:
self.users[user_id] = User(state=state, data=user)
Expand Down
16 changes: 6 additions & 10 deletions disnake/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ def _add_global_application_command(
/,
) -> None:
if not application_command.id:
AssertionError("The provided application command does not have an ID")
raise AssertionError("The provided application command does not have an ID")
self._global_application_commands[application_command.id] = application_command

def _remove_global_application_command(self, application_command_id: int, /) -> None:
Expand All @@ -478,7 +478,7 @@ def _add_guild_application_command(
self, guild_id: int, application_command: APIApplicationCommand
) -> None:
if not application_command.id:
AssertionError("The provided application command does not have an ID")
raise AssertionError("The provided application command does not have an ID")
try:
granula = self._guild_application_commands[guild_id]
granula[application_command.id] = application_command
Expand Down Expand Up @@ -2100,14 +2100,10 @@ def _get_partial_interaction_channel(

# the factory can't be a DMChannel or GroupChannel here
data.setdefault("position", 0) # type: ignore
return (
isinstance(guild, Guild)
and guild.get_channel_or_thread(channel_id)
or factory(
guild=guild, # type: ignore # FIXME: create proper fallback guild instead of passing Object
state=self,
data=data, # type: ignore # generic payload type
)
return (isinstance(guild, Guild) and guild.get_channel_or_thread(channel_id)) or factory(
guild=guild, # type: ignore # FIXME: create proper fallback guild instead of passing Object
state=self,
data=data, # type: ignore # generic payload type
)

def get_channel(self, id: Optional[int]) -> Optional[Union[Channel, Thread]]:
Expand Down
2 changes: 1 addition & 1 deletion disnake/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ def evaluate_annotation(
return cache[tp]

# this is how annotations are supposed to be unstringifed
evaluated = eval(tp, globals, locals) # noqa: PGH001, S307
evaluated = eval(tp, globals, locals) # noqa: S307
# recurse to resolve nested args further
evaluated = evaluate_annotation(evaluated, globals, locals, cache)

Expand Down
21 changes: 16 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ tools = [
"slotscheck~=0.16.4",
"python-dotenv~=1.0.0",
"check-manifest==0.49",
"ruff==0.3.4",
"ruff==0.8.0",
]
changelog = [
"towncrier==23.6.0",
Expand Down Expand Up @@ -149,7 +149,7 @@ select = [
# "RET", # flake8-return
# "SIM", # flake8-simplify
"TID251", # flake8-tidy-imports, replaces S404
"TCH", # flake8-type-checking
"TC", # flake8-type-checking
"RUF", # ruff specific exceptions
"PT", # flake8-pytest-style
"Q", # flake8-quotes
Expand Down Expand Up @@ -178,6 +178,10 @@ ignore = [
"RUF005", # might not be actually faster
"RUF006", # might not be an issue/very extreme cases

# we keep __all__ and __slots__ (roughly) sorted by source, not alphabetically
"RUF022",
"RUF023",

# calling subprocess with dynamic arguments is generally fine, the only way to avoid this is ignoring it
"S603",

Expand All @@ -202,9 +206,9 @@ ignore = [
# ignore imports that could be moved into type-checking blocks
# (no real advantage other than possibly avoiding cycles,
# but can be dangerous in places where we need to parse signatures)
"TCH001",
"TCH002",
"TCH003",
"TC001",
"TC002",
"TC003",

"S311", # insecure RNG usage, we don't use these for security-related things
"PLE0237", # pyright seems to catch this already
Expand Down Expand Up @@ -259,6 +263,13 @@ mark-parentheses = false
[tool.ruff.lint.flake8-tidy-imports.banned-api]
"subprocess".msg = "Consider possible security implications associated with the subprocess module." # replaces S404

[tool.ruff.lint.flake8-bugbear]
extend-immutable-calls = [
# this is immutable, except for storing locks, which are fine to share across contexts
"disnake.webhook.async_.AsyncWebhookAdapter",
]


[tool.towncrier]
template = "changelog/_template.rst.jinja"
package = "disnake"
Expand Down
6 changes: 3 additions & 3 deletions tests/ext/commands/test_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ def test_nan(self, value) -> None:

def test_valid(self) -> None:
x: Any = commands.Range[int, -1, 2]
assert x.underlying_type == int
assert x.underlying_type is int

x: Any = commands.Range[float, ..., 23.45]
assert x.underlying_type == float
assert x.underlying_type is float


class TestString:
Expand Down Expand Up @@ -211,7 +211,7 @@ def test_optional(self, annotation_str) -> None:

assert info.min_value == 1
assert info.max_value == 2
assert info.type == int
assert info.type is int


class TestIsolateSelf:
Expand Down

0 comments on commit 55f2fd4

Please sign in to comment.