Closed changhc closed 1 month ago
Attention: Patch coverage is 82.12560%
with 37 lines
in your changes missing coverage. Please review.
Project coverage is 89.55%. Comparing base (
ab503cb
) to head (8919589
). Report is 148 commits behind head on main.
Comparing changhc:implement-complex
(8919589) with main
(863640b)
✅ 155
untouched benchmarks
Thanks!
I've looked into the rust regex crate. It has rather limited functionality, specifically lookahead, which means we will need to handle some corner cases ourselves or create some not so straightforward logic. Another crate fancy_regex has better support for regex, but introducing another dependency on a non-native library makes me a bit hesitant unless it's really necessary or beneficial enough. With regex, my biggest concern is whether or not we can capture exactly what is accepted by python. I'm concerned about accidentally accepting/rejecting some strings that are actually rejected/accepted by the complex class in python, which might bring users unexpected problems.
If the team thinks it's okay with regex (and maybe you don't feel like having this cumbersome dictionary representation at all,) I can definitely look into that.
What if we avoided using either a regex or num_complex
entirely, and we used the Python logic to build complex
instances? This is basically what we do already for Decimal
, and it seems like a reasonable design to repeat again IMO.
That sounds good to me. Then I'll work on that. On the other hand, what do
you think about the dict representation I proposed and implemented in this
PR? Should we still keep it or should we only accept strings like 1+2j
?
That sounds good to me. Then I'll work on that. On the other hand, what do you think about the dict representation I proposed and implemented in this PR? Should we still keep it or should we only accept strings like
1+2j
?
We'll chat with the team in our open source sync tomorrow and get back to you :).
That sounds good to me. Then I'll work on that. On the other hand, what do you think about the dict representation I proposed and implemented in this PR? Should we still keep it or should we only accept strings like
1+2j
?
Let's go with the string representation for now, and if the feature lands with some popularity, we can add support for the dictionary style. For now, if folks really need that dict type serialization for complex numbers, they can use a custom serializer.
I've updated the implementation. There is one test case failing with pypy, and I think that's because of some bugs in pypy. How do you usually handle discrepancy between pypy and python? Can we ignore this case for now and simply add a note in the documentation?
Another question regarding test-pydantic-integration
. How do I handle this mutual dependency between pydantic and pydantic-core so that this test can pass?
Can we ignore this case for now and simply add a note in the documentation?
Yes, we can xfail this case when running on PyPy. If you're willing to also report the case on the PyPy GitHub, it would help them be aware they need to fix :)
Another question regarding
test-pydantic-integration
. How do I handle this mutual dependency between pydantic and pydantic-core so that this test can pass?
In this case it won't pass until we update pydantic main
for these changes, so once we merge this PR we should make a minor release and integrate them into the main repository.
Hi @davidhewitt, how should we proceed with this? Do we wait for input from the team or should we manage to make a decision here ourselves?
Hey @changhc,
Sorry we've dropped the ball on feedback here. Will chat with the team early next week so that we can move this forward and include it in our v2.9 release 🚀 !
Thanks @sydney-runkle! I'm making some changes to address David's comments. They will be ready soon!
Hi @davidhewitt, I've implemented the strict mode for complex numbers. As we discussed, when strict mode is on,
Because of the strict mode, I tweaked the messages for validation errors a little bit to make errors less confusing to users. Specifically for python input,
For other input types, the error is rather simple as complex strings are the only acceptable input values when strict mode is on.
I also added some test cases for dictionaries involving complex keys just to make sure things work as expected.
@davidhewitt, are we ready to move this across the line?
Yep, LGTM! Thanks @changhc
Thanks! I'll update https://github.com/pydantic/pydantic/pull/9654 once this PR is merged and the next minor release includes these changes.
Great! I'll get this merged tomorrow 🚀
Great work @changhc, going to go ahead and merge this - the failing integration tests should soon be fixed with your branch.
Do we need to make any more updates to https://github.com/pydantic/pydantic/pull/9654 other than supporting a new version of pydantic-core
with this change?
Change Summary
Implement a validator and a serialiser for complex numbers.
As discussed in https://github.com/pydantic/pydantic/issues/8555, since there is no official representation for complex numbers in JSON, I propose to express complex numbers as dictionaries with two keys,
real
andimag
, with floating point values. Please find examples in the newly added test suites in this PR.Note that function
str_as_complex
inshared.rs
is not yet implemented as I need to discuss with the team if we want to support this. For example, we might want to accept strings like1+2j
as a valid complex number, as long as these strings follow the format described in python's documentation. This implementation, however, will be a bit tricky. Using regular expression is the simplest solution, but it is going to be a bit slow since building regex is rather costly. Parsing strings using the cratenum_complex
will make the implementation very neat, butnum_complex
is more tolerant in terms of string format, which means we will need to handle strings not allowed in python and it is going to be problematic. The safest solution is probably to do exactly the same as how cpython parses strings. I think that can be done in a separate PR for easier reviews.Related issue number
https://github.com/pydantic/pydantic/issues/8555
Checklist
pydantic-core
(except for expected changes)