Skip to content

Commit

Permalink
Faster enum impl
Browse files Browse the repository at this point in the history
  - If a value is failed to be found, will just return the inputed value. This way, if Discord adds new fields, it wont break.

Co-authored-by: nekokatt <nekoka.tt@outlook.com>
  • Loading branch information
davfsa and nekokatt committed Sep 23, 2020
1 parent 221c899 commit 0015600
Show file tree
Hide file tree
Showing 20 changed files with 671 additions and 119 deletions.
8 changes: 4 additions & 4 deletions hikari/api/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ async def edit_permission_overwrites(
channel: snowflakes.SnowflakeishOr[channels.GuildChannel],
target: snowflakes.Snowflakeish,
*,
target_type: typing.Union[channels.PermissionOverwriteType, str],
target_type: channels.PermissionOverwriteType,
allow: undefined.UndefinedOr[permissions_.Permissions] = undefined.UNDEFINED,
deny: undefined.UndefinedOr[permissions_.Permissions] = undefined.UNDEFINED,
reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
Expand All @@ -323,7 +323,7 @@ async def edit_permission_overwrites(
snowflakes.Snowflakeish, users.PartialUser, guilds.PartialRole, channels.PermissionOverwrite
],
*,
target_type: undefined.UndefinedOr[typing.Union[channels.PermissionOverwriteType, str]] = undefined.UNDEFINED,
target_type: undefined.UndefinedOr[channels.PermissionOverwriteType] = undefined.UNDEFINED,
allow: undefined.UndefinedOr[permissions_.Permissions] = undefined.UNDEFINED,
deny: undefined.UndefinedOr[permissions_.Permissions] = undefined.UNDEFINED,
reason: undefined.UndefinedOr[str] = undefined.UNDEFINED,
Expand All @@ -338,7 +338,7 @@ async def edit_permission_overwrites(
target : typing.Union[hikari.users.PartialUser, hikari.guilds.PartialRole, hikari.channels.PermissionOverwrite, hikari.snowflakes.Snowflakeish]
The channel overwrite to edit. This may be the object or the ID of an
existing overwrite.
target_type : hikari.undefined.UndefinedOr[typing.Union[hikari.channels.PermissionOverwriteType, builtins.str]]
target_type : hikari.undefined.UndefinedOr[hikari.channels.PermissionOverwriteType]
If provided, the type of the target to update. If unset, will attempt to get
the type from `target`.
allow : hikari.undefined.UndefinedOr[hikari.permissions.Permissions]
Expand Down Expand Up @@ -481,7 +481,7 @@ async def create_invite(
target_user : hikari.undefined.UndefinedOr[hikari.snowflakes.SnowflakeishOr[hikari.users.PartialUser]]
If provided, the target user id for this invite. This may be the
object or the ID of an existing user.
target_user_type : hikari.undefined.UndefinedOr[typing.Union[hikari.invites.TargetUserType, builtins.int]]
target_user_type : hikari.undefined.UndefinedOr[hikari.invites.TargetUserType]
If provided, the type of target user for this invite.
reason : hikari.undefined.UndefinedOr[builtins.str]
If provided, the reason that will be recorded in the audit logs.
Expand Down
8 changes: 3 additions & 5 deletions hikari/api/shard.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
__all__: typing.List[str] = ["GatewayDataFormat", "GatewayCompression", "GatewayShard"]

import abc
import enum
import typing

from hikari import undefined
from hikari.utilities import enums

if typing.TYPE_CHECKING:
import datetime
Expand All @@ -41,9 +41,8 @@
from hikari import users as users_


@enum.unique
@typing.final
class GatewayDataFormat(str, enum.Enum):
class GatewayDataFormat(str, enums.Enum):
"""Format of inbound gateway payloads."""

JSON = "json"
Expand All @@ -52,9 +51,8 @@ class GatewayDataFormat(str, enum.Enum):
"""Erlang transmission format."""


@enum.unique
@typing.final
class GatewayCompression(str, enum.Enum):
class GatewayCompression(str, enums.Enum):
"""Types of gateway compression that may be supported."""

TRANSPORT_ZLIB_STREAM = "transport_zlib_stream"
Expand Down
4 changes: 3 additions & 1 deletion hikari/api/special_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ class GuildBuilder(abc.ABC):
If not overridden, the guild will use the default voice region for Discord.
"""

verification_level: undefined.UndefinedOr[guilds.GuildVerificationLevel] = attr.ib(default=undefined.UNDEFINED)
verification_level: undefined.UndefinedOr[typing.Union[guilds.GuildVerificationLevel, int]] = attr.ib(
default=undefined.UNDEFINED
)
"""Verification level required to join the guild that can be overwritten.
If not overridden, the guild will use the default verification level for
Expand Down
16 changes: 7 additions & 9 deletions hikari/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
"TeamMembershipState",
]

import enum
import typing

import attr
Expand All @@ -44,6 +43,7 @@
from hikari import snowflakes
from hikari import urls
from hikari.utilities import attr_extensions
from hikari.utilities import enums
from hikari.utilities import routes

if typing.TYPE_CHECKING:
Expand All @@ -52,9 +52,8 @@
from hikari import users


@enum.unique
@typing.final
class OAuth2Scope(str, enum.Enum):
class OAuth2Scope(str, enums.Enum):
"""OAuth2 Scopes that Discord allows.
These are categories of permissions for applications using the OAuth2 API
Expand Down Expand Up @@ -184,9 +183,8 @@ def __str__(self) -> str:
return self.name


@enum.unique
@typing.final
class ConnectionVisibility(enum.IntEnum):
class ConnectionVisibility(int, enums.Enum):
"""Describes who can see a connection with a third party account."""

NONE = 0
Expand Down Expand Up @@ -235,7 +233,7 @@ class OwnConnection:
is_activity_visible: bool = attr.ib(eq=False, hash=False, repr=False)
"""`builtins.True` if this connection's activities are shown in the user's presence."""

visibility: ConnectionVisibility = attr.ib(eq=False, hash=False, repr=True)
visibility: typing.Union[ConnectionVisibility, int] = attr.ib(eq=False, hash=False, repr=True)
"""The visibility of the connection."""


Expand All @@ -250,8 +248,8 @@ class OwnGuild(guilds.PartialGuild):
"""The guild-level permissions that apply to the current user or bot."""


@enum.unique
class TeamMembershipState(enum.IntEnum):
@typing.final
class TeamMembershipState(int, enums.Enum):
"""Represents the state of a user's team membership."""

INVITED = 1
Expand All @@ -272,7 +270,7 @@ class TeamMember:
app: traits.RESTAware = attr.ib(repr=False, metadata={attr_extensions.SKIP_DEEP_COPY: True})
"""The client application that models may use for procedures."""

membership_state: TeamMembershipState = attr.ib(repr=False)
membership_state: typing.Union[TeamMembershipState, int] = attr.ib(repr=False)
"""The state of this user's membership."""

permissions: typing.Sequence[str] = attr.ib(repr=False)
Expand Down
9 changes: 4 additions & 5 deletions hikari/audit_logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@

import abc
import datetime
import enum
import typing

import attr

from hikari import snowflakes
from hikari.utilities import attr_extensions
from hikari.utilities import enums
from hikari.utilities import mapping

if typing.TYPE_CHECKING:
Expand All @@ -60,7 +60,7 @@


@typing.final
class AuditLogChangeKey(str, enum.Enum):
class AuditLogChangeKey(str, enums.Enum):
"""Commonly known and documented keys for audit log change objects.
Others may exist. These should be expected to default to the raw string
Expand Down Expand Up @@ -142,9 +142,8 @@ class AuditLogChange:
"""The name of the audit log change's key."""


@enum.unique
@typing.final
class AuditLogEventType(enum.IntEnum):
class AuditLogEventType(int, enums.Enum):
"""The type of event that occurred."""

GUILD_UPDATE = 1
Expand Down Expand Up @@ -204,7 +203,7 @@ class ChannelOverwriteEntryInfo(BaseAuditLogEntryInfo, snowflakes.Unique):
id: snowflakes.Snowflake = attr.ib(eq=True, hash=True, repr=True)
"""The ID of this entity."""

type: channels.PermissionOverwriteType = attr.ib(repr=True)
type: typing.Union[channels.PermissionOverwriteType, str] = attr.ib(repr=True)
"""The type of entity this overwrite targets."""

role_name: typing.Optional[str] = attr.ib(repr=True)
Expand Down
14 changes: 7 additions & 7 deletions hikari/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
]

import abc
import enum
import typing

import attr
Expand All @@ -54,6 +53,7 @@
from hikari import urls
from hikari import users
from hikari.utilities import attr_extensions
from hikari.utilities import enums
from hikari.utilities import routes

if typing.TYPE_CHECKING:
Expand All @@ -67,9 +67,8 @@
from hikari import webhooks


@enum.unique
@typing.final
class ChannelType(enum.IntEnum):
class ChannelType(int, enums.Enum):
"""The known channel types that are exposed to us by the API."""

GUILD_TEXT = 0
Expand Down Expand Up @@ -201,9 +200,8 @@ def channel(self) -> typing.Union[GuildNewsChannel, GuildTextChannel]:
return channel


@enum.unique
@typing.final
class PermissionOverwriteType(str, enum.Enum):
class PermissionOverwriteType(str, enums.Enum):
"""The type of entity a Permission Overwrite targets."""

ROLE = "role"
Expand Down Expand Up @@ -252,7 +250,9 @@ class PermissionOverwrite(snowflakes.Unique):
)
"""The ID of this entity."""

type: PermissionOverwriteType = attr.ib(converter=PermissionOverwriteType, eq=True, hash=True, repr=True)
type: typing.Union[PermissionOverwriteType, str] = attr.ib(
converter=PermissionOverwriteType, eq=True, hash=True, repr=True
)
"""The type of entity this overwrite targets."""

# Flags are lazily loaded, due to the IntFlag mechanism being overly slow
Expand Down Expand Up @@ -306,7 +306,7 @@ class PartialChannel(snowflakes.Unique):
name: typing.Optional[str] = attr.ib(eq=False, hash=False, repr=True)
"""The channel's name. This will be missing for DM channels."""

type: ChannelType = attr.ib(eq=False, hash=False, repr=True)
type: typing.Union[ChannelType, int] = attr.ib(eq=False, hash=False, repr=True)
"""The channel's type."""

def __str__(self) -> str:
Expand Down
5 changes: 2 additions & 3 deletions hikari/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
"VoiceError",
]

import enum
import http
import typing

import attr

from hikari.utilities import attr_extensions
from hikari.utilities import enums

if typing.TYPE_CHECKING:
from hikari import intents as intents_
Expand Down Expand Up @@ -109,9 +109,8 @@ def __str__(self) -> str:
return self.reason


@enum.unique
@typing.final
class ShardCloseCode(enum.IntEnum):
class ShardCloseCode(int, enums.Enum):
"""Reasons for a shard connection closure."""

NORMAL_CLOSURE = 1000
Expand Down
Loading

0 comments on commit 0015600

Please sign in to comment.