-
Notifications
You must be signed in to change notification settings - Fork 106
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 deserialization for str unions #1239
Fix deserialization for str unions #1239
Conversation
PR argoproj-labs#1168 changed the logic of map_runner_input and _parse to no longer pass incoming values to json.loads if their annotated type was a union that included str, rather than only when given a subtype of `Optional[str]`. Split `origin_type_issubclass` into two functions, `origin_type_issupertype` (which matches the previous behaviour) and `origin_type_issubtype`, and use the latter instead to restore the original behaviour. Add a runner check which verifies this behaviour. Signed-off-by: Alice Purcell <alicederyn@gmail.com>
9192b57
to
52437be
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1239 +/- ##
=====================================
Coverage 45.9% 45.9%
=====================================
Files 60 60
Lines 4082 4096 +14
Branches 857 861 +4
=====================================
+ Hits 1875 1884 +9
- Misses 2175 2180 +5
Partials 32 32 ☔ View full report in Codecov by Sentry. |
parse_obj_as does not appear to support union types (it is converting ints to strings). Signed-off-by: Alice Purcell <alicederyn@gmail.com>
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.
Looks great to me. just small one.
return origin_type_issubclass(func_param_annotation, str) | ||
return origin_type_issubtype(func_param_annotation, (str, NoneType)) |
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.
👍 Thanks
Signed-off-by: Alice Purcell <alicederyn@gmail.com>
If origin_type_issubtype or origin_type_issupertype are passed a special form annotation, they will raise a TypeError due to passing it into issubclass. Fix this issue by first checking if the annotation is a type, and returning False if not. Unit test this with NoReturn, as we are very unlikely to ever create a special case for this type in the function. Signed-off-by: Alice Purcell <alicederyn@gmail.com>
The origin_type_is* functions were accidentally calling get_args on the original annotation rather than the unwrapped type. Signed-off-by: Alice Purcell <alicederyn@gmail.com>
5f03b16
to
c434384
Compare
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.
looks great 👍
Pydantic v1 checks union types in declaration order, returning as soon as a coercion succeeds; see https://docs.pydantic.dev/1.10/usage/model_config/#smart-union for more. Change the test str_or_int_parameter signature from `str | int` to `int | str` to work with this legacy mode. Signed-off-by: Alice Purcell <alicederyn@gmail.com>
Pull Request Checklist
Description of PR
PR #1168 changed the logic of
map_runner_input
and_parse
to no longer pass incoming values tojson.loads
if their annotated type was a union that included str, rather than only when given a subtype ofOptional[str]
. This makes it impossible to, for instance, pass an int to aUnion[str, int]
.This PR splits
origin_type_issubclass
into two functions,origin_type_issupertype
(which matches the previous behaviour) andorigin_type_issubtype
, and use the latter instead to restore the original behaviour.Additionally, it fixes a bug where we were passing an annotation to
issubclass
without first checking if it's a type, resulting in aTypeError
(partially addresses #1173).