-
Notifications
You must be signed in to change notification settings - Fork 58
Description
Hello,
I have a nested package structure for all of my request/response types:
API/
client/
types/
generic/
...
request/
...
response/
...
At first, I had absolute imports such as from API.client.types.generic.crud import GenericType
which would fail:
ModuleNotFoundError: No module named 'API.client.types.generic'
So, I changed them to be relative imports simply for the sake of getting this automation up and running and got another error:
from ..generic.crud import GenericType
ImportError: attempted relative import beyond top-level package
This is the python script I wrote for reference:
from pydantic2ts import generate_typescript_defs
import os
API = os.path.join(os.path.dirname(__file__), "API\\client\\types")
FRONTEND = os.path.join(os.path.dirname(__file__), "frontend\\src\\api\\types")
COPY_DIRS = ['generic', 'request', 'response']
def main():
for dir in COPY_DIRS:
if not os.path.exists(os.path.join(FRONTEND, dir)):
os.makedirs(os.path.join(FRONTEND, dir))
for file in os.listdir(os.path.join(API, dir)):
if file.endswith(".py") and not file.startswith("__"):
output_file = os.path.join(FRONTEND, dir, file.replace(".py", ".ts"))
if not os.path.exists(output_file):
with open(output_file, 'w') as f:
pass
print(f"Generating {output_file}")
generate_typescript_defs(os.path.join(API, dir, file), output_file)
if __name__ == "__main__":
main()
Is there a way to skip over imports if they fail? Or to just write the relative imports even if it leaves the scope of the current transpilation? Because yes, the error is rightly being thrown, but if the relative imports were transpiled, by the end of the script they would be resolved anyways.
I saw you are working on some other fixes for multi-file generation, so I hope this is something that can be included in there. It isn't the best practice to have all of your requests and responses mushed into one file anyways.
Thank you!