Skip to content

model.datatypes: Fix gYear/gYearMonth validation#586

Merged
s-heppner merged 3 commits into
eclipse-basyx:developfrom
rwth-iat:fix/gyear-validation
Jul 15, 2026
Merged

model.datatypes: Fix gYear/gYearMonth validation#586
s-heppner merged 3 commits into
eclipse-basyx:developfrom
rwth-iat:fix/gyear-validation

Conversation

@paul-gerber-svg

@paul-gerber-svg paul-gerber-svg commented Jul 6, 2026

Copy link
Copy Markdown

Previously, GYEAR_RE and GYEARMONTH_RE only matched exactly four digits and did not allow a leading minus sign. This rejected valid XSD values such as years with more than four digits (e.g. "20000") and negative years (e.g. "-2001"), even though the XSD spec permits both.

Additionally, GMonthDay.from_date(...) passed date.year instead of date.day to the constructor, and into_date(...) on GYear/GYearMonth raised Python's cryptic "year -2001 is out of range" error for negative years, since datetime.date does not support them.

This change widens the regular expressions to accept any number of digits and an optional leading minus sign, fixes the GMonthDay. from_date(...) argument order bug, and makes into_date(...) raise a clear ValueError when called on a G-Class with a negative year.

Fixes #566

…ear values

Previously, `_parse_xsd_gyear` and `_parse_xsd_gyearmonth` in `datatypes.py` reject values with more than 4 digits or negative signs.

Now, the regexes support an optional minus sign and 4+ digits, and the parsers correctly handle the sign from `match[1]`. Unit tests were added.

Fixes eclipse-basyx#566
@s-heppner

Copy link
Copy Markdown
Member

I know I proposed it so myself in the issue, that this only needs adaptation of the parsing. However I just realized that this would now parse and serialize the BCE times perfectly fine, but break when trying to convert them into datetime objects (via GYear.into_date() etc.).

I think this may be fine: Someone who just wants to store the value has no problem, it only becomes a problem when you want to perfom calculations on the value, but maybe should adapt the error message when trying to convert? Let's talk about this in the next dev meeting.

@s-heppner s-heppner added this to the Release 2.1.0 milestone Jul 14, 2026
Comment thread sdk/basyx/aas/model/datatypes.py
paul-gerber-svg and others added 2 commits July 15, 2026 09:28
The previous commit added a clear error message for negative years,
but placed it in `GYear.from_date` and `GYearMonth.from_date`. That
branch is unreachable: a Python `datetime.date` can never hold a
negative year, so `from_date` never receives one, and the G-Class
constructors accept negative years without raising.

Negative years actually fail in the opposite direction, in
`into_date`, which builds a `Date` and hits Python's cryptic
"year -2001 is out of range" error.

This change moves the clear error message from `from_date` to
`into_date` on `GYear` and `GYearMonth`, and adds a test covering the
negative-year case.
@s-heppner s-heppner merged commit 574f59e into eclipse-basyx:develop Jul 15, 2026
15 checks passed
@s-heppner s-heppner deleted the fix/gyear-validation branch July 15, 2026 08:42
@s-heppner s-heppner changed the title model.datatypes: Fix parsing of long and negative XSD years model.datatypes: Fix gYear/gYearMonth validation Jul 15, 2026
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.

2 participants