-
Notifications
You must be signed in to change notification settings - Fork 10
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
✨ Migrate from Pydantic V1 to V2 [WIP] #76
Conversation
Hey there! Please run pre-commit locally. Thank you |
@mahenzon could you review my PR, please? If you're not busy with something else, I'd appreciate your feedback |
@@ -33,7 +32,7 @@ | |||
RELATIONSHIP_SPLITTER = "." | |||
|
|||
# The mapping with validators using by to cast raw value to instance of target type | |||
REGISTERED_PYDANTIC_TYPES: Dict[Type, List[Callable]] = dict(_VALIDATORS) | |||
REGISTERED_PYDANTIC_TYPES: Dict[Type, List[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.
do we get rid of _VALIDATORS? and would it work?
else: | ||
return result_value, errors | ||
try: | ||
# Создаем экземпляр схемы для валидации значения |
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.
я пытался пойти таким путём в v1, там не проканало со сложными типами с доп валидаторами, например список из строк, а в валидаторе мы его создаём, если не передано, и что-то подобное. тут надо будет в тестах зафиксировать, что всё ок
@@ -318,7 +312,7 @@ def get_operator(model_column: InstrumentedAttribute, operator_name: str) -> str | |||
|
|||
|
|||
def get_custom_filter_expression_callable(schema_field, operator: str) -> Callable: | |||
return schema_field.field_info.extra.get( | |||
return schema_field.default.extra.get( |
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.
разве есть extra на default?
Looks great. Please rebase + fix tests + resolve comments |
@DavidRomanovizc, thanks for your work on this. When do you think you might be able to review/incorporate @mahenzon's feedback? |
@DavidRomanovizc thank you for all your hard work! Please resolve conflicts (rebase on the target branch |
validated = AtomicOperationRequest.parse_obj(operation_request) | ||
assert validated.dict(exclude_unset=True, by_alias=True) == operation_request | ||
validated = AtomicOperationRequest.model_validate(operation_request) | ||
assert validated.model_dump(exclude_unset=True, by_alias=True) == operation_request | ||
|
||
def test_not_supported_operation( |
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.
ValidationError now returns a new error message but I'm uncertain if we should modify the test itself to accommodate this change. I couldn't find where pydantic sets this new error message, which makes me hesitant to update the test without understanding the reason behind the change
@@ -50,26 +48,21 @@ def create_additional_query_params(schema: Optional[Type[BaseModel]]) -> tuple[l | |||
available_includes_names = [] | |||
|
|||
# TODO! ? | |||
schema.update_forward_refs(**registry.schemas) | |||
for name, field in (schema.__fields__ or {}).items(): | |||
schema.model_rebuild(_types_namespace=registry.schemas) |
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'am hesitant about this change, because now we get the pydantic.errors.PydanticUndefinedAnnotation: name 'PostSchema'
is not defined which most likely means that model_rebuild
works not as it should be or declared in the wrong place
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.
Is there any other way to update string type annotations to real objects? The only reason to do it is the need to work with real pydantic schemas when building APIs
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 function that allows you to update type annotations to real obj. So far, it seems to be handling the task well, and I've placed it in schema_builder.py::string_to_schema
@@ -488,7 +490,7 @@ def create_jsonapi_object_schemas( | |||
if use_schema_cache and schema in self.object_schemas_cache and includes is not_passed: | |||
return self.object_schemas_cache[schema] | |||
|
|||
schema.update_forward_refs(**registry.schemas) | |||
schema.model_rebuild(_types_namespace=registry.schemas) |
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.
And here
Also I commented out the |
Thank you a lot for your work! I'll take it from here |
What was wrong?
Need to migrate project from Pydantic V1 to V2
Related to Issue: #51
Todo: