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

ISO dates with trailing Z throw error when using Python < 3.11 #2096

Open
kulnor opened this issue May 4, 2024 · 1 comment
Open

ISO dates with trailing Z throw error when using Python < 3.11 #2096

kulnor opened this issue May 4, 2024 · 1 comment
Labels
bug Something that should work but isn't, with an example and a test case. community-generated

Comments

@kulnor
Copy link

kulnor commented May 4, 2024

Describe the bug
When declaring an attribute or slot with a range of datetime, a ValueError: Invalid isoformat string error is raised in Python when running version prior to 3.11

See https://note.nkmk.me/en/python-datetime-isoformat-fromisoformat/#isoformat-string-with-z

Workaround
Replace Z with +00:00 (or use Python >= 3.11)

See https://github.com/nkmk/python-snippets/blob/028c2bcd15e7c776c445ed39e6513801450ed2dc/notebook/datetime_isoformat_timezone.py#L30-L36

Suggestion
In metaamodelcore.py, automatically replace a trailing Z with +00:00 if Python version is < 3.11 (or always)

To reproduce

in Python < 3.11

from datetime import datetime
datetime.fromisoformat("2024-03-20T19:26:08.00Z") # fails
datetime.fromisoformat("2024-03-20T19:26:08.00+00:00") # works
datetime.fromisoformat("2024-03-20T19:26:08.00") # works, but no timezone

In LinkML
(1) Define a slot or attribute with range datetime, such as

created_date:
    description: The date the service was created
    range: datetime 

(2) Parse from a JSON containing with an ISO date with a traillng Z, such as
2024-03-20T19:26:08.000000Z

Expected behavior
No error when parsing the ISO string

@kulnor kulnor added the bug Something that should work but isn't, with an example and a test case. label May 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that should work but isn't, with an example and a test case. community-generated
Projects
None yet
Development

No branches or pull requests

3 participants