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

[python] always respect typespec namesapce even if package-name is set #5649

Open
wants to merge 10 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
9 changes: 2 additions & 7 deletions packages/http-client-python/emitter/src/code-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ import {
simpleTypesMap,
typesMap,
} from "./types.js";
import {
emitParamBase,
getClientNamespace,
getImplementation,
removeUnderscoresFromNamespace,
} from "./utils.js";
import { emitParamBase, getClientNamespace, getImplementation, getRootNamespace } from "./utils.js";

function emitBasicMethod<TServiceOperation extends SdkServiceOperation>(
context: PythonSdkContext<TServiceOperation>,
Expand Down Expand Up @@ -281,7 +276,7 @@ export function emitCodeModel<TServiceOperation extends SdkServiceOperation>(
// Get types
const sdkPackage = sdkContext.sdkPackage;
const codeModel: Record<string, any> = {
namespace: removeUnderscoresFromNamespace(sdkPackage.rootNamespace).toLowerCase(),
namespace: getRootNamespace(sdkContext),
clients: [],
};
for (const client of sdkPackage.clients) {
Expand Down
50 changes: 37 additions & 13 deletions packages/http-client-python/emitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,26 +200,50 @@ export function capitalize(name: string): string {
return name[0].toUpperCase() + name.slice(1);
}

const LIB_NAMESPACE = [
"azure.core",
"azure.resourcemanager",
"azure.clientgenerator.core",
"typespec.rest",
"typespec.http",
"typespec.versioning",
];

function getRootTypespecNamespace(context: PythonSdkContext<SdkServiceOperation>): string {
if (context.sdkPackage.clients.length > 0) {
return context.sdkPackage.clients[0].clientNamespace;
}
if (context.sdkPackage.models.length > 0) {
const result = context.sdkPackage.models
.map((model) => model.clientNamespace)
.filter((namespace) => !LIB_NAMESPACE.includes(namespace));
if (result.length > 0) {
result.sort();
return result[0];
}
}
if (context.sdkPackage.namespaces.length > 0) {
return context.sdkPackage.namespaces[0].fullName;
}
return "";
}

export function getRootNamespace(context: PythonSdkContext<SdkServiceOperation>): string {
const rootNamespace = context.emitContext.options["enable-typespec-namespace"]
? getRootTypespecNamespace(context)
: context.sdkPackage.rootNamespace;
return removeUnderscoresFromNamespace(rootNamespace).toLowerCase();
}

export function getClientNamespace<TServiceOperation extends SdkServiceOperation>(
context: PythonSdkContext<TServiceOperation>,
clientNamespace: string,
) {
const rootNamespace = removeUnderscoresFromNamespace(
context.sdkPackage.rootNamespace,
).toLowerCase();
const rootNamespace = getRootNamespace(context);
if (!context.emitContext.options["enable-typespec-namespace"]) {
return rootNamespace;
}
if (
[
"azure.core",
"azure.resourcemanager",
"azure.clientgenerator.core",
"typespec.rest",
"typespec.http",
"typespec.versioning",
].some((item) => clientNamespace.toLowerCase().startsWith(item))
) {
if (LIB_NAMESPACE.some((item) => clientNamespace.toLowerCase().startsWith(item))) {
return rootNamespace;
}
return clientNamespace === ""
Expand Down
38 changes: 11 additions & 27 deletions packages/http-client-python/eng/scripts/ci/regenerate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,16 @@ const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<string, st
},
"type/model/visibility": [
{ "package-name": "typetest-model-visibility" },
{ "package-name": "headasbooleantrue", "head-as-boolean": "true" },
{ "package-name": "headasbooleanfalse", "head-as-boolean": "false" },
{
"package-name": "headasbooleantrue",
"head-as-boolean": "true",
"enable-typespec-namespace": "false",
},
{
"package-name": "headasbooleanfalse",
"head-as-boolean": "false",
"enable-typespec-namespace": "false",
},
],
"type/property/nullable": {
"package-name": "typetest-property-nullable",
Expand Down Expand Up @@ -123,9 +131,6 @@ const EMITTER_OPTIONS: Record<string, Record<string, string> | Record<string, st
"client/structure/two-operation-group": {
"package-name": "client-structure-twooperationgroup",
},
"client/namespace": {
"enable-typespec-namespace": "true",
},
};

function toPosix(dir: string): string {
Expand All @@ -139,28 +144,7 @@ function getEmitterOption(spec: string, flavor: string): Record<string, string>[
? relativeSpec
: dirname(relativeSpec);
const emitter_options = EMITTER_OPTIONS[key] || [{}];
const result = Array.isArray(emitter_options) ? emitter_options : [emitter_options];

function updateOptions(options: Record<string, string>): void {
if (options["package-name"] && options["enable-typespec-namespace"] === undefined) {
options["enable-typespec-namespace"] = "false";
}
}

// when package name is different with typespec namespace, disable typespec namespace
if (flavor !== "azure") {
for (const options of result) {
if (Array.isArray(options)) {
for (const option of options) {
updateOptions(option);
}
} else {
updateOptions(options);
}
}
}

return result;
return Array.isArray(emitter_options) ? emitter_options : [emitter_options];
}

// Function to execute CLI commands asynchronously
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ def _serialize_and_write_package_files(self, client_namespace: str) -> None:
env = Environment(
loader=PackageLoader("pygen.codegen", "templates/packaging_templates"),
undefined=StrictUndefined,
trim_blocks=True,
lstrip_blocks=True,
)

package_files = _PACKAGE_FILES
Expand Down Expand Up @@ -465,7 +467,10 @@ def _namespace_from_package_name(self) -> str:
return get_namespace_from_package_name(self.code_model.options["package_name"])

def _name_space(self) -> str:
if self.code_model.namespace.count(".") >= self._namespace_from_package_name.count("."):
if (
self.code_model.namespace.count(".") >= self._namespace_from_package_name.count(".")
or self.code_model.options["enable_typespec_namespace"]
):
return self.code_model.namespace

return self._namespace_from_package_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,28 @@
{{ license_header }}
# coding: utf-8
{% if package_mode %}

import os
import re
{% endif -%}
{% endif %}
from setuptools import setup, find_packages

{% set package_name = package_name or code_model.clients[0].name %}

PACKAGE_NAME = "{{ package_name|lower }}"
{% if package_mode -%}
{% if code_model.options["enable_typespec_namespace"] %}
PACKAGE_NAMESPACE = "{{ code_model.namespace|lower }}"
{% endif %}
{% if package_mode %}
PACKAGE_PPRINT_NAME = "{{ package_pprint_name }}"

{% if code_model.options["enable_typespec_namespace"] %}
# a.b.c => a/b/c
package_folder_path = PACKAGE_NAMESPACE.replace(".", "/")
{% else %}
# a-b-c => a/b/c
package_folder_path = PACKAGE_NAME.replace("-", "/")
{% endif %}

# Version extraction inspired from 'requests'
with open(os.path.join(package_folder_path, "_version.py"), "r") as fd:
Expand All @@ -33,7 +42,8 @@ version = "{{ package_version }}"
{% set long_description = code_model.description %}
{% set author_email = "" %}
{% set url = "" %}
{% endif -%}
{% endif %}


setup(
name=PACKAGE_NAME,
Expand Down Expand Up @@ -70,9 +80,9 @@ setup(
{% if pkgutil_names %}
# Exclude packages that will be covered by PEP420 or nspkg
{% endif %}
{%- for pkgutil_name in pkgutil_names %}
{% for pkgutil_name in pkgutil_names %}
"{{ pkgutil_name }}",
{%- endfor %}
{% endfor %}
]
),
include_package_data=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# --------------------------------------------------------------------------
import pytest
from typetest.enum.fixed import aio, models
from azure.core.exceptions import HttpResponseError


@pytest.fixture
Expand All @@ -20,8 +21,8 @@ async def test_known_value(client):


@pytest.mark.asyncio
async def test_unknown_value(client: aio.FixedClient, core_library):
async def test_unknown_value(client: aio.FixedClient):
try:
await client.string.put_unknown_value("Weekend")
except core_library.exceptions.HttpResponseError as err:
except HttpResponseError as err:
assert err.status_code == 500
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# --------------------------------------------------------------------------
import pytest
from typetest.enum.fixed import FixedClient, models
from azure.core.exceptions import HttpResponseError


@pytest.fixture
Expand All @@ -18,8 +19,8 @@ def test_known_value(client):
client.string.put_known_value(models.DaysOfWeekEnum.MONDAY)


def test_unknown_value(client: FixedClient, core_library):
def test_unknown_value(client: FixedClient):
try:
client.string.put_unknown_value("Weekend")
except core_library.exceptions.HttpResponseError as err:
except HttpResponseError as err:
assert err.status_code == 500
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

import pytest
import isodate
from type.array.aio import ArrayClient
from type.array import models


@pytest.fixture
async def client():
async with ArrayClient() as client:
yield client


@pytest.mark.asyncio
async def test_boolean_value(client: ArrayClient):
assert await client.boolean_value.get() == [True, False]
await client.boolean_value.put([True, False])


@pytest.mark.asyncio
async def test_datetime_value(client: ArrayClient):
assert await client.datetime_value.get() == [isodate.parse_datetime("2022-08-26T18:38:00Z")]
await client.datetime_value.put([isodate.parse_datetime("2022-08-26T18:38:00Z")])


@pytest.mark.asyncio
async def test_duration_value(client: ArrayClient):
assert await client.duration_value.get() == [isodate.parse_duration("P123DT22H14M12.011S")]
await client.duration_value.put([isodate.parse_duration("P123DT22H14M12.011S")])


@pytest.mark.asyncio
async def test_float32_value(client: ArrayClient):
assert await client.float32_value.get() == [43.125]
await client.float32_value.put([43.125])


@pytest.mark.asyncio
async def test_int32_value(client: ArrayClient):
assert await client.int32_value.get() == [1, 2]
await client.int32_value.put([1, 2])


@pytest.mark.asyncio
async def test_int64_value(client: ArrayClient):
assert await client.int64_value.get() == [2**53 - 1, -(2**53 - 1)]
await client.int64_value.put([2**53 - 1, -(2**53 - 1)])


@pytest.mark.asyncio
async def test_model_value(client: ArrayClient):
assert await client.model_value.get() == [
models.InnerModel(property="hello"),
models.InnerModel(property="world"),
]
await client.model_value.put(
[
models.InnerModel(property="hello"),
models.InnerModel(property="world"),
]
)


@pytest.mark.asyncio
async def test_nullable_boolean_value(client: ArrayClient):
assert await client.nullable_boolean_value.get() == [True, None, False]
await client.nullable_boolean_value.put([True, None, False])


@pytest.mark.asyncio
async def test_nullable_float_value(client: ArrayClient):
assert await client.nullable_float_value.get() == [1.25, None, 3.0]
await client.nullable_float_value.put([1.25, None, 3.0])


@pytest.mark.asyncio
async def test_nullable_int32_value(client: ArrayClient):
assert await client.nullable_int32_value.get() == [1, None, 3]
await client.nullable_int32_value.put([1, None, 3])


@pytest.mark.asyncio
async def test_nullable_model_value(client: ArrayClient):
assert await client.nullable_model_value.get() == [
models.InnerModel(property="hello"),
None,
models.InnerModel(property="world"),
]
await client.nullable_model_value.put(
[
models.InnerModel(property="hello"),
None,
models.InnerModel(property="world"),
]
)


@pytest.mark.asyncio
async def test_nullable_string_value(client: ArrayClient):
assert await client.nullable_string_value.get() == ["hello", None, "world"]
await client.nullable_string_value.put(["hello", None, "world"])


@pytest.mark.asyncio
async def test_string_value(client: ArrayClient):
assert await client.string_value.get() == ["hello", ""]
await client.string_value.put(["hello", ""])


@pytest.mark.asyncio
async def test_unknown_value(client: ArrayClient):
assert await client.unknown_value.get() == [1, "hello", None]
await client.unknown_value.put([1, "hello", None])
Loading
Loading