-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Allow schema_extra to be a classmethod #1306
Allow schema_extra to be a classmethod #1306
Conversation
I find it far easier to remove the check altogether, than tinker with method inspection on class object. Not sure what is the best way to make mypy happy though. |
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.
please can you fix mypy. Also what happens if schema_extra
is a instance method? Does it work or break? Let's add a test.
@@ -1534,20 +1534,6 @@ def schema_extra(schema): | |||
assert Model.schema() == {'title': 'Model', 'type': 'override'} | |||
|
|||
|
|||
def test_model_with_schema_extra_callable_classmethod_asserts(): |
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.
let's keep this test but demonstrate that it works.
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.
Done.
Codecov Report
@@ Coverage Diff @@
## master #1306 +/- ##
===========================================
+ Coverage 99.89% 100.00% +0.10%
===========================================
Files 21 21
Lines 3708 3732 +24
Branches 731 737 +6
===========================================
+ Hits 3704 3732 +28
+ Misses 2 0 -2
+ Partials 2 0 -2
Continue to review full report at Codecov.
|
So, I've added a test for instance methods, but I'm not sure how useful it is. Lint is failing due to obsolete pyflakes version (fixed in master, but not yet released on pypi). How should we proceed? |
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.
nearly there.
pydantic/main.py
Outdated
pass | ||
|
||
@overload | ||
def __call__(self, schema: Dict[str, Any], model: Type['Model']) -> None: |
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.
Surely you don't need both this case and, the one below?
Better to use model_class
than model
to be clear.
You need to add . # noqa F811
to get linting to pass, see other examples in the code base
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.
Done.
@@ -89,7 +104,7 @@ class BaseConfig: | |||
getter_dict: Type[GetterDict] = GetterDict | |||
alias_generator: Optional[Callable[[str], str]] = None | |||
keep_untouched: Tuple[type, ...] = () | |||
schema_extra: Union[Dict[str, Any], Callable[[Dict[str, Any]], None]] = {} | |||
schema_extra: Union[Dict[str, Any], 'SchemaExtraCallable'] = {} |
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.
maybe you could test this works by adding a to tests/mypy/modules/success.py
?
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 could use some help here. I've been trying something like this to pass
class ConfigSchemaExtraCallable(BaseConfig['MyModel']):
@staticmethod
def schema_extra(schema: Dict[str, Any], model_class: Type['MyModel']) -> None:
pass
to no avail.
Even when I made BaseConfig
generic, mypy still complained about signature incompatibility.
spent ages trying to improve on this, but nothing seems to work. Let's go with this. |
Change Summary
Allow
schema_extra
to be a classmethod.Related issue number
Resolves #1305
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)