linkml / linkml

Linked Open Data Modeling Language
https://linkml.io/linkml
Other
317 stars 99 forks source link

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

Open kulnor opened 5 months ago

kulnor commented 5 months ago

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

cmungall commented 5 months ago

See also: