-
Notifications
You must be signed in to change notification settings - Fork 226
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
Python multi namespace #5375
base: main
Are you sure you want to change the base?
Python multi namespace #5375
Conversation
…hon-multi-namespace
…hon-multi-namespace
…hon-multi-namespace
No changes needing a change description found. |
You can try these changes here
|
packages/http-client-python/generator/pygen/codegen/models/code_model.py
Show resolved
Hide resolved
packages/http-client-python/generator/pygen/codegen/models/code_model.py
Outdated
Show resolved
Hide resolved
packages/http-client-python/generator/pygen/codegen/models/code_model.py
Show resolved
Hide resolved
…hon-multi-namespace
@@ -43,6 +44,7 @@ const EmitterOptionsSchema: JSONSchemaType<PythonEmitterOptions> = { | |||
debug: { type: "boolean", nullable: true }, | |||
flavor: { type: "string", nullable: true }, | |||
"examples-dir": { type: "string", nullable: true, format: "absolute-path" }, | |||
"enable-typespec-namespace": { type: "boolean", nullable: true }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can we add some comment for the new config? i believe we should do such clean up with the pr of consolidating emitter's config name.
const options = context.emitContext.options; | ||
if ([undefined, false].includes(options["enable-typespec-namespace"])) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
const options = context.emitContext.options; | |
if ([undefined, false].includes(options["enable-typespec-namespace"])) { | |
if ([undefined, false].includes(context.emitContext.options["enable-typespec-namespace"])) { |
"client/namespace": { | ||
"enable-typespec-namespace": "true", | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for unbranded. shall we always enable it by default? i heard java do that by default and for unbranded, it is reasonable.
|
||
def init_file_import(self) -> FileImport: | ||
return FileImport(self.code_model) | ||
|
||
@property | ||
def serialize_namespace(self) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a comment for this? sth like serialize_namespace
is the namespace of the serialized file. for example, when we serialize the async operation A
with Azure.Test.Foo
namespace, then the serialize_namespace
should be Azure.Test.Foo.aio._operations
.
self._operations_folder_name[client_namespace] = name | ||
return self._operations_folder_name[client_namespace] | ||
|
||
def get_serialize_namespace( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i prefer to move this function to base_serializer.py
because it is not related with the code model, but for the code serialization. the result for this function is depend on how we put the generated code. same for other helper like operations_folder_name
.
def _serialize_and_write_sample(self, env: Environment, namespace_path: Path): | ||
out_path = self._package_root_folder(namespace_path) / Path("generated_samples") | ||
def _serialize_and_write_sample(self, env: Environment, namespace: str): | ||
out_path = self.exec_path(namespace) / Path("generated_samples") |
There was a problem hiding this comment.
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 samples and tests files will be structured? it seems they all put in the corresponding namespace. but it's better to organize them with namespace structure under one generated_samples
.
self.code_model.for_test = True | ||
out_path = self._package_root_folder(namespace_path) / Path("generated_tests") | ||
out_path = self.exec_path(namespace) / Path("generated_tests") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same problem with samples.
client.imports( | ||
self.async_mode, | ||
serialize_namespace=self.serialize_namespace, | ||
serialize_namespace_type=NamespaceType.CLIENT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this serialize_namespace_type
used in imports
? to me, it should be used in calculating serialize_namespace
.
client.config.imports( | ||
self.async_mode, | ||
serialize_namespace=self.serialize_namespace, | ||
serialize_namespace_type=NamespaceType.CLIENT, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same issue.
For Azure/autorest.python#2795.
Compared to the previous logic that only supported a single namespace, this PR introduces support for multiple namespaces. This primarily requires updates in two key areas:
imports
function.Considering these two main changes, here's a detailed summary of the updates in this PR:
enable-typespec-namespace
, which lets users decide if they want to support native typespec namespace.clientNamespace
for models/operations/enums/namedUnions/clients: the emitter usesclientNamespace
to determine the exact location where the target type should be.get_relative_import_path
for CodeModel: all relative import paths should be calculated using this function.client_namespace_types
to CodeModel: it counts and stores all namespace types and information aboutclients/operation_groups/enums/models
in each namespace.client_namespace_types
property in CodeModel, we can cycle through each namespace and serialize its clients/models/enums/operations using the same logic.relative_path
andoperation
kwargs, which were not set from the top caller but from various functions during the calling process. The signatures were difficult to understand and maintain. I replaces them with the newserialize_namespace
andserialize_namespace_type
kwargs. Now, any function that needs to calculate relative imports can use these two parameters, which are set from the top caller, making them easier to understand and maintain.