-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 BaseModel schema to have fields of type Type #4154
Allow BaseModel schema to have fields of type Type #4154
Conversation
please review |
Thanks @aminalaee for the patch 👍 I've tested your fix locally and it raises here is the eaxmple: from pydantic import BaseModel
from typing import Type
class Test:
b: int
class TypeTest(BaseModel):
t: Type[Test]
print(TypeTest.schema()) it will raise
|
please update |
Also conflicts, @aminalaee do you think you'll be able to look at this? |
Yes if it's not a very high priority I can take a look to fix this. |
I think best to fix it if you have time 🙏, V1.10 won't be out until earliest the end of the week. |
@hramezani I'm not sure how that would be possible since in your example Lines 984 to 988 in d90def3
So to my understanding this is the current situation, both validation and schema will work for this: from pydantic import BaseModel
from typing import Type
class Test(BaseModel):
a: int
class TypeTest(BaseModel):
t: Type[Test] since the But for cases like this which are also mentioned in the docs here, the from pydantic import BaseModel
from typing import Type
class Test:
b: int
class TypeTest(BaseModel):
t: Type[Test]
# Or
class TypeTest(BaseModel):
t: Type I guess this has been the behaviour all along. I think in some issues they are referring to the first case which will be fixed by this like #4051 and #2916 and some the second case which really is a wrong usage (IMO) like #3390 |
please review |
Thanks @aminalaee for your update! |
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 think this needs to be re-thought. We also need tests for other types that don't inherit from BaseModel
.
tests/test_schema.py
Outdated
'type': 'object', | ||
'properties': {'a': {'title': 'A', 'type': 'integer'}, 'b': {'$ref': '#/definitions/Foo'}}, | ||
'required': ['a', 'b'], | ||
'definitions': { |
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.
This is not correct, this is saying b
should be an instance of Foo
, but the type says "b
should be Foo
or a subclass of Foo
".
I assume there's no equivalent in JSONSchema since you can't encode "the type Foo
" in JSON, so I assume we'll need to use a generic "any" or similar.
please update. |
Thanks for the review, I've changed it to behave as |
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.
Thank so much. |
Change Summary
Allows BaseModel to have fields of type
Type[]
and generate JSON schema.Related issue number
Fixes #4051 and fixes #2916 .
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)