File: fixmodule.py

package info (click to toggle)
mautrix-python 0.20.7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 1,812 kB
  • sloc: python: 19,103; makefile: 16
file content (52 lines) | stat: -rw-r--r-- 1,590 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# This is a script that fixes the __module__ tags in mautrix-python and some libraries.
# It's used to help Sphinx/autodoc figure out where the things are canonically imported from
# (by default, it shows the exact module they're defined in rather than the top-level import path).

from typing import NewType
from types import FunctionType, ModuleType

import aiohttp

from . import appservice, bridge, client, crypto, errors, types
from .crypto import attachments
from .util import async_db, config, db, formatter, logging


def _fix(obj: ModuleType) -> None:
    for item_name in getattr(obj, "__all__", None) or dir(obj):
        item = getattr(obj, item_name)
        if isinstance(item, (type, FunctionType, NewType)):
            # Ignore backwards-compatibility imports like the BridgeState import in mautrix.bridge
            if item.__module__.startswith("mautrix") and not item.__module__.startswith(
                obj.__name__
            ):
                continue

            item.__module__ = obj.__name__

            if isinstance(item, NewType):
                # By default autodoc makes a blank "Bases:" text,
                # so adjust it to show the type as the "base"
                item.__bases__ = (item.__supertype__,)
        # elif type(item).__module__ == "typing":
        #     print(obj.__name__, item_name, type(item))


_things_to_fix = [
    types,
    bridge,
    client,
    crypto,
    attachments,
    errors,
    appservice,
    async_db,
    config,
    db,
    formatter,
    logging,
    aiohttp,
]

for mod in _things_to_fix:
    _fix(mod)