Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Relative/Package imports throwing error #56

Copy link
Copy link
@andrewmiranda2001

Description

@andrewmiranda2001
Issue body actions

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!

snehalbaghel

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.