Skip to content
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

Fix AnyUrl not being encodable #1071

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

07pepa
Copy link
Member

@07pepa 07pepa commented Nov 23, 2024

On nevest pydantic anyurl is not ecodable

@staticxterm
Copy link
Contributor

Looks good, please add some tests to tests/odm/test_encoder.py or elsewhere?
How come that the current test case "test_should_encode_pydantic_v2_url_correctly" didn't catch this?

@07pepa
Copy link
Member Author

07pepa commented Nov 26, 2024

Looks good, please add some tests to tests/odm/test_encoder.py or elsewhere? How come that the current test case "test_should_encode_pydantic_v2_url_correctly" didn't catch this?

there already is test that would fail without change ... hiden by using older version upgrading version uncovered this.

and set compatible pymongo
and set compatible pymongo
and set compatible pymongo
address newest pydantic changes
@staticxterm
Copy link
Contributor

Looks good, please add some tests to tests/odm/test_encoder.py or elsewhere? How come that the current test case "test_should_encode_pydantic_v2_url_correctly" didn't catch this?

there already is test that would fail without change ... hiden by using older version upgrading version uncovered this.

Okay, I get this, but I believe there should be a kind of a test that would catch this regression, if possible. But I see you've added the OpenAPI schema generation test. I guess this could suffice for now.
By the way, did this test fail under Pydantic v1 only, under Pydantic v2 only, under some specific Pydantic version or what was the case here?


if IS_PYDANTIC_V2:

@classmethod
def validate(cls, v, _: ValidationInfo):
def _validate(cls, v: str | PydanticObjectId, *_) -> PydanticObjectId:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why write the signature of the _validate() method with "*_" at the end? Which other arguments can it receive?
I believe the Pydanticv2 no_info_plain_validator_function() signature of the called method is simply (cls, v).
I tested this and it worked for me both under Pydantic v1 and Pydantic v2, using a single cls._validate(cls, v) method.

Also, the v: str | PydanticObjectId part, the v can be "Any" essentially. Due to the if check on line 145, v can also be bytes.

Comment on lines 64 to 70
if IS_PYDANTIC_V2:
plain_validator = (
core_schema.with_info_plain_validator_function
if hasattr(core_schema, "with_info_plain_validator_function")
core_schema.no_info_plain_validator_function
if hasattr(core_schema, "no_info_plain_validator_function")
else core_schema.general_plain_validator_function
)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my unpublished commit (working on the Link/BackLink serialization bugfix) I simply removed all of this boilerplate code and used the Pydantic v2 no_info_plain_validator_function() and a single cls._validate(cls, v) method, which already had the same validation code running under both major Pydantic versions.
Of course, this required some small but simple refactoring.


await asyncio.gather(*[insert_find() for _ in range(10)])
await SampleModel2.delete_all()
clients = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like there's too many unrelated changes now in this PR.
This seems to address this test spitting out some warning when there's multiple AsyncIOMotorClients being created and connecting to the same DB, but without closing the previous one.
Could this be put in another PR, please?



def test_openapi_schema_generation():
get_openapi(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test should ideally have some assertation, but I get why this is here...

@@ -16,7 +16,7 @@ jobs:
matrix:
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12", "3.13" ]
mongodb-version: [4.4, 5.0, 6.0, 7.0, 8.0 ]
pydantic-version: [ "1.10.18", "2.9.2" ]
pydantic-version: [ "1.10.18", "2.10.1" ]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any way we can ensure that it is now not also broken on Pydantic v2.9.2, or some version inbetween?
Or should we always support the newer versions, or rather, test against these newer versions?
Because in the pyproject.toml we support any Pydantic v2 version.

return model_type.parse_obj(data)
except Exception:
print(f"Error parsing model {model_type} with data {data}")
model_type.model_validate(data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the validation failed for whatever reason, why try and re-call the model_validate() method again, in the Exception case?
If we were using Pydantic v1, this would again re-throw...
Should this line be removed, since it's a no-op?

Copy link

@Riverfount Riverfount left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just review the notes of @staticxterm and the Github Action is failing, fix this issues.

@staticxterm
Copy link
Contributor

Looked into this a bit, it seems there's a lot of breakage with Url types in Pydantic v2.10.x. It was still broken for me on current latest 2.10.3. I suggest to reduce this PR to a minimum change in project.toml where we skip the Pydantic minor version range which is causing these issues (works fine on 2.9.2, started breaking with 2.10.x) and lock it down to a known working version (2.9.2) in our CI as well. Hopefully Pydantic team fixes it upstream with 2.11.0+ or some other patch version on 2.10.x (doesn't seem like it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants