-
Notifications
You must be signed in to change notification settings - Fork 4
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
Unintentional type casting with union types when creating instances #379
Comments
This issue seems related to this one in |
Hi David, Thanks for creating such a detailed issue with tests to replicate it! I'm going to split this into two issues - one for the Union typing issue and one for the _auto_sync issue. |
#380 for the _auto_sync concerns you mentioned. I've added a comment there, tl;dr that behavior is expected. |
I reviewed your tests (thanks again!) and added them as a test case to the pydantic_aioredis coverage here https://github.com/andrewthetechie/pydantic-aioredis/blob/main/test/test_union_typing.py This is an issue in pydantic. To validate that, I wrote two test cases that replicate your issue, but using Pydantic instead of pydantic_aioredis models https://github.com/andrewthetechie/pydantic-aioredis/blob/main/test/test_union_typing.py#L48-L61 These tests fail just like yours did when running Reading through pydantic/pydantic#4519, it doesn't seem like this is something easy to fix. |
@david-wahlstedt I wrote up some docs to cover this case and PR'd it here #383 If you've got a moment, I'd really appreciate if you review it and any feedback you have. |
@all-contributors add @david-wahlstedt for test |
I've put up a pull request to add @david-wahlstedt! 🎉 |
Hi, when I wrote my tests I ran them in another directory where I had pydanti-aioredis installed, and I used Redis locally on my machine. Now I tried running pytest in the root directory of the repo, but it feild with the following message:
I got the same error when i just ran pytest without arguments. |
You need to ensure you have all the dev requirements installed to run the testing. Here's the docs on how to setup a dev environment https://pydantic-aioredis.readthedocs.io/en/latest/development.html#environment-setup tl;dr is
Then you can run the tests directly with pytest or with nox
With nox you can run the full test suite, but you'll need to have python 3.7, 3.8, 3.9, 3.10, and 3.11 available:
|
* docs: add docs about Union types and casting #379 * docs: add additional example
#383 merged and should show up in read the docs soon. Ty for the PR review |
Hi, just discovered this issue as it linked to my pydantic issue #4519. Just wanted to inform that these has nothing to do with each other and the seemingly faulty functionality of the current issue is really about configuring pydantic correctly for your use case. E.g.:
I assume the default configuration is set for performance issues. This is for pydantic v1. v2 arrived yesterday and I haven't checked if this changes anything. |
Hi, I encountered some problems with how union types are handled. If I have a field x : Union[int, float] and set it to 1.9 inside the constructor, it gets truncated into 1: int. But if I assign the field x=1.9 after instance creation, it is preserved a a float with correct value. The same the other way around: if I have y : Union[float, int] and assign it to 2 inside the constructor, it gets casted into 2.0: float. But if I do x=2 after instance creation, it is preserved as an int. I also tried to put Optional inside or outside the union, and it changed the behavior as well. Furthermore I have noticed that if you create an instance with _auto_sync on (default), it is not saved in Redis until you assign some field to it after creation. I don't know if this might be a
pydantic
issue rather than apydantic-aioredis
one. But I start here ans see what you think. I have written a pytest script that reproduces the issue, and I tried to make it small to isolate the problem. Here it is:The text was updated successfully, but these errors were encountered: