Skip to content

Python conversion of xsd:gYear and xsd:gYearMonth typed Literals should not return datetime.date objects #3078

@lu-pl

Description

@lu-pl

Currently, the rdflib.Literal.toPython conversion of xsd:gYear and xsd:gYearMonth-typed rdflib.Literals returns datetime.date objects with the day (xsd:gYearMonth) or day and month (xsd:gYear) arguments set to 1.

from rdflib import Literal, XSD

year = Literal("2025", datatype=XSD.gYear)
year_month = Literal("2025-02", datatype=XSD.gYearMonth)

year_month.toPython()    # datetime.date(2025, 2, 1)
year.toPython()   # datetime.date(2025, 1, 1)

I think this is plain wrong, xsd:gYear is not equivalent to datetime.date(2025, 1, 1).

Note that issue #1379 addressed this already, but focused more on a serialization problem and was then closed without really discussing the datetime conversion of xsd:gYear and xsd:gYearMonth.

A practical solution for this would be to just return an rdflib.Literal with the datatype property set; this way, the XSD type information would still be available.

I would be happy to implement the necessary changes and open a PR if this the suggested/some solution is agreed upon.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions