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

JSON string 'format' kwarg #984

Merged
merged 45 commits into from
Sep 13, 2024
Merged

Conversation

hudson-ai
Copy link
Collaborator

Implement (some) format strings for JSON according to standards and RFCs listed here

@codecov-commenter
Copy link

codecov-commenter commented Aug 15, 2024

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 90.69767% with 4 lines in your changes missing coverage. Please review.

Project coverage is 61.43%. Comparing base (003917c) to head (e1312df).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
guidance/library/_json.py 90.69% 4 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

❗ There is a different number of reports uploaded between BASE (003917c) and HEAD (e1312df). Click for more details.

HEAD has 4 uploads less than BASE
Flag BASE (003917c) HEAD (e1312df)
84 80
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #984      +/-   ##
==========================================
- Coverage   70.10%   61.43%   -8.67%     
==========================================
  Files          62       62              
  Lines        4449     4468      +19     
==========================================
- Hits         3119     2745     -374     
- Misses       1330     1723     +393     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@riedgar-ms riedgar-ms left a comment

Choose a reason for hiding this comment

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

Would like to see some tests for each 'format'

"time": r'\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})?',
"date": r'\d{4}-\d{2}-\d{2}',
"duration": r'P(\d+Y)?(\d+M)?(\d+D)?(T(\d+H)?(\d+M)?(\d+S)?)?',
# Email addresses
Copy link
Collaborator

Choose a reason for hiding this comment

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

These are probably not comprehensive (per RFC 5322), but are a good starting point :-)

@hudson-ai hudson-ai changed the title [WIP] JSON string 'format' kwarg JSON string 'format' kwarg Sep 12, 2024
@hudson-ai
Copy link
Collaborator Author

@Harsha-Nori @riedgar-ms feeling pretty good about this. Some notes:

  1. Added pretty comprehensive tests for every built in format. Tests were borrowed from the official JSON schema validator test suite (will be borrowing more tests in the future...)
  2. A LOT of these tests have been marked as XFAIL. This includes several format strings that are yet unimplemented (and will probably not be implemented in this PR). Most of the other marked tests fall into one of two categories:
    1. Strings that SHOULD be allowed aren't (this just means we don't have full "recall")
    2. Strings that SHOULDN'T be allowed but are (this is bad.) Note that the tests in this category are all dealing with validating either (1) leap seconds/days (hard, probably won't fix) or (2) limiting the day-of-month to the right limit depending on the month (can probably fix, maybe in a different PR)
  3. Introduced a semantic change to json generation which was needed to support the (previously unknown to me) semantics in which schemas like {"format": "uuid"} will validate against strings matching that format as well as any non-string. Additionally, types can be passed as lists.

@hudson-ai
Copy link
Collaborator Author

Could alternatively xskip the classes that are currently xfailed if testing speed is a concern

Copy link
Collaborator

@riedgar-ms riedgar-ms left a comment

Choose a reason for hiding this comment

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

I'd prefer xfail to skip. That way if they do start passing we get to find out about it (albeit through a test run failure, until the xfail is removed). And there's no real time constraint, compared to the tests which run against an actual LLM.

"bad_str",
[
'"2020-01-32"', # a invalid date string with 32 days in January
pytest.param('"2021-02-29"', marks=pytest.mark.xfail(reason="number of days not yet tied to month")), # a invalid date string with 29 days in February (normal)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want some extra challenge, remember that 2000 was a leap year, but that 1900 wasn't ;-)

'"008:030:006Z"', # invalid time string with extra leading zeros
'"8:3:6Z"', # invalid time string with no leading zero for single digit
'"8:0030:6Z"', # hour, minute, second must be two digits
pytest.param('"22:59:60Z"', marks=pytest.mark.xfail(reason="leap seconds are hard")), # invalid leap second, Zulu (wrong hour)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Happy to see leap seconds acknowledged :-)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can't take credit 😉

pytest.param('"\\"joe@bloggs\\"@example.com"', marks=pytest.mark.xfail(reason="Quoted strings not yet implemented in local part")), # a quoted string with a @ in the local part is valid
'"joe.bloggs@[127.0.0.1]"', # an IPv4-address-literal after the @ is valid
pytest.param('"joe.bloggs@[IPv6:::1]"', marks=pytest.mark.xfail(reason="IPv6 is hard")), # an IPv6-address-literal after the @ is valid
'"[email protected]"', # two separated dots inside local part are valid
Copy link
Collaborator

Choose a reason for hiding this comment

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

Small extra one (since things not supporting it bug me): can you include a local part with a + in the middle, like GMail supports?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can do :)

@hudson-ai hudson-ai merged commit b424a32 into guidance-ai:main Sep 13, 2024
20 checks passed
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