New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(fields): handle properly default value for type Callable
#2094
fix(fields): handle properly default value for type Callable
#2094
Conversation
Codecov Report
@@ Coverage Diff @@
## master #2094 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 21 21
Lines 4199 4199
Branches 854 854
=========================================
Hits 4199 4199
Continue to review full report at Codecov.
|
pydantic/main.py
Outdated
@@ -198,7 +198,8 @@ def validate_custom_root_type(fields: Dict[str, ModelField]) -> None: | |||
raise ValueError('__root__ cannot be mixed with other fields') | |||
|
|||
|
|||
UNTOUCHED_TYPES = FunctionType, property, type, classmethod, staticmethod | |||
FIELD_DEFAULT_VALUE_UNTOUCHED_TYPES = property, type, classmethod, staticmethod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this name shorter. I think we need to describe what's going in here in a comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment and renamed it to ANNOTATED_FIELD_UNTOUCHED_TYPES
. Still a bit long but I couldn't find an explicit name shorter than that.
1e75ab1
to
cfba22c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
otherwise LGTM
tests/test_dataclasses.py
Outdated
def non_default_callable(x): | ||
return foo(x, 'nondefault') | ||
|
||
for cls in (HasCallablesModel, HasCallablesDC): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's really confusing if something fails to work out which object failed.
Better just to split the test and duplicate code, or if there's a lot of duplication, use fixtures.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I copied the test from the issue but I agree it was a bit confusing. I rewrote the test. Hope it's easier to read.
Also added with an annotated callable like Callable[[int, float, str], bool]
.
Ready for another review :)
tests/test_dataclasses.py
Outdated
default_callable_factory: Callable = dataclasses.field(default=lambda x: foo(x, 'factory')) | ||
|
||
class HasCallablesModel(BaseModel): | ||
non_default_callable: Callable |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about typed callables like Callable[[int, float, str], bool]
?
Surely worth adding tests for them too?
Added a small commit for schema generation in the batch since it's related (callable fields with default value) even though it's not the same root cause 😉 |
Change Summary
BEFORE
AFTER
Related issue number
closes #1596
closes #2086
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)