Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: internal
packages:
- "@typespec/http-client-python"
---

clean up namespace logic in python
2 changes: 1 addition & 1 deletion packages/http-client-python/emitter/src/code-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function emitOperationGroups<TServiceOperation extends SdkServiceOperation>(
// operation has same clientNamespace as the operation group
for (const og of operationGroups) {
for (const op of og.operations) {
op.clientNamespace = getClientNamespace(context, og.clientNamespace);
op.clientNamespace = og.clientNamespace;
}
}

Expand Down
29 changes: 13 additions & 16 deletions packages/http-client-python/emitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ export function capitalize(name: string): string {
return name[0].toUpperCase() + name.slice(1);
}

// Library namespaces that should not be used as client namespaces
const LIB_NAMESPACE = [
"azure.core",
"azure.resourcemanager",
Expand All @@ -272,29 +273,25 @@ const LIB_NAMESPACE = [
];

export function getRootNamespace(context: PythonSdkContext): string {
let rootNamespace = "";
if (context.sdkPackage.clients.length > 0) {
rootNamespace = context.sdkPackage.clients[0].namespace;
} else if (context.sdkPackage.models.length > 0) {
const result = context.sdkPackage.models
.map((model) => model.namespace)
.filter((namespace) => !LIB_NAMESPACE.includes(namespace));
if (result.length > 0) {
result.sort();
rootNamespace = result[0];
}
return context.sdkPackage.clients[0].namespace.toLowerCase();
} else if (context.sdkPackage.namespaces.length > 0) {
rootNamespace = context.sdkPackage.namespaces[0].fullName;
return context.sdkPackage.namespaces[0].fullName.toLowerCase();
}
return "";
}

return rootNamespace.toLowerCase();
function isLibraryNamespace(namespace: string): boolean {
const ns = namespace.toLowerCase();
return LIB_NAMESPACE.some((lib) => ns.startsWith(lib));
}

export function getClientNamespace(context: PythonSdkContext, clientNamespace: string) {
if (
clientNamespace === "" ||
LIB_NAMESPACE.some((item) => clientNamespace.toLowerCase().startsWith(item))
) {
// Namespace precedence: @clientNamespace > --namespace > original namespace
// These are resolved by TCGC and passed in as clientNamespace.
// However, models from library namespaces (azure.core, azure.resourcemanager, etc.)
// should use the SDK's root namespace instead.
if (clientNamespace === "" || isLibraryNamespace(clientNamespace)) {
return getRootNamespace(context);
}
return clientNamespace.toLowerCase();
Expand Down
Loading