mirror of https://gitlab.com/litecord/litecord.git
revamp how Flags works
instead of pulling a hack and injecting from_int() in `__init_subclass__`, we just make an actual function, and cache the wanted attributes in the subclass at its creation this fixes statis analyzers claiming from_int() doesn't exist on subclasses, for good reason, as they'd be turing-complete to do so, lol - auth: fix some mypy issues about reusing same variable
This commit is contained in:
parent
7278c15d9c
commit
420646e76f
|
|
@ -56,11 +56,11 @@ async def raw_token_check(token: str, db=None) -> int:
|
||||||
# just try by fragments instead of
|
# just try by fragments instead of
|
||||||
# unpacking
|
# unpacking
|
||||||
fragments = token.split(".")
|
fragments = token.split(".")
|
||||||
user_id = fragments[0]
|
user_id_str = fragments[0]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
user_id = base64.b64decode(user_id.encode())
|
user_id_decoded = base64.b64decode(user_id_str.encode())
|
||||||
user_id = int(user_id)
|
user_id = int(user_id_decoded)
|
||||||
except (ValueError, binascii.Error):
|
except (ValueError, binascii.Error):
|
||||||
raise Unauthorized("Invalid user ID type")
|
raise Unauthorized("Invalid user ID type")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,28 +54,22 @@ class Flags:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init_subclass__(cls, **_kwargs):
|
def __init_subclass__(cls, **_kwargs):
|
||||||
attrs = inspect.getmembers(cls, lambda x: not inspect.isroutine(x))
|
# get only the members that represent a field
|
||||||
|
cls._attrs = inspect.getmembers(cls, lambda x: isinstance(x, int))
|
||||||
|
|
||||||
def _make_int(value):
|
@classmethod
|
||||||
|
def from_int(cls, value: int):
|
||||||
|
"""Create a Flags from a given int value."""
|
||||||
res = Flags()
|
res = Flags()
|
||||||
|
|
||||||
setattr(res, "value", value)
|
setattr(res, "value", value)
|
||||||
|
|
||||||
for attr, val in attrs:
|
for attr, val in cls._attrs:
|
||||||
# get only the ones that represent a field in the
|
|
||||||
# number's bits
|
|
||||||
if not isinstance(val, int):
|
|
||||||
continue
|
|
||||||
|
|
||||||
has_attr = (value & val) == val
|
has_attr = (value & val) == val
|
||||||
|
# set attributes dynamically
|
||||||
# set each attribute
|
|
||||||
setattr(res, f"is_{attr}", has_attr)
|
setattr(res, f"is_{attr}", has_attr)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
cls.from_int = _make_int
|
|
||||||
|
|
||||||
|
|
||||||
class ChannelType(EasyEnum):
|
class ChannelType(EasyEnum):
|
||||||
GUILD_TEXT = 0
|
GUILD_TEXT = 0
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue