From 420646e76f2590c781ae8818bd41dac5f8633e90 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 25 Oct 2019 10:00:35 -0300 Subject: [PATCH] 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 --- litecord/auth.py | 30 +++++++++++++++--------------- litecord/enums.py | 30 ++++++++++++------------------ 2 files changed, 27 insertions(+), 33 deletions(-) diff --git a/litecord/auth.py b/litecord/auth.py index 078a02f..b96e475 100644 --- a/litecord/auth.py +++ b/litecord/auth.py @@ -56,20 +56,20 @@ async def raw_token_check(token: str, db=None) -> int: # just try by fragments instead of # unpacking fragments = token.split(".") - user_id = fragments[0] + user_id_str = fragments[0] try: - user_id = base64.b64decode(user_id.encode()) - user_id = int(user_id) + user_id_decoded = base64.b64decode(user_id_str.encode()) + user_id = int(user_id_decoded) except (ValueError, binascii.Error): raise Unauthorized("Invalid user ID type") pwd_hash = await db.fetchval( """ - SELECT password_hash - FROM users - WHERE id = $1 - """, + SELECT password_hash + FROM users + WHERE id = $1 + """, user_id, ) @@ -88,10 +88,10 @@ async def raw_token_check(token: str, db=None) -> int: # with people leaving their clients open forever) await db.execute( """ - UPDATE users - SET last_session = (now() at time zone 'utc') - WHERE id = $1 - """, + UPDATE users + SET last_session = (now() at time zone 'utc') + WHERE id = $1 + """, user_id, ) @@ -128,10 +128,10 @@ async def admin_check() -> int: flags = await app.db.fetchval( """ - SELECT flags - FROM users - WHERE id = $1 - """, + SELECT flags + FROM users + WHERE id = $1 + """, user_id, ) diff --git a/litecord/enums.py b/litecord/enums.py index ce68e18..7c5b143 100644 --- a/litecord/enums.py +++ b/litecord/enums.py @@ -54,27 +54,21 @@ class Flags: """ 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): - res = Flags() + @classmethod + def from_int(cls, value: int): + """Create a Flags from a given int value.""" + res = Flags() + setattr(res, "value", value) - setattr(res, "value", value) + for attr, val in cls._attrs: + has_attr = (value & val) == val + # set attributes dynamically + setattr(res, f"is_{attr}", has_attr) - for attr, val in 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 - - # set each attribute - setattr(res, f"is_{attr}", has_attr) - - return res - - cls.from_int = _make_int + return res class ChannelType(EasyEnum):