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

feat: Support numpy types #209

Merged
merged 7 commits into from Apr 27, 2022
Merged

Conversation

kmsquire
Copy link
Collaborator

@kmsquire kmsquire commented Mar 24, 2022

  • Adds support for numpy types specified in dataclasses, converting
    them to and from standard python types as needed.
  • Note that this commit does NOT handle the case where a numpy
    type is misrepresented as a standard python type. In most cases,
    these values will fail to serialize, or may be serialized
    incorrectly.

The second commit now supports serialization of untyped or incorrectly typed numpy values (i.e., where the type of a variable is a regular Python type, but the contents are a numpy scalar or array) for json and toml only. Deserialization is unaffected in these cases--i.e., the value will be deserialized to the declared (non-numpy) type. (Deserialization to declared numpy types is handled in the first commit).

It should be possible to add support for yaml and toml, but the mechanism for doing so is different and not as obvious.

Closes #145

@yukinarit
Copy link
Owner

Hi @kmsquire!

This PR is awesome! Let me review it on this weekend 👍

@kmsquire
Copy link
Collaborator Author

Tests seem to be failing on 3.7. Locally they pass for me on 3.9 on macos, but maybe I'm doing something that isn't compatible with 3.7. I'll try to take a look.

@kmsquire
Copy link
Collaborator Author

I think I fixed the issues on 3.7 (and 3.8). The fix might be a little hacky, so I'm very open to changes.

@yukinarit
Copy link
Owner

It appears Github Actions uses CPython 3.7 instead of pypy>=3.7. Let me fix it first and see if the poetry error disappears.

he currently activated Python version 3.6.12 is not supported by the project (^3.7.0).
Trying to find and use a compatible version.
Using python3 (3.7.9)
Creating virtualenv pyserde--xklI_Zh-py3.7 in C:\Users\runneradmin\AppData\Local\pypoetry\Cache\virtualenvs
Installing dependencies from lock file

https://github.com/yukinarit/pyserde/runs/5701449127?check_suite_focus=true

@yukinarit yukinarit mentioned this pull request Mar 26, 2022
@yukinarit
Copy link
Owner

Hi, @kmsquire

I've fixed to use pypy 3.8. Could you rebase or merge the change and push again? It may resolve the CI issue.

@yukinarit
Copy link
Owner

Looks like numpy 1.21.1 wheel is not provided for python 3.10? https://pypi.org/project/numpy/1.21.1/#files

@kmsquire
Do you have any idea why the latest numpy 1.22.3 wasn't picked by poetry? 🤔

@kmsquire
Copy link
Collaborator Author

kmsquire commented Mar 29, 2022

Do you have any idea why the latest numpy 1.22.3 wasn't picked by poetry? 🤔

It looks like numpy 1.22.3 only supports python 3.8-3.10 (not 3.7) (link), so poetry probably picked a version that (it thought) supported >3.7.

I'll try to see if a conditional install will work (1.21.x on 3.7, 1.22.x on >=3.8).

@codecov
Copy link

codecov bot commented Mar 29, 2022

Codecov Report

Merging #209 (61b6130) into master (316b98e) will decrease coverage by 1.08%.
The diff coverage is 76.84%.

@@            Coverage Diff             @@
##           master     #209      +/-   ##
==========================================
- Coverage   92.56%   91.47%   -1.09%     
==========================================
  Files          10       11       +1     
  Lines        1250     1338      +88     
  Branches      272      286      +14     
==========================================
+ Hits         1157     1224      +67     
- Misses         59       78      +19     
- Partials       34       36       +2     
Impacted Files Coverage Δ
serde/compat.py 85.21% <50.00%> (-1.48%) ⬇️
serde/numpy.py 70.37% <70.37%> (ø)
serde/de.py 97.53% <100.00%> (+0.11%) ⬆️
serde/json.py 100.00% <100.00%> (ø)
serde/msgpack.py 100.00% <100.00%> (ø)
serde/se.py 96.28% <100.00%> (+0.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 128f8ce...61b6130. Read the comment docs.

@kmsquire
Copy link
Collaborator Author

Okay, I fixed the numpy install issue.

I hadn't run make setup previously, so I didn't have pre-commit installed. I did this now and fixed linting issues.

black 19.3b0 is installed by pre-commit, but is broken right now because of an update to click. Updating black to 22.3.0 fixes the issue, so that should probably be done in .pre-commit-config.yaml.

I think all that is remaining is to add some more tests to fill out test coverage (and whatever else you comment on).

@kmsquire
Copy link
Collaborator Author

kmsquire commented Apr 1, 2022

As a note to self when I have time, here is possible way to test the code run if numpy isn't installed:
https://stackoverflow.com/questions/2481511/mocking-importerror-in-python

Copy link
Owner

@yukinarit yukinarit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, Thanks for the great PR! It looks almost good to me!
Please check the review comments. Also, please format the code by make fmt

pyproject.toml Outdated Show resolved Hide resolved
poetry.lock Outdated Show resolved Hide resolved
@kmsquire
Copy link
Collaborator Author

kmsquire commented Apr 7, 2022

Hi, Thanks for the great PR! It looks almost good to me! Please check the review comments. Also, please format the code by make fmt

Hi, to be clear: the formatting issue doesn't have anything to do with my code--I did run make fmt. The problem is that the version of black that is being used depends on click, and a version of click was released that broke this (old) version of black.

The easiest solution to this problem would be to upgrade black to the latest version, which doesn't have this issue. I can do that in this PR if you'd like, although it might be better as a separate PR.

* Adds support for numpy types specified in dataclasses, converting
  them to and from standard python types as needed.
* Note that this commit does NOT handle the case where a numpy
  type is misrepresented as a standard python type.  In most cases,
  these values will fail to serialize, or may be serialized
  incorrectly.
@kmsquire
Copy link
Collaborator Author

kmsquire commented Apr 8, 2022

Okay, I removed poetry.lock, and I updated black to the latest version so that it could run properly in CI (and elsewhere).

As noted above, I did not change the numpy install conditions, because while your suggestion makes sense, it results in incorrect behavior on Python 3.7.

@yukinarit
Copy link
Owner

CI tried to install numpy 1.22 for python 3.7

  Command ['/home/runner/.cache/pypoetry/virtualenvs/pyserde-tFPN8vZX-py3.7/bin/pip', 'install', '--no-deps', '/home/runner/.cache/pypoetry/artifacts/5b/d8/4a/ba9c80ec9b3f93ad0a31ca32f1fe044ff136c92d0e9a14fbe976d6d036/numpy-1.22.3.zip'] errored with the following return code 1, and output: 

How about updating the marker condition to use ~? 🤔

markers = "python_version ~= '3.7'

@yukinarit
Copy link
Owner

@kmsquire Sorry for the delay. I don't mind upgrading black! 👍

@kmsquire
Copy link
Collaborator Author

CI tried to install numpy 1.22 for python 3.7

Strange, this was working before. I'll take a look.

@kmsquire
Copy link
Collaborator Author

markers = "python_version ~= '3.7'

Unfortunately, this doesn't seem to work, nor has anything else I've tried locally. 😢

I'm going to file a bug report with poetry.

In the mean time, I'll back out the update to numpy, so we'll be on version 1.21.x for all versions of python.

@kmsquire
Copy link
Collaborator Author

Getting closer (albeit with a restriction to numpy 1.21 for now).

The pypi test image needs to have some blas installed in order to compile numpy. I'm not sure of the best way to do this, and probably won't be able to look into it further until sometime next week.

@yukinarit
Copy link
Owner

yukinarit commented Apr 19, 2022

It's quite hard work to setup build requirements for Windows, Linux and macOS. I don't mind removing pypy from CI build matrix until the wheel is provided for all python versions 👍

@kmsquire
Copy link
Collaborator Author

Okay, I think this should finally be good.

The latest set of commits

  • restricts numpy versions correctly based on python version
  • removes pypy-3.8 from the CI test matrix (for now)

@yukinarit
Copy link
Owner

@kmsquire it seems the CI error was introduced in #215

Let me look into it 👍

Copy link
Owner

@yukinarit yukinarit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yukinarit
Copy link
Owner

Closes #145

@yukinarit yukinarit merged commit 30763e1 into yukinarit:master Apr 27, 2022
@yukinarit
Copy link
Owner

@kmsquire Thank you for the great contribution! 🎉👍

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.

Add example for serializing Numpy data
2 participants