[Python] Generate pyi stubfile for not importable C module

Discussão em 'Python' iniciado por Stack, Fevereiro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have a package A that uses a special package A.B, both are 3rd and 4th party packages written in C using Boost. Because it there is some special setup A.B is not included as a submodule but only as a some variable. Both import A.B and also first import A then import A.B fail with a ModuleNotFoundError.

    Only the following two ways work to access B.

    # OK
    import A
    A.B # -> <module: B>

    # OK
    from A import B
    B # -> <module: B>

    # ModuleNotFoundError
    import A.B


    B again is a nested package, e.g. with B.x.y.z, B.a.b, ...


    I would like to create stub files for A.B, however pyright --createstub or mypy's stubgen relies on import A.B and therefore fails.

    There exists a useful idea how to write a parser in this answer, however as I have to deal with properties, overloads, cross-imports extending it feels like reinventing the wheel and many cases need custom handling.

    I assume that mypy's stubgen --inspect-mode code could be modified or used with a debugger to provide the module directly instead of trying to import it and using it afterwards.

    This is where I am stuck. How can modify stubgen or any other stub generator to get it to work with such a module?


    EDIT: Further notes:

    after importing A, B and all submodules are recursively added to sys.modules without any dot-path, i.e. import A; import B works if performed in that order.

    Continue reading...

Compartilhe esta Página