@azure-tools/typespec-python 0.30.0 → 0.31.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/scripts/eng/lint.js.map +1 -1
- package/dist/scripts/eng/regenerate.js +0 -6
- package/dist/scripts/eng/regenerate.js.map +1 -1
- package/dist/scripts/eng/run-tests.js +16 -7
- package/dist/scripts/eng/run-tests.js.map +1 -1
- package/dist/scripts/eng/utils.js.map +1 -1
- package/dist/scripts/system-requirements.d.ts.map +1 -1
- package/dist/scripts/system-requirements.js.map +1 -1
- package/dist/src/code-model.js.map +1 -1
- package/dist/src/emitter.d.ts.map +1 -1
- package/dist/src/emitter.js +3 -1
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/external-process.d.ts +0 -1
- package/dist/src/external-process.d.ts.map +1 -1
- package/dist/src/http.js.map +1 -1
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +77 -4
- package/dist/src/types.js.map +1 -1
- package/dist/src/utils.d.ts.map +1 -1
- package/dist/src/utils.js +60 -5
- package/dist/src/utils.js.map +1 -1
- package/generator/pygen/codegen/models/operation.py +13 -2
- package/generator/pygen/codegen/models/parameter.py +5 -1
- package/generator/pygen/codegen/models/primitive_types.py +2 -1
- package/generator/pygen/codegen/models/property.py +5 -1
- package/generator/pygen/codegen/serializers/builder_serializer.py +15 -10
- package/generator/pygen/codegen/serializers/model_serializer.py +14 -4
- package/generator/pygen/codegen/serializers/test_serializer.py +6 -0
- package/generator/pygen/codegen/templates/model_base.py.jinja2 +315 -69
- package/generator/pygen/codegen/templates/model_dpg.py.jinja2 +6 -1
- package/generator/pygen/codegen/templates/serialization.py.jinja2 +271 -162
- package/generator/pygen/codegen/templates/test.py.jinja2 +2 -2
- package/generator/pygen/utils.py +9 -0
- package/package.json +6 -5
- package/scripts/__pycache__/venvtools.cpython-310.pyc +0 -0
- package/scripts/eng/regenerate.ts +0 -6
- package/scripts/eng/run-tests.ts +17 -7
package/dist/src/utils.js
CHANGED
|
@@ -1,13 +1,68 @@
|
|
|
1
1
|
import { getSimpleTypeResult, getType } from "./types.js";
|
|
2
2
|
import { getNamespaceFullName } from "@typespec/compiler";
|
|
3
|
+
function IsFullyUpperCase(identifier, maxUppercasePreserve) {
|
|
4
|
+
const len = identifier.length;
|
|
5
|
+
if (len > 1) {
|
|
6
|
+
if (len <= maxUppercasePreserve && identifier === identifier.toUpperCase()) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (len <= maxUppercasePreserve + 1 && identifier.endsWith("s")) {
|
|
10
|
+
const i = identifier.substring(0, len - 1);
|
|
11
|
+
if (i.toUpperCase() === i) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
function deconstruct(identifier, maxUppercasePreserve) {
|
|
19
|
+
if (Array.isArray(identifier)) {
|
|
20
|
+
return [...identifier.flatMap((each) => deconstruct(each, maxUppercasePreserve))];
|
|
21
|
+
}
|
|
22
|
+
return `${identifier}`
|
|
23
|
+
.replace(/([a-z]+)([A-Z])/g, "$1 $2") // Add a space in between camelCase words(e.g. fooBar => foo Bar)
|
|
24
|
+
.replace(/(\d+)/g, " $1 ") // Adds a space after numbers(e.g. foo123 => foo123 bar)
|
|
25
|
+
.replace(/\b([A-Z]+)([A-Z])s([^a-z])(.*)/g, "$1$2« $3$4") // Add a space after a plural uppper cased word(e.g. MBsFoo => MBs Foo)
|
|
26
|
+
.replace(/\b([A-Z]+)([A-Z])([a-z]+)/g, "$1 $2$3") // Add a space between an upper case word(2 char+) and the last captial case.(e.g. SQLConnection -> SQL Connection)
|
|
27
|
+
.replace(/«/g, "s")
|
|
28
|
+
.trim()
|
|
29
|
+
.split(/[\W|_]+/)
|
|
30
|
+
.map((each) => (IsFullyUpperCase(each, maxUppercasePreserve) ? each : each.toLowerCase()));
|
|
31
|
+
}
|
|
32
|
+
function isEqual(s1, s2) {
|
|
33
|
+
// when s2 is undefined and s1 is the string 'undefined', it returns 0, making this true.
|
|
34
|
+
// To prevent that, first we need to check if s2 is undefined.
|
|
35
|
+
return s2 !== undefined && !!s1 && !s1.localeCompare(s2, undefined, { sensitivity: "base" });
|
|
36
|
+
}
|
|
37
|
+
function removeSequentialDuplicates(identifier) {
|
|
38
|
+
const ids = [...identifier].filter((each) => !!each);
|
|
39
|
+
for (let i = 0; i < ids.length; i++) {
|
|
40
|
+
while (isEqual(ids[i], ids[i - 1])) {
|
|
41
|
+
ids.splice(i, 1);
|
|
42
|
+
}
|
|
43
|
+
while (isEqual(ids[i], ids[i - 2]) && isEqual(ids[i + 1], ids[i - 1])) {
|
|
44
|
+
ids.splice(i, 2);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return ids;
|
|
48
|
+
}
|
|
49
|
+
function normalize(identifier, removeDuplicates = true, maxUppercasePreserve = 0) {
|
|
50
|
+
if (!identifier || identifier.length === 0) {
|
|
51
|
+
return [""];
|
|
52
|
+
}
|
|
53
|
+
return typeof identifier === "string"
|
|
54
|
+
? normalize(deconstruct(identifier, maxUppercasePreserve), removeDuplicates, maxUppercasePreserve)
|
|
55
|
+
: removeDuplicates
|
|
56
|
+
? removeSequentialDuplicates(identifier)
|
|
57
|
+
: identifier;
|
|
58
|
+
}
|
|
3
59
|
export function camelToSnakeCase(name) {
|
|
4
60
|
if (!name)
|
|
5
61
|
return name;
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return camelToSnakeCaseRe(name[0].toLowerCase() + name.slice(1));
|
|
62
|
+
const words = normalize(name, false, 6);
|
|
63
|
+
const result = words.join("_").toLowerCase();
|
|
64
|
+
const result_final = result.replace(/([^\d])_(\d+)/g, "$1$2");
|
|
65
|
+
return result_final;
|
|
11
66
|
}
|
|
12
67
|
export function removeUnderscoresFromNamespace(name) {
|
|
13
68
|
// needed because of the _specs_ tests
|
package/dist/src/utils.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG1D,MAAM,UAAU,
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,mBAAmB,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,oBAAoB,CAAC;AAG1D,SAAS,gBAAgB,CAAC,UAAkB,EAAE,oBAA4B;IACtE,MAAM,GAAG,GAAG,UAAU,CAAC,MAAM,CAAC;IAC9B,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;QACV,IAAI,GAAG,IAAI,oBAAoB,IAAI,UAAU,KAAK,UAAU,CAAC,WAAW,EAAE,EAAE,CAAC;YACzE,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,IAAI,GAAG,IAAI,oBAAoB,GAAG,CAAC,IAAI,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YAC9D,MAAM,CAAC,GAAG,UAAU,CAAC,SAAS,CAAC,CAAC,EAAE,GAAG,GAAG,CAAC,CAAC,CAAC;YAC3C,IAAI,CAAC,CAAC,WAAW,EAAE,KAAK,CAAC,EAAE,CAAC;gBACxB,OAAO,IAAI,CAAC;YAChB,CAAC;QACL,CAAC;IACL,CAAC;IACD,OAAO,KAAK,CAAC;AACjB,CAAC;AAED,SAAS,WAAW,CAAC,UAAkC,EAAE,oBAA4B;IACjF,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,EAAE,CAAC;QAC5B,OAAO,CAAC,GAAG,UAAU,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,WAAW,CAAC,IAAI,EAAE,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AC,KAAK,CAAC;IACxB,IAAI,CAAC,OAAO;QAAE,OAAO,KAAK,CAAC;IAC3B,OAAO,CACH,OAAO,CAAC,IAAI,KAAK,OAAO;QACxB,OAAO,CAAC,SAAS,KAAK,SAAS;QAC/B,CAAC,YAAY,EAAE,wBAAwB,CAAC,CAAC,QAAQ,CAAC,oBAAoB,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QAC1F,OAAO,CAAC,IAAI,KAAK,eAAe,CACnC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,wBAAwB,CACpC,MAAoC;IAEpC,IAAI,MAAM,CAAC,OAAO,EAAE,CAAC;QACjB,OAAO;YACH,WAAW,EAAE,MAAM,CAAC,OAAO;YAC3B,OAAO,EAAE,MAAM,CAAC,WAAW;SAC9B,CAAC;IACN,CAAC;IACD,OAAO;QACH,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,EAAE;KACxC,CAAC;AACN,CAAC;AAED,MAAM,UAAU,UAAU,CAAC,IAAY;IACnC,OAAO,IAAI,CAAC,CAAC,CAAC,CAAC,WAAW,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACjD,CAAC"}
|
|
@@ -38,6 +38,7 @@ from .parameter_list import ParameterList
|
|
|
38
38
|
from .model_type import ModelType
|
|
39
39
|
from .base import BaseType
|
|
40
40
|
from .request_builder import OverloadedRequestBuilder, RequestBuilder
|
|
41
|
+
from ...utils import xml_serializable, json_serializable
|
|
41
42
|
|
|
42
43
|
if TYPE_CHECKING:
|
|
43
44
|
from .code_model import CodeModel
|
|
@@ -422,14 +423,24 @@ class OperationBase( # pylint: disable=too-many-public-methods,too-many-instanc
|
|
|
422
423
|
if self.parameters.has_body:
|
|
423
424
|
if self.has_form_data_body:
|
|
424
425
|
file_import.add_submodule_import(relative_path, "_model_base", ImportType.LOCAL)
|
|
425
|
-
|
|
426
|
+
elif xml_serializable(self.parameters.body_parameter.default_content_type):
|
|
427
|
+
file_import.add_submodule_import(
|
|
428
|
+
f"{relative_path}_model_base",
|
|
429
|
+
"_get_element",
|
|
430
|
+
ImportType.LOCAL,
|
|
431
|
+
)
|
|
432
|
+
elif json_serializable(self.parameters.body_parameter.default_content_type):
|
|
426
433
|
file_import.add_submodule_import(
|
|
427
434
|
f"{relative_path}_model_base",
|
|
428
435
|
"SdkJSONEncoder",
|
|
429
436
|
ImportType.LOCAL,
|
|
430
437
|
)
|
|
431
438
|
file_import.add_import("json", ImportType.STDLIB)
|
|
432
|
-
if
|
|
439
|
+
if any(xml_serializable(str(r.default_content_type)) for r in self.responses):
|
|
440
|
+
file_import.add_submodule_import(f"{relative_path}_model_base", "_deserialize_xml", ImportType.LOCAL)
|
|
441
|
+
elif any(r.type for r in self.responses):
|
|
442
|
+
file_import.add_submodule_import(f"{relative_path}_model_base", "_deserialize", ImportType.LOCAL)
|
|
443
|
+
if self.default_error_deserialization or self.non_default_errors:
|
|
433
444
|
file_import.add_submodule_import(f"{relative_path}_model_base", "_deserialize", ImportType.LOCAL)
|
|
434
445
|
return file_import
|
|
435
446
|
|
|
@@ -232,7 +232,11 @@ class BodyParameter(_ParameterBase):
|
|
|
232
232
|
def is_form_data(self) -> bool:
|
|
233
233
|
# hacky, but rn in legacy, there is no formdata model type, it's just a dict
|
|
234
234
|
# with all of the entries splatted out
|
|
235
|
-
return
|
|
235
|
+
return (
|
|
236
|
+
self.type.is_form_data
|
|
237
|
+
or bool(self.entries)
|
|
238
|
+
or ("multipart/form-data" in self.content_types and self.code_model.options["from_typespec"])
|
|
239
|
+
)
|
|
236
240
|
|
|
237
241
|
@property
|
|
238
242
|
def is_partial_body(self) -> bool:
|
|
@@ -611,6 +611,7 @@ class SdkCoreType(PrimitiveType):
|
|
|
611
611
|
def __init__(self, yaml_data: Dict[str, Any], code_model: "CodeModel") -> None:
|
|
612
612
|
super().__init__(yaml_data=yaml_data, code_model=code_model)
|
|
613
613
|
self.name = yaml_data.get("name", "")
|
|
614
|
+
self.submodule = yaml_data.get("submodule", "")
|
|
614
615
|
|
|
615
616
|
def docstring_type(self, **kwargs: Any) -> str:
|
|
616
617
|
return f"~{self.code_model.core_library}.{self.type_annotation(**kwargs)}"
|
|
@@ -620,7 +621,7 @@ class SdkCoreType(PrimitiveType):
|
|
|
620
621
|
|
|
621
622
|
def imports(self, **kwargs: Any) -> FileImport:
|
|
622
623
|
file_import = super().imports(**kwargs)
|
|
623
|
-
file_import.add_submodule_import(
|
|
624
|
+
file_import.add_submodule_import(self.submodule, self.name, ImportType.SDKCORE)
|
|
624
625
|
return file_import
|
|
625
626
|
|
|
626
627
|
@property
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# Licensed under the MIT License. See License.txt in the project root for
|
|
4
4
|
# license information.
|
|
5
5
|
# --------------------------------------------------------------------------
|
|
6
|
-
from typing import Any, Dict, Optional, TYPE_CHECKING, List, cast
|
|
6
|
+
from typing import Any, Dict, Optional, TYPE_CHECKING, List, cast, Union
|
|
7
7
|
|
|
8
8
|
from .base import BaseModel
|
|
9
9
|
from .constant_type import ConstantType
|
|
@@ -96,6 +96,10 @@ class Property(BaseModel): # pylint: disable=too-many-instance-attributes
|
|
|
96
96
|
return self.is_polymorphic and self.client_default_value is None
|
|
97
97
|
return self.is_discriminator and self.is_polymorphic and cast(ConstantType, self.type).value is None
|
|
98
98
|
|
|
99
|
+
@property
|
|
100
|
+
def xml_metadata(self) -> Optional[Dict[str, Union[str, bool]]]:
|
|
101
|
+
return self.yaml_data.get("xmlMetadata")
|
|
102
|
+
|
|
99
103
|
def type_annotation(self, *, is_operation_file: bool = False) -> str:
|
|
100
104
|
if self.is_base_discriminator:
|
|
101
105
|
return "str"
|
|
@@ -35,7 +35,7 @@ from ..models import (
|
|
|
35
35
|
from .parameter_serializer import ParameterSerializer, PopKwargType
|
|
36
36
|
from ..models.parameter_list import ParameterType
|
|
37
37
|
from . import utils
|
|
38
|
-
from ...utils import
|
|
38
|
+
from ...utils import xml_serializable, json_serializable
|
|
39
39
|
|
|
40
40
|
T = TypeVar("T")
|
|
41
41
|
OrderedSet = Dict[T, None]
|
|
@@ -61,10 +61,6 @@ def _all_same(data: List[List[str]]) -> bool:
|
|
|
61
61
|
return len(data) > 1 and all(sorted(data[0]) == sorted(data[i]) for i in range(1, len(data)))
|
|
62
62
|
|
|
63
63
|
|
|
64
|
-
def _json_serializable(content_type: str) -> bool:
|
|
65
|
-
return bool(JSON_REGEXP.match(content_type.split(";")[0].strip().lower()))
|
|
66
|
-
|
|
67
|
-
|
|
68
64
|
def _need_type_ignore(builder: OperationType) -> bool:
|
|
69
65
|
for excep in builder.non_default_errors:
|
|
70
66
|
for status_code in excep.status_codes:
|
|
@@ -413,7 +409,9 @@ class RequestBuilderSerializer(_BuilderBaseSerializer[RequestBuilderType]):
|
|
|
413
409
|
return "response.json()"
|
|
414
410
|
|
|
415
411
|
@staticmethod
|
|
416
|
-
def declare_non_inputtable_headers_queries(
|
|
412
|
+
def declare_non_inputtable_headers_queries(
|
|
413
|
+
builder: RequestBuilderType,
|
|
414
|
+
) -> List[str]:
|
|
417
415
|
def _get_value(param):
|
|
418
416
|
declaration = param.get_declaration() if param.constant else None
|
|
419
417
|
if param.location in [ParameterLocation.HEADER, ParameterLocation.QUERY]:
|
|
@@ -688,7 +686,7 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
688
686
|
f"'{body_param.type.serialization_type}'{is_xml_cmd}{serialization_ctxt_cmd})"
|
|
689
687
|
)
|
|
690
688
|
elif self.code_model.options["models_mode"] == "dpg":
|
|
691
|
-
if
|
|
689
|
+
if json_serializable(body_param.default_content_type):
|
|
692
690
|
if hasattr(body_param.type, "encode") and body_param.type.encode: # type: ignore
|
|
693
691
|
create_body_call = (
|
|
694
692
|
f"_{body_kwarg_name} = json.dumps({body_param.client_name}, "
|
|
@@ -700,6 +698,8 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
700
698
|
f"_{body_kwarg_name} = json.dumps({body_param.client_name}, "
|
|
701
699
|
"cls=SdkJSONEncoder, exclude_readonly=True) # type: ignore"
|
|
702
700
|
)
|
|
701
|
+
elif xml_serializable(body_param.default_content_type):
|
|
702
|
+
create_body_call = f"_{body_kwarg_name} = _get_element({body_param.client_name})"
|
|
703
703
|
else:
|
|
704
704
|
create_body_call = f"_{body_kwarg_name} = {body_param.client_name}"
|
|
705
705
|
else:
|
|
@@ -961,8 +961,11 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
961
961
|
and response.default_content_type == "application/json"
|
|
962
962
|
else ""
|
|
963
963
|
)
|
|
964
|
-
response_attr = "json" if
|
|
965
|
-
|
|
964
|
+
response_attr = "json" if json_serializable(str(response.default_content_type)) else "text"
|
|
965
|
+
deserialize_func = "_deserialize"
|
|
966
|
+
if xml_serializable(str(response.default_content_type)):
|
|
967
|
+
deserialize_func = "_deserialize_xml"
|
|
968
|
+
deserialize_code.append(f"deserialized = {deserialize_func}(")
|
|
966
969
|
deserialize_code.append(
|
|
967
970
|
f" {response.type.type_annotation(is_operation_file=True)},{pylint_disable}"
|
|
968
971
|
)
|
|
@@ -1086,7 +1089,9 @@ class _OperationSerializer(_BuilderBaseSerializer[OperationType]):
|
|
|
1086
1089
|
return retval
|
|
1087
1090
|
|
|
1088
1091
|
def error_map(self, builder: OperationType) -> List[str]:
|
|
1089
|
-
retval = [
|
|
1092
|
+
retval = [
|
|
1093
|
+
"error_map: MutableMapping[int, Type[HttpResponseError]] = { # pylint: disable=unsubscriptable-object"
|
|
1094
|
+
]
|
|
1090
1095
|
if builder.non_default_errors:
|
|
1091
1096
|
if not 401 in builder.non_default_error_status_codes:
|
|
1092
1097
|
retval.append(" 401: ClientAuthenticationError,")
|
|
@@ -251,12 +251,22 @@ class DpgModelSerializer(_ModelSerializer):
|
|
|
251
251
|
elif hasattr(prop.type, "encode") and prop.type.encode: # type: ignore
|
|
252
252
|
args.append(f'format="{prop.type.encode}"') # type: ignore
|
|
253
253
|
|
|
254
|
+
if prop.xml_metadata:
|
|
255
|
+
args.append(f"xml={prop.xml_metadata}")
|
|
256
|
+
|
|
254
257
|
field = "rest_discriminator" if prop.is_discriminator else "rest_field"
|
|
255
|
-
type_ignore =
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
258
|
+
type_ignore = (
|
|
259
|
+
" # type: ignore"
|
|
260
|
+
if prop.is_discriminator and isinstance(prop.type, (ConstantType, EnumValue)) and prop.type.value
|
|
261
|
+
else ""
|
|
262
|
+
)
|
|
263
|
+
generated_code = f'{prop.client_name}: {prop.type_annotation()} = {field}({", ".join(args)})'
|
|
264
|
+
pylint_disable = (
|
|
265
|
+
" # pylint: disable=line-too-long"
|
|
266
|
+
if len(generated_code) <= 120 < (len(generated_code) + len(type_ignore))
|
|
267
|
+
else ""
|
|
259
268
|
)
|
|
269
|
+
return f"{generated_code}{type_ignore}{pylint_disable}"
|
|
260
270
|
|
|
261
271
|
def initialize_properties(self, model: ModelType) -> List[str]:
|
|
262
272
|
init_args = []
|
|
@@ -80,6 +80,12 @@ class TestCase:
|
|
|
80
80
|
self.operation = operation
|
|
81
81
|
self.is_async = is_async
|
|
82
82
|
|
|
83
|
+
@property
|
|
84
|
+
def name(self) -> str:
|
|
85
|
+
if self.operation_groups[-1].is_mixin:
|
|
86
|
+
return self.operation.name
|
|
87
|
+
return "_".join([og.property_name for og in self.operation_groups] + [self.operation.name])
|
|
88
|
+
|
|
83
89
|
@property
|
|
84
90
|
def operation_group_prefix(self) -> str:
|
|
85
91
|
if self.operation_groups[-1].is_mixin:
|