-
Notifications
You must be signed in to change notification settings - Fork 7
fix union types #144
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
base: main
Are you sure you want to change the base?
fix union types #144
Conversation
Thanks for your pr. Add the unit tests for it please. |
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #144 +/- ##
=======================================
Coverage 88.59% 88.60%
=======================================
Files 8 8
Lines 491 500 +9
=======================================
+ Hits 435 443 +8
- Misses 56 57 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
In your example, different requests lead to different responses, which is understandable. I think it would be reasonable to have a request-to-response type mapping handler to handle this situation. @get("/types?type={type}", response_type_handler=type_handler)
def get_type(self, type: str) -> Union[A, B, C]: ...
def type_handler(request_info: RequestInfo):
match request_info.params["type"]:
case "A": return A
case "B": return B
... |
tests/test_union_types.py
Outdated
| json={"type": "B", "value": 42} | ||
| ) | ||
| m.get('http://example.com/types?type=A', json={"name": "test_A", "book": "test_book"}) | ||
| m.get('http://example.com/types?type=B', json={"name": "test_B", "other_book": "test_other_book"}) |
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 mean:
json={"name": "test_A"}
json={"name": "test_B"}
other fields are optional.
|
When the required field is the same and the optional field is different in the response model, and the response data only has the value of the required field, your code has no way to handle this situation correctly. |
|
To be honest, I don't understand what needs to be done in the test, even with all the explanations, I brought it closer to the option I use in my project, the problem is that if you make the first name key in both types, then the type check will not happen, everything will work in the IDE, but the first type of the two will be in the test in any case, in order for it to work, you need to add those parameters that are individual for each type. |
|
Well, you made the pull request yourself, I think mine can be rejected. |
I'm pondering if there's a way to solve this problem while ensuring ease of use. However, I don't have enough time to figure it out. If you're willing, you could help me solve this problem, rather than just solving the one you encountered.thanks |
Fixed substitution of types when using Union