From 9e43700f558e4dcca7e281f4d712de14986cf1dd Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Wed, 13 Sep 2023 20:07:07 +0200 Subject: [PATCH] Fix docgen in strict mode --- .../src/telethon/_impl/client/types/meta.py | 26 ++++++++++++++++++- .../telethon/_impl/mtproto/mtp/encrypted.py | 2 +- .../src/telethon/_impl/mtproto/mtp/types.py | 2 +- client/src/telethon/types.py | 9 ++++--- client/tests/mtproto_test.py | 6 ++--- 5 files changed, 36 insertions(+), 9 deletions(-) diff --git a/client/src/telethon/_impl/client/types/meta.py b/client/src/telethon/_impl/client/types/meta.py index d3f02019..3996e577 100644 --- a/client/src/telethon/_impl/client/types/meta.py +++ b/client/src/telethon/_impl/client/types/meta.py @@ -1,9 +1,33 @@ +""" +Class definitions stolen from `trio`, with some modifications. +""" +import abc from typing import Type, TypeVar T = TypeVar("T") -class NoPublicConstructor(type): +class Final(abc.ABCMeta): + def __new__( + cls, + name: str, + bases: tuple[type, ...], + cls_namespace: dict[str, object], + ) -> "Final": + # Allow subclassing while within telethon._impl (or other package names). + allowed_base = Final.__module__[ + : Final.__module__.find(".", Final.__module__.find(".") + 1) + ] + for base in bases: + if isinstance(base, Final) and not base.__module__.startswith(allowed_base): + raise TypeError( + f"{base.__module__}.{base.__qualname__} does not support" + " subclassing" + ) + return super().__new__(cls, name, bases, cls_namespace) + + +class NoPublicConstructor(Final): def __call__(cls, *args: object, **kwargs: object) -> None: raise TypeError( f"{cls.__module__}.{cls.__qualname__} has no public constructor" diff --git a/client/src/telethon/_impl/mtproto/mtp/encrypted.py b/client/src/telethon/_impl/mtproto/mtp/encrypted.py index 2f39ce28..ff5fc1e2 100644 --- a/client/src/telethon/_impl/mtproto/mtp/encrypted.py +++ b/client/src/telethon/_impl/mtproto/mtp/encrypted.py @@ -204,7 +204,7 @@ class Encrypted(Mtp): self._rpc_results.append( ( msg_id, - RpcError.from_mtproto_error(GeneratedRpcError.from_bytes(result)), + RpcError._from_mtproto_error(GeneratedRpcError.from_bytes(result)), ) ) elif inner_constructor == RpcAnswerUnknown.constructor_id(): diff --git a/client/src/telethon/_impl/mtproto/mtp/types.py b/client/src/telethon/_impl/mtproto/mtp/types.py index 536aa44d..60fac4ef 100644 --- a/client/src/telethon/_impl/mtproto/mtp/types.py +++ b/client/src/telethon/_impl/mtproto/mtp/types.py @@ -38,7 +38,7 @@ class RpcError(ValueError): return self._value @classmethod - def from_mtproto_error(cls, error: GeneratedRpcError) -> Self: + def _from_mtproto_error(cls, error: GeneratedRpcError) -> Self: if m := re.search(r"-?\d+", error.error_message): name = re.sub( r"_{2,}", diff --git a/client/src/telethon/types.py b/client/src/telethon/types.py index add55ea2..0bb73d51 100644 --- a/client/src/telethon/types.py +++ b/client/src/telethon/types.py @@ -1,3 +1,4 @@ +from ._impl.client.client import Config, InlineResult, InlineResults from ._impl.client.types import ( AsyncList, Channel, @@ -9,15 +10,17 @@ from ._impl.client.types import ( LoginToken, MediaLike, Message, - NoPublicConstructor, OutFileLike, PasswordToken, RestrictionReason, User, ) -from ._impl.session import PackedChat +from ._impl.session import PackedChat, PackedType __all__ = [ + "Config", + "InlineResult", + "InlineResults", "AsyncList", "Channel", "Chat", @@ -28,10 +31,10 @@ __all__ = [ "LoginToken", "MediaLike", "Message", - "NoPublicConstructor", "OutFileLike", "PasswordToken", "RestrictionReason", "User", "PackedChat", + "PackedType", ] diff --git a/client/tests/mtproto_test.py b/client/tests/mtproto_test.py index d3fbdaf9..ff65932f 100644 --- a/client/tests/mtproto_test.py +++ b/client/tests/mtproto_test.py @@ -7,7 +7,7 @@ from telethon._impl.tl.mtproto.types import RpcError as GeneratedRpcError def test_rpc_error_parsing() -> None: - assert RpcError.from_mtproto_error( + assert RpcError._from_mtproto_error( GeneratedRpcError( error_code=400, error_message="CHAT_INVALID", @@ -19,7 +19,7 @@ def test_rpc_error_parsing() -> None: caused_by=None, ) - assert RpcError.from_mtproto_error( + assert RpcError._from_mtproto_error( GeneratedRpcError( error_code=420, error_message="FLOOD_WAIT_31", @@ -31,7 +31,7 @@ def test_rpc_error_parsing() -> None: caused_by=None, ) - assert RpcError.from_mtproto_error( + assert RpcError._from_mtproto_error( GeneratedRpcError( error_code=500, error_message="INTERDC_2_CALL_ERROR",