Skip to content

Commit 32a4215

Browse files
authored
Merge pull request #47 from team23/sivachandran-sp/override-partial-class-name
Allow partial class name can be overridden by @sivachandran
2 parents 1eb1855 + 4d0ad4d commit 32a4215

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

pydantic_partial/partial.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def create_partial_model(
4646
base_cls: type[SelfT],
4747
*fields: str,
4848
recursive: bool = False,
49+
partial_cls_name: Optional[str] = None,
4950
) -> type[SelfT]:
5051
# Convert one type to being partial - if possible
5152
def _partial_annotation_arg(field_name_: str, field_annotation: type) -> type:
@@ -128,9 +129,12 @@ def _partial_annotation_arg(field_name_: str, field_annotation: type) -> type:
128129
if not optional_fields:
129130
return base_cls
130131

132+
if partial_cls_name is None:
133+
partial_cls_name = f"{base_cls.__name__}Partial"
134+
131135
# Generate new subclass model with those optional fields
132136
return pydantic.create_model(
133-
f"{base_cls.__name__}Partial",
137+
partial_cls_name,
134138
__base__=base_cls,
135139
**optional_fields,
136140
)
@@ -147,21 +151,23 @@ def model_as_partial(
147151
cls: type[ModelSelfT],
148152
*fields: str,
149153
recursive: bool = False,
154+
partial_cls_name: Optional[str] = None,
150155
) -> type[ModelSelfT]:
151156
return cast(
152157
type[ModelSelfT],
153-
create_partial_model(cls, *fields, recursive=recursive),
158+
create_partial_model(cls, *fields, recursive=recursive, partial_cls_name=partial_cls_name),
154159
)
155160

156161
@classmethod
157162
def as_partial(
158163
cls: type[ModelSelfT],
159164
*fields: str,
160165
recursive: bool = False,
166+
partial_cls_name: Optional[str] = None,
161167
) -> type[ModelSelfT]:
162168
warnings.warn(
163169
"as_partial(...) is deprecated, use model_as_partial(...) instead",
164170
DeprecationWarning,
165171
stacklevel=2,
166172
)
167-
return cls.model_as_partial(*fields, recursive=recursive)
173+
return cls.model_as_partial(*fields, recursive=recursive, partial_cls_name=partial_cls_name)

tests/test_partial.py

+9
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,12 @@ def test_no_change_to_optional_fields():
263263
def test_as_partial_works_as_expected():
264264
with pytest.warns(DeprecationWarning):
265265
assert Something.model_as_partial() is Something.as_partial()
266+
267+
268+
def test_partial_class_name_can_be_overridden():
269+
SomethingPartial = Something.model_as_partial("name")
270+
assert SomethingPartial.__name__ == "SomethingPartial"
271+
272+
partial_cls_name = "SomethingWithOptionalName"
273+
SomethingWithOptionalName = Something.model_as_partial("name", partial_cls_name=partial_cls_name)
274+
assert SomethingWithOptionalName.__name__ == partial_cls_name

tests/test_partial_without_mixin.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@ def test_partial_model_will_only_be_cached_with_same_params():
5555

5656

5757
def test_partial_model_will_be_the_same_on_mixin():
58-
# Note: When we do not pass recursive=False the functools.lru_cache will
58+
# Note: When we do not pass recursive=False and partial_cls_name=None the functools.lru_cache will
5959
# actually create a separate model class, as the parameters differ.
60-
SomethingWithMixinPartial1 = create_partial_model(SomethingWithMixin, recursive=False)
60+
SomethingWithMixinPartial1 = create_partial_model(SomethingWithMixin, recursive=False, partial_cls_name=None)
6161
SomethingWithMixinPartial2 = SomethingWithMixin.model_as_partial()
6262

6363
assert SomethingWithMixinPartial1 is SomethingWithMixinPartial2
64+
65+
66+
def test_partial_class_name_can_be_overridden():
67+
SomethingPartial = create_partial_model(Something, "name")
68+
assert SomethingPartial.__name__ == "SomethingPartial"
69+
70+
partial_cls_name = "SomethingWithOptionalName"
71+
SomethingWithOptionalName = create_partial_model(Something, "name", partial_cls_name=partial_cls_name)
72+
assert SomethingWithOptionalName.__name__ == partial_cls_name

0 commit comments

Comments
 (0)