Skip to content

Refactor Platforms/WASI/__main__.py for lazy importing and future new subcommands#145404

Open
brettcannon wants to merge 9 commits intopython:mainfrom
brettcannon:wasi-refactor
Open

Refactor Platforms/WASI/__main__.py for lazy importing and future new subcommands#145404
brettcannon wants to merge 9 commits intopython:mainfrom
brettcannon:wasi-refactor

Conversation

@brettcannon
Copy link
Member

Everything related to the build subcommand & friends has been moved into a _build module which is now lazily imported in __main__ which now only handles argparse. This did require tweaking what the defaults are in the argument parser so that it didn't trigger needing to import _build. It also required changing dispatching to subcommands from a dict to a match statement to also avoid triggering the import of _build. As well, all imports in _build that are not needed for every subcommand has also been made lazy. Since neither PEP 8 or Ruff/Black have decided how to sort lazy imports, they are interleaved and imports are sorted by name as if the lazy aspect of the statement wasn't there.

This work also sets things up so that any future subcommands can go into their own module and also be lazily imported into __main__.

@hugovk
Copy link
Member

hugovk commented Mar 2, 2026

Since neither PEP 8 or Ruff/Black have decided how to sort lazy imports,

Ruff and isort issues:

they are interleaved and imports are sorted by name as if the lazy aspect of the statement wasn't there.

Although this PR is using __lazy_modules__ and not the lazy keyword.

brettcannon and others added 2 commits March 2, 2026 09:09
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
@hugovk
Copy link
Member

hugovk commented Mar 2, 2026

Now we have this duplicate:

      - id: ruff-check
        name: Run Ruff (lint) on Tools/build/
        args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
        files: ^Tools/build/
      - id: ruff-check
        name: Run Ruff (lint) on Tools/build/
        args: [--exit-non-zero-on-fix, --config=Tools/build/.ruff.toml]
        files: ^Tools/build/

@brettcannon
Copy link
Member Author

they are interleaved and imports are sorted by name as if the lazy aspect of the statement wasn't there.

Although this PR is using __lazy_modules__ and not the lazy keyword.

Correct! (The initial version did, but I realized backporting would not be fun with lazy, so I went with __lazy_modules__.)

Maybe that will help define what to do with lazy as sorting differently based on whether __lazy_modules__ or lazy is used might be a little too weird.



@subdir(lambda context: CROSS_BUILD_DIR / host_triple(context), clean_ok=True)
def configure_wasi_python(context, working_dir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the original version of this function, we had a guard to check if the context.wasi_sdk_path was set before calling wasi_sdk_env. Was it intentionally removed here?

"w",
encoding="utf-8",
delete=False,
dir=logdir,
Copy link
Member

@savannahostrowski savannahostrowski Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not new but I just noticed that logdir is unbound when context.logdir is not None.

@bedevere-app
Copy link

bedevere-app bot commented Mar 3, 2026

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants