Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support generating Python SDK with Pyodide #4784

Open
wants to merge 50 commits into
base: main
Choose a base branch
from

Conversation

YalinLi0312
Copy link
Member

No description provided.

@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label Oct 17, 2024
@azure-sdk
Copy link
Collaborator

No changes needing a change description found.

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 📚 Next docs

Copy link
Member

@iscai-msft iscai-msft left a comment

Choose a reason for hiding this comment

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

looking really good, thanks @YalinLi0312 !

try {
Write-Host 'Checking for differences in generated code...'
& "$packageRoot/eng/scripts/Check-GitChanges.ps1"
Write-Host 'Done. No code generation differences detected.'
Copy link
Member

Choose a reason for hiding this comment

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

have you made sure that this will catch differences in the generated code? You can try passing in an extra flag or something when generating with pyodide to make sure that any changes are caught

@@ -242,6 +245,9 @@ function addOptions(
const emitterConfigs: EmitterConfig[] = [];
for (const config of getEmitterOption(spec)) {
const options: Record<string, string> = { ...config };
if (flags.pyodide) {
Copy link
Member

Choose a reason for hiding this comment

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

nit: it might be better to keep the flags having the same name. I know we might not do this everywhere, but might as well when adding new flags

@@ -30,12 +30,12 @@
},
"scripts": {
"clean": "rimraf ./dist ./temp ./emitter/temp ./generator/test/azure/generated ./generator/test/unbranded/generated ./venv",
"build": "tsc -p ./emitter/tsconfig.build.json",
"build": "tsc -p ./emitter/tsconfig.build.json && tsx ./eng/scripts/setup/run-python3.ts ./eng/scripts/setup/build_pygen_wheel.py && rimraf ./venv_build_wheel",
Copy link
Member

Choose a reason for hiding this comment

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

nit: i would follow laurent's comments and clean up the package.json scripts as well in this pr

if (sdkContext.arm === true) {
commandArgs.push("--azure-arm=true");
}
if (resolvedOptions.flavor === "azure") {
Copy link
Contributor

Choose a reason for hiding this comment

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

The logic about options (e.g. "azure-arm"/"flavor") between python mode and pyodide mode is same so it is better to handle them in one central place.

Write-Host "Building project ..."
& npm run build

Write-Host "Regenerating project with Pyodide ..."
Copy link
Contributor

@msyyc msyyc Nov 20, 2024

Choose a reason for hiding this comment

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

I think there is no need to add new step in eng to run test twice with python and pyodide mode to make the CI time double than now. We can run azure test with python mode and unbranded test with pyodide mode. Then you just need to add use-pyodide: true here

await regenerate({ ...flags, flavor: "unbranded" });
.

Copy link
Member

@tadelesh tadelesh left a comment

Choose a reason for hiding this comment

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

some nit

resolvedOptions["package-pprint-name"] = `"${resolvedOptions["package-pprint-name"]}"`;
}
if (resolvedOptions["use-pyodide"]) {
const commandArgs: Record<string, string> = {};
Copy link
Member

Choose a reason for hiding this comment

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

the command args should be same whatever use pyodide or not, it's better to consolidate the logic.

Comment on lines +131 to +142
async def main():
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning)
from pygen import m2r, preprocess, codegen, black

m2r.M2R(output_folder=outputFolder, cadl_file=yamlRelativePath, **commandArgs).process()
preprocess.PreProcessPlugin(output_folder=outputFolder, cadl_file=yamlRelativePath, **commandArgs).process()
codegen.CodeGenerator(output_folder=outputFolder, cadl_file=yamlRelativePath, **commandArgs).process()
black.BlackScriptPlugin(output_folder=outputFolder, **commandArgs).process()

await main()
Copy link
Member

Choose a reason for hiding this comment

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

is pyodide able to run a py file? i just wondering if two paths could be consolidated. previously, we used py file. here, we use inline code.


await main()
`;
await pyodide.runPythonAsync(python, { globals });
Copy link
Member

Choose a reason for hiding this comment

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

how will pyodide deal with exception?

Comment on lines +153 to +154
await runPython3("./eng/scripts/setup/install.py");
await runPython3("./eng/scripts/setup/prepare.py");
Copy link
Member

Choose a reason for hiding this comment

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

i'm wondering how the exception be caught here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
emitter:client:python Issue for the Python client emitter: @typespec/http-client-python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants