|
16 | 16 | IMPORT_DATETIME, |
17 | 17 | IMPORT_TIME, |
18 | 18 | IMPORT_TIMEDELTA, |
19 | | - IMPORT_UNION, |
20 | 19 | Import, |
21 | 20 | ) |
22 | 21 | from datamodel_code_generator.model import DataModel, DataModelFieldBase |
|
37 | 36 | from datamodel_code_generator.model.types import standard_primitive_type_map_factory, type_map_factory |
38 | 37 | from datamodel_code_generator.types import ( |
39 | 38 | NONE, |
40 | | - OPTIONAL_PREFIX, |
41 | | - UNION_DELIMITER, |
42 | 39 | UNION_OPERATOR_DELIMITER, |
43 | | - UNION_PREFIX, |
44 | 40 | DataType, |
45 | 41 | StrictTypes, |
46 | 42 | Types, |
@@ -96,8 +92,6 @@ def new_imports(self: DataModelFieldBaseT) -> tuple[Import, ...]: |
96 | 92 | extra_imports.append(IMPORT_MSGSPEC_META) |
97 | 93 | if not self.required and not self.nullable: |
98 | 94 | extra_imports.append(IMPORT_MSGSPEC_UNSETTYPE) |
99 | | - if not self.data_type.use_union_operator: # pragma: no cover |
100 | | - extra_imports.append(IMPORT_UNION) |
101 | 95 | if self.default is None or self.default is UNDEFINED: |
102 | 96 | extra_imports.append(IMPORT_MSGSPEC_UNSET) |
103 | 97 | return chain_as_tuple(original_imports.fget(self), extra_imports) # pyright: ignore[reportOptionalCall] |
@@ -218,32 +212,18 @@ class Constraints(_Constraints): |
218 | 212 |
|
219 | 213 |
|
220 | 214 | @lru_cache |
221 | | -def get_neither_required_nor_nullable_type(type_: str, use_union_operator: bool) -> str: # noqa: FBT001 |
| 215 | +def get_neither_required_nor_nullable_type(type_: str, use_union_operator: bool) -> str: # noqa: ARG001, FBT001 |
222 | 216 | """Get type hint for fields that are neither required nor nullable, using UnsetType.""" |
223 | | - type_ = _remove_none_from_union(type_, use_union_operator=use_union_operator) |
224 | | - if type_.startswith(OPTIONAL_PREFIX): # pragma: no cover |
225 | | - type_ = type_[len(OPTIONAL_PREFIX) : -1] |
226 | | - |
| 217 | + type_ = _remove_none_from_union(type_, use_union_operator=True) |
227 | 218 | if not type_ or type_ == NONE: |
228 | 219 | return UNSET_TYPE |
229 | | - if use_union_operator: |
230 | | - return UNION_OPERATOR_DELIMITER.join((type_, UNSET_TYPE)) |
231 | | - if type_.startswith(UNION_PREFIX): # pragma: no cover |
232 | | - return f"{type_[:-1]}{UNION_DELIMITER}{UNSET_TYPE}]" |
233 | | - return f"{UNION_PREFIX}{type_}{UNION_DELIMITER}{UNSET_TYPE}]" # pragma: no cover |
| 220 | + return UNION_OPERATOR_DELIMITER.join((type_, UNSET_TYPE)) |
234 | 221 |
|
235 | 222 |
|
236 | 223 | @lru_cache |
237 | | -def _add_unset_type(type_: str, use_union_operator: bool) -> str: # noqa: FBT001 |
| 224 | +def _add_unset_type(type_: str, use_union_operator: bool) -> str: # noqa: ARG001, FBT001 |
238 | 225 | """Add UnsetType to a type hint without removing None.""" |
239 | | - if use_union_operator: |
240 | | - return f"{type_}{UNION_OPERATOR_DELIMITER}{UNSET_TYPE}" |
241 | | - if type_.startswith(UNION_PREFIX): # pragma: no cover |
242 | | - return f"{type_[:-1]}{UNION_DELIMITER}{UNSET_TYPE}]" |
243 | | - if type_.startswith(OPTIONAL_PREFIX): # pragma: no cover |
244 | | - inner_type = type_[len(OPTIONAL_PREFIX) : -1] |
245 | | - return f"{UNION_PREFIX}{inner_type}{UNION_DELIMITER}{NONE}{UNION_DELIMITER}{UNSET_TYPE}]" |
246 | | - return f"{UNION_PREFIX}{type_}{UNION_DELIMITER}{UNSET_TYPE}]" # pragma: no cover |
| 226 | + return f"{type_}{UNION_OPERATOR_DELIMITER}{UNSET_TYPE}" |
247 | 227 |
|
248 | 228 |
|
249 | 229 | @import_extender |
|
0 commit comments