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

Updated JSON serialization of Date and CalendarDate to support None #582

Merged
merged 7 commits into from
Dec 15, 2021

Conversation

jlstevens
Copy link
Contributor

Simple fix that addresses #578 issue for None valued Date parameters and does the same for CalendarDate parameters.

@jlstevens
Copy link
Contributor Author

@jbednar @philippjfr Ready for review assuming the tests go green on CI (they work fine locally).

@codecov-commenter
Copy link

codecov-commenter commented Dec 15, 2021

Codecov Report

Merging #582 (a3e5777) into master (74f7ac2) will increase coverage by 0.18%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #582      +/-   ##
==========================================
+ Coverage   82.23%   82.41%   +0.18%     
==========================================
  Files           4        4              
  Lines        2994     3014      +20     
==========================================
+ Hits         2462     2484      +22     
+ Misses        532      530       -2     
Impacted Files Coverage Δ
numbergen/__init__.py 80.32% <0.00%> (+0.40%) ⬆️
param/serializer.py 86.95% <0.00%> (+0.86%) ⬆️

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 74f7ac2...a3e5777. Read the comment docs.

@jlstevens
Copy link
Contributor Author

Tests have passed except for the pypy suite due to an unrelated issue.

@jbednar
Copy link
Member

jbednar commented Dec 15, 2021

Thanks! The code looks good. Some questions:

  • Thanks for realizing that CalendarDate needed the fix too. It looks like there is a lot of overlap between Date and Calendar date, with nearly identical changes needed in each. I think users will expect those classes to behave the same in all cases apart from the type, so for futureproofing we could consider refactoring them into a BaseDate class with a strformat parameter ("%Y-%m-%d" vs. "%Y-%m-%dT%H:%M:%S.%f"), or just making Date be that baseclass and having very little code in CalendarDate so that those two classes always stay in sync.
  • Can you scan for any other classes that allow_None that also need this fix?

@jlstevens
Copy link
Contributor Author

Thanks for prompting me to have another look!

I realize that my initial impression was correct and JSONNullable is applied even for the special schema methods. I'll push a commit now to revert that unnecessary change.

Otherwise it is about checking how the serialize and deserialize handle 'null'. The full list of parameters using these methods are: Tuple, Array, DataFrame, Date (done) and CalendarDate (done).

I'll make sure Tuple, Array, DataFrame work properly now.

@jlstevens
Copy link
Contributor Author

jlstevens commented Dec 15, 2021

I think users will expect those classes to behave the same in all cases apart from the type, so for futureproofing we could consider refactoring them into a BaseDate class with a strformat parameter ("%Y-%m-%d" vs. "%Y-%m-%dT%H:%M:%S.%f"), or just making Date be that baseclass and having very little code in CalendarDate so that those two classes always stay in sync.

At a glance that does look to be true but looking closer there isn't that much shared. The isinstance checks are different and other than the strformat format, CalendarDate.deserialize uses the .date() method. The serialize methods are also different so on balance I don't think there is that strong an incentive to make a base class.

@jbednar
Copy link
Member

jbednar commented Dec 15, 2021

Ah, didn't spot ".date()". Ok, keeping them separate is fine. Having a general declarative implementation of the instance checks across many Parameter types would be a separate task, but would seem to simplify things.

@jlstevens
Copy link
Contributor Author

@philippjfr @jbednar Any idea what the rationale might have been to hardcode allow_None=True for Array? Is there a reason arrays always have to accept None?

class Array(ClassSelector):
    """
    Parameter whose value is a numpy array.
    """

    def __init__(self, default=None, **params):
        from numpy import ndarray
        super(Array, self).__init__(ndarray, allow_None=True, default=default, **params)

@jbednar
Copy link
Member

jbednar commented Dec 15, 2021

I would assume that's because Numpy is not a required dependency and thus we can't have an array as the default value. That said, I think the right thing to do is to have the default be None but not specify allow_None, so that when someone instantiates their own Array parameter they can put their own default in, typically non-None, and then separately decide whether to allow None. That's a breaking change, but I think it's the right thing to do, as I don't think allow_None should have been hardcoded except when using the default None value.

@jlstevens
Copy link
Contributor Author

Your suggestion about allow_None makes sense to me and should be put into another issue.

Otherwise, I have added None/'null' support for Tuple, Array and DataFrame. Ready for review/merge once more.

Copy link
Member

@jbednar jbednar left a comment

Choose a reason for hiding this comment

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

Looks good, but please consistently choose either an early return style:

if value is None:
    return 'null'

return val...

or an if/else:

if value is None:
    return 'null'
else:
    return val...

Right now it's a mix of the two, and thus less easy to scan.

@jbednar jbednar merged commit ff0a178 into master Dec 15, 2021
@maximlt maximlt deleted the null_date_serialization branch April 5, 2023 14:51
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.

3 participants