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

Support defining multiple optional fields with different types #416

Merged
merged 1 commit into from
Mar 18, 2024

Conversation

netomi
Copy link
Contributor

@netomi netomi commented Feb 16, 2024

This fixes #407 .

The fix avoids the hack to dynamically generate a Union type instance which will just modify the existing Union[int, str] instance and re-use it for any type annotated with Union.

@netomi
Copy link
Contributor Author

netomi commented Feb 16, 2024

I want to add that this is a super-annoying bug and makes it difficult to use odmantic 1.0. Additionally this bug might have very nasty side-effects as it modifies the built-in Union[int, str] type. Though it might not be used a lot, but in case it is used by some other module, it will have the ability to break it in very creative ways imho.

@netomi
Copy link
Contributor Author

netomi commented Mar 12, 2024

For people that cant wait for a released version, I use that to patch odmantic when building a docker image:

WORKDIR /app/.venv/lib/python3.12/site-packages
COPY ./docker/odmantic.patch ./
RUN patch -p1 < odmantic.patch

using a patch file

--- a/odmantic/model.py
+++ b/odmantic/model.py
@@ -194,11 +194,9 @@
         # generics is found
         # https://github.com/pydantic/pydantic/issues/8354
         if type_origin is Union:
-            new_root = Union[
-                int, str
-            ]  # We don't care about int,str since they will be replaced
-            setattr(new_root, "__args__", new_arg_types)
-            type_ = new_root  # type: ignore
+            # as new_arg_types is a tuple, we can directly create a matching Union instance,
+            # instead of hacking our way around it: https://stackoverflow.com/a/72884529/3784643
+            type_ = Union[new_arg_types]  # type: ignore
         else:
             type_ = GenericAlias(type_origin, new_arg_types)  # type: ignore
     return type_

…esulted in all optional fields being assigned the same type
@art049 art049 force-pushed the support-multiple-optional-fields branch from 2adee4d to 2b3dcf6 Compare March 18, 2024 03:23
Copy link

codecov bot commented Mar 18, 2024

Codecov Report

Attention: Patch coverage is 86.66667% with 2 lines in your changes are missing coverage. Please review.

Project coverage is 99.14%. Comparing base (5d469e8) to head (2b3dcf6).

Files Patch % Lines
tests/unit/test_field.py 85.71% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #416      +/-   ##
==========================================
- Coverage   99.18%   99.14%   -0.04%     
==========================================
  Files          52       52              
  Lines        5045     5057      +12     
  Branches      712      712              
==========================================
+ Hits         5004     5014      +10     
- Misses         37       39       +2     
  Partials        4        4              
Flag Coverage Δ
tests-3.10-4.4-standalone 93.61% <86.66%> (-0.03%) ⬇️
tests-3.10-5-standalone 93.61% <86.66%> (-0.03%) ⬇️
tests-3.10-6-standalone 93.61% <86.66%> (-0.03%) ⬇️
tests-3.11-4-replicaSet 98.99% <86.66%> (-0.04%) ⬇️
tests-3.11-4.2-sharded 93.61% <86.66%> (-0.03%) ⬇️
tests-3.11-4.4-standalone 93.61% <86.66%> (-0.03%) ⬇️
tests-3.11-5-standalone 93.61% <86.66%> (-0.03%) ⬇️
tests-3.11-6-standalone 93.61% <86.66%> (-0.03%) ⬇️
tests-3.8-4.4-standalone 93.56% <86.66%> (-0.03%) ⬇️
tests-3.8-5-standalone 93.56% <86.66%> (-0.03%) ⬇️
tests-3.8-6-standalone 93.56% <86.66%> (-0.03%) ⬇️
tests-3.9-4.4-standalone 93.52% <86.66%> (-0.03%) ⬇️
tests-3.9-5-standalone 93.52% <86.66%> (-0.03%) ⬇️
tests-3.9-6-standalone 93.52% <86.66%> (-0.03%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@art049
Copy link
Owner

art049 commented Mar 18, 2024

If this patch works, this is amazing! I didn't find the workaround you use when I developed this feature.
I think this might also help with #379 as well.

@art049 art049 merged commit 43cf227 into art049:master Mar 18, 2024
30 of 34 checks passed
@art049
Copy link
Owner

art049 commented Mar 18, 2024

Thank you so much @netomi !

@netomi netomi deleted the support-multiple-optional-fields branch March 18, 2024 05:42
@netomi
Copy link
Contributor Author

netomi commented Mar 18, 2024

ty for merging the fix and making a new release right away.

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.

when a model has many optional field, it will raise unwanted validation type error
2 participants