fix(fields): handle properly default value for type Callable#2094
fix(fields): handle properly default value for type Callable#2094samuelcolvin merged 8 commits intopydantic:masterfrom
Callable#2094Conversation
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.
|
|
|
||
|
|
||
| UNTOUCHED_TYPES = FunctionType, property, type, classmethod, staticmethod | ||
| FIELD_DEFAULT_VALUE_UNTOUCHED_TYPES = property, type, classmethod, staticmethod |
There was a problem hiding this comment.
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.
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
| def non_default_callable(x): | ||
| return foo(x, 'nondefault') | ||
|
|
||
| for cls in (HasCallablesModel, HasCallablesDC): |
There was a problem hiding this comment.
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.
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 :)
| 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.
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>.mdfile added describing change(see changes/README.md for details)