diff --git a/.chronus/changes/python-nullable-type-annotations-2026-10-24.md b/.chronus/changes/python-nullable-type-annotations-2026-10-24.md new file mode 100644 index 00000000000..7c9489a92e8 --- /dev/null +++ b/.chronus/changes/python-nullable-type-annotations-2026-10-24.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Preserve nullability in Python type annotations for required model properties and array or dictionary elements, including nested collections. Required nullable properties remain required constructor arguments. diff --git a/packages/http-client-python/emitter/src/types.ts b/packages/http-client-python/emitter/src/types.ts index 1c56325afab..8ef4ea0e23f 100644 --- a/packages/http-client-python/emitter/src/types.ts +++ b/packages/http-client-python/emitter/src/types.ts @@ -434,6 +434,7 @@ function emitArrayOrDict( return getSimpleTypeResult(context, { type: kind, elementType: getType(context, type.valueType), + ...(type.valueType.kind === "nullable" ? { elementNullable: true } : {}), }); } diff --git a/packages/http-client-python/emitter/test/nullable-collections.test.ts b/packages/http-client-python/emitter/test/nullable-collections.test.ts new file mode 100644 index 00000000000..0d933df0d31 --- /dev/null +++ b/packages/http-client-python/emitter/test/nullable-collections.test.ts @@ -0,0 +1,69 @@ +import { expectDiagnostics } from "@typespec/compiler/testing"; +import { expect, it } from "vitest"; +import { emitCodeModel, type CodeModelType } from "./test-host.js"; + +interface ModelWithProperties extends CodeModelType { + properties: { clientName: string; type: CollectionType }[]; +} + +interface CollectionType extends CodeModelType { + elementType: CodeModelType; + elementNullable?: boolean; +} + +async function emitCollections(properties: string) { + const { codeModel, diagnostics } = await emitCodeModel(` + import "@typespec/http"; + using TypeSpec.Http; + @service namespace Test; + model Item { value: string; } + model Collections { ${properties} } + @post op echo(@body body: Collections): Collections; + `); + expectDiagnostics(diagnostics, []); + const model = codeModel.types.find((type) => type.name === "Collections") as ModelWithProperties; + expect(model).toBeDefined(); + return Object.fromEntries( + model.properties.map((property) => [property.clientName, property.type]), + ); +} + +it.each(["Item[]", "Record"])( + "preserves nullable elements without mutating shared types for %s", + async (plainType) => { + const nullableType = plainType === "Item[]" ? "(Item | null)[]" : "Record"; + const properties = await emitCollections(` + plain: ${plainType}; + nullable: ${nullableType}; + repeated: ${nullableType}; + `); + + expect(properties.plain.elementNullable).toBeUndefined(); + expect(properties.nullable.elementNullable).toBe(true); + expect(properties.nullable).not.toBe(properties.plain); + expect(properties.nullable).toBe(properties.repeated); + expect(properties.nullable.elementType).toBe(properties.plain.elementType); + expect(properties.plain.elementType).not.toHaveProperty("nullable"); + }, +); + +it("preserves nullability at each level of nested collections", async () => { + const properties = await emitCollections(` + arrays: ((string | null)[] | null)[]; + dictionaries: Record | null>; + mixed: Record<(string | null)[]>; + `); + + const nullableStrings = { type: "list", elementNullable: true, elementType: { type: "string" } }; + expect(properties.arrays).toEqual({ + type: "list", + elementNullable: true, + elementType: nullableStrings, + }); + expect(properties.dictionaries).toEqual({ + type: "dict", + elementNullable: true, + elementType: { type: "dict", elementNullable: true, elementType: { type: "string" } }, + }); + expect(properties.mixed).toEqual({ type: "dict", elementType: nullableStrings }); +}); diff --git a/packages/http-client-python/generator/pygen/codegen/models/dictionary_type.py b/packages/http-client-python/generator/pygen/codegen/models/dictionary_type.py index be2ba453483..6fe0db881d8 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/dictionary_type.py +++ b/packages/http-client-python/generator/pygen/codegen/models/dictionary_type.py @@ -5,7 +5,7 @@ # -------------------------------------------------------------------------- from typing import Any, Optional, TYPE_CHECKING from .base import BaseType -from .imports import FileImport +from .imports import FileImport, ImportType if TYPE_CHECKING: from .code_model import CodeModel @@ -29,6 +29,7 @@ def __init__( ) -> None: super().__init__(yaml_data=yaml_data, code_model=code_model) self.element_type = element_type + self.element_nullable: bool = yaml_data.get("elementNullable", False) @property def encode(self) -> Optional[str]: @@ -48,13 +49,19 @@ def type_annotation(self, **kwargs: Any) -> str: :return: The type annotation for this schema :rtype: str """ - return f"dict[str, {self.element_type.type_annotation(**kwargs)}]" + element_annotation = self.element_type.type_annotation(**kwargs) + if self.element_nullable: + element_annotation = f"Optional[{element_annotation}]" + return f"dict[str, {element_annotation}]" def description(self, *, is_operation_file: bool) -> str: return "" if is_operation_file else self.yaml_data.get("description", "") def docstring_text(self, **kwargs: Any) -> str: - return f"dict mapping str to {self.element_type.docstring_text(**kwargs)}" + element_text = self.element_type.docstring_text(**kwargs) + if self.element_nullable: + element_text = f"({element_text} or None)" + return f"dict mapping str to {element_text}" @property def xml_serialization_ctxt(self) -> Optional[str]: @@ -66,7 +73,10 @@ def docstring_type(self, **kwargs: Any) -> str: :param str namespace: Optional. The namespace for the models. """ - return f"dict[str, {self.element_type.docstring_type(**kwargs)}]" + element_docstring = self.element_type.docstring_type(**kwargs) + if self.element_nullable: + element_docstring = f"Optional[{element_docstring}]" + return f"dict[str, {element_docstring}]" def get_json_template_representation( self, @@ -114,6 +124,8 @@ def from_yaml(cls, yaml_data: dict[str, Any], code_model: "CodeModel") -> "Dicti def imports(self, **kwargs: Any) -> FileImport: file_import = FileImport(self.code_model) file_import.merge(self.element_type.imports(**kwargs)) + if self.element_nullable: + file_import.add_submodule_import("typing", "Optional", ImportType.STDLIB) return file_import @property diff --git a/packages/http-client-python/generator/pygen/codegen/models/list_type.py b/packages/http-client-python/generator/pygen/codegen/models/list_type.py index 368fb39816d..c5542751d00 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/list_type.py +++ b/packages/http-client-python/generator/pygen/codegen/models/list_type.py @@ -5,7 +5,7 @@ # -------------------------------------------------------------------------- from typing import Any, Optional, Union, TYPE_CHECKING from .base import BaseType -from .imports import FileImport +from .imports import FileImport, ImportType from .utils import NamespaceType if TYPE_CHECKING: @@ -22,6 +22,7 @@ def __init__( ) -> None: super().__init__(yaml_data=yaml_data, code_model=code_model) self.element_type = element_type + self.element_nullable: bool = yaml_data.get("elementNullable", False) self.max_items: Optional[int] = yaml_data.get("maxItems") self.min_items: Optional[int] = yaml_data.get("minItems") self.unique_items: bool = yaml_data.get("uniqueItems", False) @@ -54,7 +55,10 @@ def type_annotation(self, **kwargs: Any) -> str: list_type = "List" else: list_type = "list" - return f"{list_type}[{self.element_type.type_annotation(**kwargs)}]" + element_annotation = self.element_type.type_annotation(**kwargs) + if self.element_nullable: + element_annotation = f"Optional[{element_annotation}]" + return f"{list_type}[{element_annotation}]" def description(self, *, is_operation_file: bool) -> str: return "" if is_operation_file else self.yaml_data.get("description", "") @@ -85,13 +89,19 @@ def docstring_type(self, **kwargs: Any) -> str: if self.code_model.options["version-tolerant"] and self.element_type.xml_metadata: # this means we're version tolerant XML, we just return the XML element return self.element_type.docstring_type(**kwargs) - return f"list[{self.element_type.docstring_type(**kwargs)}]" + element_docstring = self.element_type.docstring_type(**kwargs) + if self.element_nullable: + element_docstring = f"Optional[{element_docstring}]" + return f"list[{element_docstring}]" def docstring_text(self, **kwargs: Any) -> str: if self.code_model.options["version-tolerant"] and self.element_type.xml_metadata: # this means we're version tolerant XML, we just return the XML element return self.element_type.docstring_text(**kwargs) - return f"list of {self.element_type.docstring_text(**kwargs)}" + element_text = self.element_type.docstring_text(**kwargs) + if self.element_nullable: + element_text = f"({element_text} or None)" + return f"list of {element_text}" @property def validation(self) -> Optional[dict[str, Union[bool, int, str]]]: @@ -143,6 +153,8 @@ def from_yaml(cls, yaml_data: dict[str, Any], code_model: "CodeModel") -> "ListT def imports(self, **kwargs: Any) -> FileImport: file_import = FileImport(self.code_model) file_import.merge(self.element_type.imports(**kwargs)) + if self.element_nullable: + file_import.add_submodule_import("typing", "Optional", ImportType.STDLIB) return file_import @property diff --git a/packages/http-client-python/generator/pygen/codegen/models/property.py b/packages/http-client-python/generator/pygen/codegen/models/property.py index d2a544c0511..b75289bd33c 100644 --- a/packages/http-client-python/generator/pygen/codegen/models/property.py +++ b/packages/http-client-python/generator/pygen/codegen/models/property.py @@ -117,7 +117,7 @@ def type_annotation(self, *, is_operation_file: bool = False, **kwargs: Any) -> if self.nullable: return f"Optional[{types_type_annotation}]" return types_type_annotation - if (self.optional and self.client_default_value is None) or self.readonly: + if self.nullable or (self.optional and self.client_default_value is None) or self.readonly: return f"Optional[{types_type_annotation}]" return types_type_annotation @@ -164,7 +164,7 @@ def imports(self, **kwargs) -> FileImport: # In TypedDict types.py, Optional means nullable if self.nullable: file_import.add_submodule_import("typing", "Optional", ImportType.STDLIB) - elif (self.optional and self.client_default_value is None) or self.readonly: + elif self.nullable or (self.optional and self.client_default_value is None) or self.readonly: file_import.add_submodule_import("typing", "Optional", ImportType.STDLIB) if self.code_model.options["models-mode"] == "dpg": if serialize_namespace_type != NamespaceType.TYPES_FILE: diff --git a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 index d34b069aed4..28a51a55e04 100644 --- a/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 +++ b/packages/http-client-python/generator/pygen/codegen/templates/model_base.py.jinja2 @@ -1219,15 +1219,20 @@ def _deserialize_sequence( if isinstance(obj, ET.Element): obj = list(obj) + # Optional elements still use the array's encoding; keep their wrapper for item decoding. + array_deserializer = deserializer + if isinstance(array_deserializer, functools.partial) and array_deserializer.func is _deserialize_with_optional: + array_deserializer = array_deserializer.args[0] + # encoded string may be deserialized to sequence - if isinstance(obj, str) and isinstance(deserializer, functools.partial): + if isinstance(obj, str) and isinstance(array_deserializer, functools.partial): # for list[str] - if _is_array_encoded_deserializer(deserializer): - return deserializer(obj) + if _is_array_encoded_deserializer(array_deserializer): + return array_deserializer(obj) # for list[Union[...]] - if isinstance(deserializer.args[0], list): - for sub_deserializer in deserializer.args[0]: + if isinstance(array_deserializer.args[0], list): + for sub_deserializer in array_deserializer.args[0]: if _is_array_encoded_deserializer(sub_deserializer): return sub_deserializer(obj) @@ -1244,6 +1249,8 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur annotation: typing.Any, module: typing.Optional[str], rf: typing.Optional["_RestField"] = None, + *, + is_field_type: bool = True, ) -> typing.Optional[typing.Callable[[typing.Any], typing.Any]]: if not annotation: return None @@ -1281,18 +1288,22 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur # is it optional? try: if any(a is _NONE_TYPE for a in annotation.__args__): # pyright: ignore - if rf: + # Nullable elements do not make the containing XML field optional. + if rf and is_field_type: rf._is_optional = True if len(annotation.__args__) <= 2: # pyright: ignore if_obj_deserializer = _get_deserialize_callable_from_annotation( - next(a for a in annotation.__args__ if a is not _NONE_TYPE), module, rf # pyright: ignore + next(a for a in annotation.__args__ if a is not _NONE_TYPE), module, rf, # pyright: ignore + is_field_type=is_field_type, ) return functools.partial(_deserialize_with_optional, if_obj_deserializer) # the type is Optional[Union[...]], we need to remove the None type from the Union annotation_copy = copy.copy(annotation) annotation_copy.__args__ = [a for a in annotation_copy.__args__ if a is not _NONE_TYPE] # pyright: ignore - return _get_deserialize_callable_from_annotation(annotation_copy, module, rf) + return _get_deserialize_callable_from_annotation( + annotation_copy, module, rf, is_field_type=is_field_type + ) except AttributeError: pass @@ -1300,7 +1311,7 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur if getattr(annotation, "__origin__", None) is typing.Union: # initial ordering is we make `string` the last deserialization option, because it is often them most generic deserializers = [ - _get_deserialize_callable_from_annotation(arg, module, rf) + _get_deserialize_callable_from_annotation(arg, module, rf, is_field_type=is_field_type) for arg in _sorted_annotations(annotation.__args__) # pyright: ignore ] @@ -1312,7 +1323,7 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur ) if annotation_name.lower() == "dict": value_deserializer = _get_deserialize_callable_from_annotation( - annotation.__args__[1], module, rf # pyright: ignore + annotation.__args__[1], module, rf, is_field_type=False # pyright: ignore ) return functools.partial( @@ -1329,12 +1340,12 @@ def _get_deserialize_callable_from_annotation( # pylint: disable=too-many-retur if annotation_name.lower() in ["list", "set", "tuple", "sequence"]: if len(annotation.__args__) > 1: # pyright: ignore entry_deserializers = [ - _get_deserialize_callable_from_annotation(dt, module, rf) + _get_deserialize_callable_from_annotation(dt, module, rf, is_field_type=False) for dt in annotation.__args__ # pyright: ignore ] return functools.partial(_deserialize_multiple_sequence, entry_deserializers, module) deserializer = _get_deserialize_callable_from_annotation( - annotation.__args__[0], module, rf # pyright: ignore + annotation.__args__[0], module, rf, is_field_type=False # pyright: ignore ) return functools.partial(_deserialize_sequence, deserializer, module) diff --git a/packages/http-client-python/tests/unit/test_nullable_collection_deserialization.py b/packages/http-client-python/tests/unit/test_nullable_collection_deserialization.py new file mode 100644 index 00000000000..69a9382ae45 --- /dev/null +++ b/packages/http-client-python/tests/unit/test_nullable_collection_deserialization.py @@ -0,0 +1,116 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +from typing import Optional, Union +import xml.etree.ElementTree as ET + +import pytest + +from specialwords._utils.model_base import Model, rest_field + +NullableList = Optional[list[Optional[str]]] +ListWithNullableItems = list[Optional[str]] + + +@pytest.mark.parametrize("use_field_plan", [False, True]) +def test_nullable_items_do_not_make_missing_xml_collections_optional(use_field_plan): + fields = { + "required": list[Optional[str]], + "nested": list[list[Optional[str]]], + "dictionaries": list[dict[str, Optional[str]]], + "optional": Optional[list[str]], + "both_nullable": Optional[list[Optional[str]]], + "nullable_alias": "NullableList", + "required_alias": "ListWithNullableItems", + } + model_type = type( + f"XmlCollections{use_field_plan}", + (Model,), + { + "__module__": __name__, + "__annotations__": fields, + "_xml": {"name": "Payload"} if use_field_plan else {}, + **{name: rest_field(xml={"name": name, "unwrapped": True}) for name in fields}, + }, + ) + + result = model_type(ET.fromstring("")) + + assert result.required == [] + assert result.nested == [] + assert result.dictionaries == [] + assert result.required_alias == [] + assert result.optional is None + assert result.both_nullable is None + assert result.nullable_alias is None + + +def test_nullable_collection_items_keep_field_format(): + class EncodedCollections(Model): + items: list[Optional[bytes]] = rest_field(format="base64url") + values: dict[str, Optional[bytes]] = rest_field(format="base64url") + nested: list[list[Optional[bytes]]] = rest_field(format="base64url") + + result = EncodedCollections( + {"items": [None, "_w"], "values": {"null": None, "data": "_w"}, "nested": [[None, "_w"]]} + ) + + assert result.items == [None, b"\xff"] + assert result.values == {"null": None, "data": b"\xff"} + assert result.nested == [[None, b"\xff"]] + + +class Child(Model): + value: str = rest_field() + + +def test_nullable_model_items_keep_mapping_deserialization(): + class Collections(Model): + items: list[Optional[Child]] = rest_field() + values: dict[str, Optional[Child]] = rest_field() + + result = Collections({"items": [None, {"value": "item"}], "values": {"null": None, "data": {"value": "entry"}}}) + + assert result["items"][0] is None + assert isinstance(result["items"][1], Child) + assert result["items"][1].value == "item" + assert result["values"]["null"] is None + assert isinstance(result["values"]["data"], Child) + assert result["values"]["data"].value == "entry" + + +@pytest.mark.parametrize( + "encoding,encoded", + [ + ("commaDelimited", "blue,green"), + ("pipeDelimited", "blue|green"), + ("spaceDelimited", "blue green"), + ("newlineDelimited", "blue\ngreen"), + ], +) +@pytest.mark.parametrize("item_type,kind", [(Optional[str], "String"), (Optional[Union[str, int]], "Union")]) +def test_delimited_nullable_items_deserialize_as_arrays(encoding, encoded, item_type, kind): + model_type = type( + f"EncodedNullable{encoding}{kind}", + (Model,), + { + "__module__": __name__, + "__annotations__": {"items": list[item_type], "nullable_items": Optional[list[item_type]]}, + "items": rest_field(format=encoding), + "nullable_items": rest_field(format=encoding), + }, + ) + + for value, expected in [(encoded, ["blue", "green"]), (["blue", "green"], ["blue", "green"]), ("", []), ([], [])]: + assert model_type({"items": value}).items == expected + assert model_type(items=value).items == expected + + explicit_null = model_type({"nullable_items": None}) + omitted = model_type(nullable_items=None) + assert explicit_null.nullable_items is None + assert "nullable_items" in explicit_null + assert omitted.nullable_items is None + assert "nullable_items" not in omitted diff --git a/packages/http-client-python/tests/unit/test_nullable_collections.py b/packages/http-client-python/tests/unit/test_nullable_collections.py new file mode 100644 index 00000000000..9e9e59777e9 --- /dev/null +++ b/packages/http-client-python/tests/unit/test_nullable_collections.py @@ -0,0 +1,124 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import pytest +from jinja2 import Environment, PackageLoader + +from pygen.codegen.models import CodeModel, Property, build_type +from pygen.codegen.models.utils import NamespaceType +from pygen.codegen.serializers.import_serializer import FileImportSerializer +from pygen.codegen.serializers.model_serializer import DpgModelSerializer +from pygen.codegen.serializers.types_serializer import TypesSerializer + + +@pytest.fixture +def code_model(): + return CodeModel( + { + "clients": [ + { + "name": "client", + "namespace": "example", + "moduleName": "example", + "parameters": [], + "url": "", + "operationGroups": [], + } + ], + "namespace": "example", + }, + options={ + "show-send-request": True, + "builders-visibility": "public", + "show-operations": True, + "models-mode": "dpg", + "version-tolerant": True, + "flavor": "unbranded", + "client-side-validation": False, + "keep-setup-py": False, + "basic-setup-py": False, + "no-namespace-folders": False, + "generation-subdir": None, + }, + ) + + +@pytest.mark.parametrize("kind, expected", [("list", "list[Optional[str]]"), ("dict", "dict[str, Optional[str]]")]) +def test_nullable_elements_in_annotations_and_docstrings(code_model, kind, expected): + collection = build_type({"type": kind, "elementNullable": True, "elementType": {"type": "string"}}, code_model) + + assert collection.type_annotation() == expected + assert collection.docstring_type() == expected + assert "None" in collection.docstring_text() + assert "from typing import Optional" in str(FileImportSerializer(collection.imports())) + + +@pytest.mark.parametrize( + "kind, expected, wire_type", [("list", "list[str]", "[str]"), ("dict", "dict[str, str]", "{str}")] +) +def test_nonnullable_collections_and_serialization_remain_unchanged(code_model, kind, expected, wire_type): + element = {"type": "string"} + plain = build_type({"type": kind, "elementType": element}, code_model) + nullable = build_type({"type": kind, "elementNullable": True, "elementType": element}, code_model) + + assert plain.type_annotation() == expected + assert plain.element_type is nullable.element_type + assert plain.serialization_type() == nullable.serialization_type() == wire_type + assert "Optional" not in str(FileImportSerializer(plain.imports())) + + +@pytest.mark.parametrize("kind, container", [("list", "list[{}]"), ("dict", "dict[str, {}]")]) +def test_nullable_model_elements_preserve_forward_references(code_model, kind, container): + collection = build_type( + { + "type": kind, + "elementNullable": True, + "elementType": { + "type": "model", + "base": "dpg", + "name": "Item", + "snakeCaseName": "item", + "properties": [], + }, + }, + code_model, + ) + + assert collection.type_annotation() == container.format('Optional["_models.Item"]') + assert collection.type_annotation(serialize_namespace_type=NamespaceType.TYPES_FILE) == container.format( + 'Optional["Item"]' + ) + assert collection.docstring_type() == container.format("Optional[~example.models.Item]") + + +@pytest.mark.parametrize( + "outer, outer_nullable, inner, expected, wire_type", + [ + ("list", True, "list", "list[Optional[list[Optional[str]]]]", "[[str]]"), + ("dict", True, "dict", "dict[str, Optional[dict[str, Optional[str]]]]", "{{str}}"), + ("dict", False, "list", "dict[str, list[Optional[str]]]", "{[str]}"), + ], +) +def test_nested_nullable_collections_render_in_models_and_typeddicts( + code_model, outer, outer_nullable, inner, expected, wire_type +): + collection = build_type( + { + "type": outer, + "elementNullable": outer_nullable, + "elementType": {"type": inner, "elementNullable": True, "elementType": {"type": "string"}}, + }, + code_model, + ) + prop = Property({"wireName": "values", "clientName": "values", "optional": False}, code_model, type=collection) + env = Environment(loader=PackageLoader("pygen.codegen", "templates")) + model_serializer = DpgModelSerializer(code_model, env, models=[]) + types_serializer = TypesSerializer(code_model, env, models=[]) + + assert model_serializer.declare_property(prop) == f"values: {expected} = rest_field()" + assert types_serializer.declare_property(prop, frozenset()) == f"values: Required[{expected}]" + assert collection.serialization_type() == wire_type + assert "from typing import Optional" in str(FileImportSerializer(prop.imports())) diff --git a/packages/http-client-python/tests/unit/test_nullable_properties.py b/packages/http-client-python/tests/unit/test_nullable_properties.py new file mode 100644 index 00000000000..5080eb6b87a --- /dev/null +++ b/packages/http-client-python/tests/unit/test_nullable_properties.py @@ -0,0 +1,99 @@ +# ------------------------------------------------------------------------- +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. See License.txt in the project root for +# license information. +# -------------------------------------------------------------------------- + +import ast + +from jinja2 import Environment, PackageLoader +import pytest + +from pygen.codegen.models import CodeModel, DPGModelType, build_type +from pygen.codegen.models.model_type import TypedDictModelType +from pygen.codegen.models.property import Property +from pygen.codegen.serializers.model_serializer import DpgModelSerializer +from pygen.codegen.serializers.types_serializer import TypesSerializer + + +def _render_model(optional: bool, nullable: bool) -> tuple[ast.Module, ast.Module]: + code_model = CodeModel( + { + "clients": [ + { + "name": "client", + "namespace": "sample", + "moduleName": "sample", + "parameters": [], + "url": "", + "operationGroups": [], + } + ], + "namespace": "sample", + }, + options={ + "show-send-request": True, + "builders-visibility": "public", + "show-operations": True, + "models-mode": "dpg", + "flavor": "unbranded", + "client-side-validation": False, + "keep-setup-py": False, + "basic-setup-py": False, + "no-namespace-folders": False, + "generation-subdir": None, + }, + ) + prop = Property( + yaml_data={ + "wireName": "value", + "clientName": "value", + "optional": optional, + "nullable": nullable, + }, + code_model=code_model, + type=build_type({"type": "string"}, code_model), + ) + model_data = {"name": "Payload", "type": "model", "snakeCaseName": "payload", "usage": 2} + model = DPGModelType(yaml_data=model_data, code_model=code_model, properties=[prop]) + input_model = TypedDictModelType(yaml_data=model_data, code_model=code_model, properties=[prop]) + env = Environment(loader=PackageLoader("pygen.codegen", "templates"), trim_blocks=True, lstrip_blocks=True) + model_source = DpgModelSerializer(code_model=code_model, env=env, models=[model]).serialize() + input_source = TypesSerializer(code_model=code_model, env=env, models=[input_model]).serialize() + return ast.parse(model_source), ast.parse(input_source) + + +@pytest.mark.parametrize( + "optional,nullable,model_annotation,input_annotation", + [ + (False, False, "str", "Required[str]"), + (False, True, "Optional[str]", "Required[Optional[str]]"), + (True, False, "Optional[str]", "str"), + (True, True, "Optional[str]", "Optional[str]"), + ], +) +def test_property_nullability_preserves_requiredness(optional, nullable, model_annotation, input_annotation): + model_module, input_module = _render_model(optional, nullable) + model = next(node for node in model_module.body if isinstance(node, ast.ClassDef)) + field = next(node for node in model.body if isinstance(node, ast.AnnAssign)) + assert ast.unparse(field.annotation) == model_annotation + + constructor = next(node for node in model.body if isinstance(node, ast.FunctionDef) and node.args.kwonlyargs) + assert ast.unparse(constructor.args.kwonlyargs[0].annotation) == model_annotation + default = constructor.args.kw_defaults[0] + if optional: + assert isinstance(default, ast.Constant) and default.value is None + else: + assert default is None + + if model_annotation == "Optional[str]": + assert any( + isinstance(node, ast.ImportFrom) + and node.module == "typing" + and any(alias.name == "Optional" for alias in node.names) + for node in model_module.body + ) + + input_model = next(node for node in input_module.body if isinstance(node, ast.ClassDef)) + input_field = next(node for node in input_model.body if isinstance(node, ast.AnnAssign)) + assert ast.unparse(input_field.annotation) == input_annotation