Ensure __all__ are consistent

This commit is contained in:
Lonami Exo 2023-11-02 12:43:06 +01:00
parent 2992a8e212
commit cea9fc6c77
7 changed files with 67 additions and 29 deletions

View File

@ -1,8 +1,4 @@
from .client import Client
from .net import Config
__all__ = [
"InlineResult",
"Client",
"Config",
]
__all__ = ["Client", "Config"]

View File

@ -4,8 +4,8 @@ from .markdown import parse as parse_markdown_message
from .markdown import unparse as generate_markdown_message
__all__ = [
"generate_html_message",
"parse_html_message",
"generate_markdown_message",
"generate_html_message",
"parse_markdown_message",
"generate_markdown_message",
]

View File

@ -85,5 +85,4 @@ __all__ = [
"SwitchInline",
"Text",
"Url",
"create",
]

View File

@ -80,13 +80,4 @@ def expand_peer(peer: abcs.Peer, *, broadcast: Optional[bool]) -> Chat:
raise RuntimeError("unexpected case")
__all__ = [
"Chat",
"ChatLike",
"Channel",
"Group",
"User",
"build_chat_map",
"peer_id",
"expand_peer",
]
__all__ = ["Channel", "Chat", "Group", "User"]

View File

@ -21,6 +21,7 @@ __all__ = [
"NO_UPDATES_TIMEOUT",
"USER_CHANNEL_DIFF_LIMIT",
"Gap",
"MessageBox",
"PossibleGap",
"PrematureEndReason",
"PtsInfo",
@ -30,7 +31,6 @@ __all__ = [
"Session",
"UpdateState",
"User",
"MessageBox",
"MemorySession",
"SqliteSession",
"Storage",

View File

@ -2,13 +2,4 @@ from . import abcs, functions, mtproto, types
from .core import Request
from .layer import LAYER, TYPE_MAPPING
__all__ = [
"abcs",
"core",
"functions",
"mtproto",
"types",
"Request",
"LAYER",
"TYPE_MAPPING",
]
__all__ = ['abcs', 'functions', 'mtproto', 'types', 'Request', 'LAYER', 'TYPE_MAPPING']

View File

@ -0,0 +1,61 @@
"""
Scan the `client/` directory for __init__.py files.
For every depth-1 import, add it, in order, to the __all__ variable.
"""
import ast
import os
import re
from pathlib import Path
from typing import List
class ImportVisitor(ast.NodeVisitor):
def __init__(self) -> None:
self.imported_names: List[str] = []
def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
if node.level == 1:
for name in node.names:
self.imported_names.append(name.asname or name.name)
def main() -> None:
impl_root = Path.cwd() / "client/src/telethon/_impl"
autogenerated_re = re.compile(
rf"(tl|mtproto){re.escape(os.path.sep)}(abcs|functions|types)"
)
files = []
for file in impl_root.rglob("__init__.py"):
file_str = str(file)
if autogenerated_re.search(file_str):
continue
files.append(file_str)
with file.open(encoding="utf-8") as fd:
contents = fd.read()
lines = contents.splitlines(True)
module = ast.parse(contents)
visitor = ImportVisitor()
visitor.visit(module)
for stmt in module.body:
match stmt:
case ast.Assign(targets=[ast.Name(id="__all__")], value=ast.List()):
# Not rewriting the AST to preserve comments and formatting.
lines[stmt.lineno - 1 : stmt.end_lineno] = [
"__all__ = ["
+ ", ".join(map(repr, visitor.imported_names))
+ "]\n"
]
break
with file.open("w", encoding="utf-8", newline="\n") as fd:
fd.writelines(lines)
if __name__ == "__main__":
main()