Skip to content

[Python] generate metadata files #7359

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .chronus/changes/python-metadata-2025-4-15-18-12-43.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@typespec/http-client-python"
---

store apiVersion info in `_metadata.json`
3 changes: 3 additions & 0 deletions packages/http-client-python/emitter/src/code-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,5 +323,8 @@ export function emitCodeModel(sdkContext: PythonSdkContext) {
...simpleTypesMap.values(),
];
codeModel["crossLanguagePackageId"] = ignoreDiagnostics(getCrossLanguagePackageId(sdkContext));
if ((sdkContext.emitContext.options as any).flavor === "azure") {
Copy link
Member

Choose a reason for hiding this comment

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

Any test cases that we could verify this change?

Copy link
Contributor Author

@msyyc msyyc May 16, 2025

Choose a reason for hiding this comment

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

_metadata.json is not part of package so we can't test it by import then check like other cases. You can see the generated file in PR https://github.com/Azure/autorest.python/pull/3089/files#diff-3cd63a63f5c9e91001bdea81bb22b04fb20951ffae50393999131961cd61af6b

Copy link
Member

Choose a reason for hiding this comment

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

i see. So for python, we only include the value of api-version?

codeModel["metadata"] = sdkPackage.metadata;
}
return codeModel;
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def __init__(
self.has_subnamespace = False
self._operations_folder_name: Dict[str, str] = {}
self._relative_import_path: Dict[str, str] = {}
self.metadata: Dict[str, Any] = yaml_data.get("metadata", {})

@staticmethod
def get_imported_namespace_for_client(imported_namespace: str, async_mode: bool = False) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# license information.
# --------------------------------------------------------------------------
import logging
import json
from collections import namedtuple
import re
from typing import List, Any, Union
Expand Down Expand Up @@ -143,6 +144,13 @@ def serialize(self) -> None:
self._serialize_and_write_sample(env, namespace=client_namespace)
if self.code_model.options["generate_test"]:
self._serialize_and_write_test(env, namespace=client_namespace)

# add _metadata.json
if self.code_model.metadata:
self.write_file(
exec_path / Path("_metadata.json"),
json.dumps(self.code_model.metadata, indent=2),
)
elif client_namespace_type.clients:
# add clients folder if there are clients in this namespace
self._serialize_client_and_config_files(client_namespace, client_namespace_type.clients, env)
Expand Down
Loading