|
1 | 1 | from enum import StrEnum |
2 | | -from typing import Annotated, TypedDict |
| 2 | +from typing import Annotated, NotRequired, TypedDict |
3 | 3 |
|
4 | 4 | import py_avro_schema as pas |
5 | 5 | from py_avro_schema._alias import Alias, register_type_alias |
@@ -103,13 +103,31 @@ class PyType(TypedDict, total=False): |
103 | 103 | "type": "record", |
104 | 104 | "name": "PyType", |
105 | 105 | "fields": [ |
106 | | - { |
107 | | - "name": "name", |
108 | | - "type": "string", |
109 | | - }, |
| 106 | + {"name": "name", "type": "string"}, |
110 | 107 | {"name": "nickname", "type": ["string", "null"]}, |
111 | 108 | {"name": "age", "type": ["long", "null", "string"]}, |
112 | 109 | {"name": "opt", "type": [{"namedString": "Opt", "type": "string"}, "null"]}, |
113 | 110 | ], |
114 | 111 | } |
115 | 112 | assert_schema(PyType, expected, options=pas.Option.MARK_NON_TOTAL_TYPED_DICTS) |
| 113 | + |
| 114 | + |
| 115 | +def test_non_required_keyword(): |
| 116 | + class PyType(TypedDict): |
| 117 | + name: str |
| 118 | + value: NotRequired[str] |
| 119 | + value_int: NotRequired[int] |
| 120 | + nullable_value: NotRequired[str | None] |
| 121 | + |
| 122 | + expected = { |
| 123 | + "type": "record", |
| 124 | + "name": "PyType", |
| 125 | + "fields": [ |
| 126 | + {"name": "name", "type": "string"}, |
| 127 | + {"name": "value", "type": "string"}, |
| 128 | + {"name": "value_int", "type": ["long", "string"]}, |
| 129 | + {"name": "nullable_value", "type": ["string", "null"]}, |
| 130 | + ], |
| 131 | + } |
| 132 | + |
| 133 | + assert_schema(PyType, expected, options=pas.Option.MARK_NON_TOTAL_TYPED_DICTS) |
0 commit comments