Reputation: 3947
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.
Upvotes: 0
Views: 90
Reputation: 3947
As the import structure is messy I fixed in manually before creating the stubs.
First, I manually wrote a little script that walks through the messy package and fixes sys.modules
on the fly to how it should be. e.g. sys.modules["A.B"] = B; del sys.modules["B"]
Secondly, I edited the entry point of the parser script, e.g. stubgen or mypy, to execute my fix script.
Third, I executed the stub generator normally.
Upvotes: 1