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
Pass model_class to schema_extra staticmethod #1125
Pass model_class to schema_extra staticmethod #1125
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1125 +/- ##
======================================
Coverage 100% 100%
======================================
Files 20 20
Lines 3445 3450 +5
Branches 665 667 +2
======================================
+ Hits 3445 3450 +5
Continue to review full report at Codecov.
|
@samuelcolvin I think this is ready for review now, the tests seem to be failing due to an unrelated issue? |
163d22e
to
a5f34ff
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.
sorry for the delay in getting back to this.
Cool, I'll make those changes when I'm back from holiday next week.
…On Fri, 3 Jan 2020, 00:50 Samuel Colvin, ***@***.***> wrote:
***@***.**** commented on this pull request.
sorry for the delay in getting back to this.
------------------------------
In changes/1125-therefromhere.md
<#1125 (comment)>
:
> @@ -0,0 +1 @@
+* Pass model class to the `Config.schema_extra` callable, #1125 by @therefromhere
⬇️ Suggested change
-* Pass model class to the `Config.schema_extra` callable, #1125 by @therefromhere
+* Pass model class to the `Config.schema_extra` callable
The ending is added automatically from the file name.
------------------------------
In docs/examples/schema_extra_callable.py
<#1125 (comment)>
:
> @@ -8,8 +8,10 @@ class Person(BaseModel):
class Config:
@staticmethod
- def schema_extra(schema: Dict[str, Any]) -> None:
- for prop in schema.get('properties', {}).values():
- prop.pop('title', None)
+ def schema_extra(
+ schema: Dict[str, Any], model_class: Type["Person"]
⬇️ Suggested change
- schema: Dict[str, Any], model_class: Type["Person"]
+ schema: Dict[str, Any], model_class: Type['Person']
Also, are you sure this can't go on one line? Examples have a line length
maximum of 80.
------------------------------
In docs/examples/schema_extra_callable.py
<#1125 (comment)>
:
> @@ -8,8 +8,10 @@ class Person(BaseModel):
class Config:
@staticmethod
- def schema_extra(schema: Dict[str, Any]) -> None:
- for prop in schema.get('properties', {}).values():
- prop.pop('title', None)
+ def schema_extra(
+ schema: Dict[str, Any], model_class: Type["Person"]
+ ) -> None:
+ for prop in schema.get("properties", {}).values():
⬇️ Suggested change
- for prop in schema.get("properties", {}).values():
+ for prop in schema.get('properties', {}).values():
------------------------------
In docs/examples/schema_extra_callable.py
<#1125 (comment)>
:
> @@ -8,8 +8,10 @@ class Person(BaseModel):
class Config:
@staticmethod
- def schema_extra(schema: Dict[str, Any]) -> None:
- for prop in schema.get('properties', {}).values():
- prop.pop('title', None)
+ def schema_extra(
+ schema: Dict[str, Any], model_class: Type["Person"]
+ ) -> None:
+ for prop in schema.get("properties", {}).values():
+ prop.pop("title", None)
⬇️ Suggested change
- prop.pop("title", None)
+ prop.pop('title', None)
pydantic uses single quotes be default.
------------------------------
In pydantic/schema.py
<#1125 (comment)>
:
> @@ -464,7 +465,11 @@ def model_process_schema(
s.update(m_schema)
schema_extra = model.__config__.schema_extra
if callable(schema_extra):
- schema_extra(s)
+ assert isinstance(schema_extra, FunctionType), 'Config.schema_extra callable is expected to be a staticmethod'
we should use TypeError here
------------------------------
In tests/test_schema.py
<#1125 (comment)>
:
> @@ -1490,6 +1490,19 @@ class Config:
def test_model_with_schema_extra_callable():
+ class Model(BaseModel):
+ name: str = None
+
+ class Config:
+ @staticmethod
+ def schema_extra(schema, model_class):
+ schema.pop('properties')
+ schema['type'] = 'override'
⬇️ Suggested change
- schema['type'] = 'override'
+ schema['type'] = 'override'
+ assert isinstance(model_class, Model)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#1125>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AABQHJAOADWXBC7I5LYAIJ3Q3XIHXANCNFSM4J6YD3OQ>
.
|
Co-Authored-By: Samuel Colvin <samcolvin@gmail.com>
e98fd9b
to
f764bf8
Compare
@samuelcolvin I've made those changes + rebased. |
great, thanks. |
* Pass model_class to schema_extra staticmethod Resolves pydantic#1122 * Add changelog * Apply suggestions from code review Co-Authored-By: Samuel Colvin <samcolvin@gmail.com> * Fix import after rebase * Fix test bug * Use TypeError instead of assert as per review * Rename var so declaration fits one one line * tiny tweaks Co-authored-by: Samuel Colvin <samcolvin@gmail.com>
@therefromhere @samuelcolvin any particular reason why Also, when |
I think because it would be confusing - bear in mind it'll be a classmethod of Config (so cls wouldn't give access to your model class). This threw me originally when I was talking about it. What would the use case for a classmethod be here? Good point about regular methods not hitting TypeError, that's a bug. |
I'm using classmethods for config inheritance, so things break for me when updating from v1.3. |
Change Summary
Pass model_class to schema_extra staticmethod.
Related issue number
Resolves #1122
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)