pyxsd.relaxng.datatypes ======================= .. py:module:: pyxsd.relaxng.datatypes .. autoapi-nested-parse:: RELAX NG datatype protocols and the built-in/XSD 1.0 libraries. RELAX NG delegates lexical acceptance and value equality to datatype libraries. A factory compiles a type name, its parameters, and the schema-side context into a reusable :class:`Datatype`. Every operation receives the context of the string it interprets, so a ``value`` pattern can compare a schema-side QName against an instance-side QName in different prefix scopes. The empty library URI is the RELAX NG built-in library. It has exactly the two datatypes of RELAX NG 1.0 §6.2.9: ``string`` accepts every string and compares by identity, and ``token`` accepts every string and compares after XML-whitespace normalization. Parameters are not allowed. :data:`XSD_LIBRARY` adapts pyxsd's existing XSD built-in classes. The adapter follows XSD 1.0 semantics, not the XSD compiler's default 1.1 profile: * the XSD 1.1-only types ``anyAtomicType``, ``dateTimeStamp``, ``dayTimeDuration``, ``yearMonthDuration`` (and ``precisionDecimal``) are rejected explicitly rather than accepted as strings; * lexical year ``0000`` is rejected for ``date``, ``dateTime``, ``gYear`` and ``gYearMonth``; lexical ``-0001`` is 1 BCE, so 1 BCE is a leap year and ``-0001-02-29`` is valid while ``-0004-02-29`` (4 BCE) is not; * temporal value equality, bounds, and decoding use the XSD 1.0 astronomical mapping for negative years (lexical ``y < 0`` is astronomical ``y + 1``), not the shared classes' XSD 1.1 numbering; * duration equality uses the six-dimensional XSD 1.0 value space, so ``P1Y`` and ``P12M`` are distinct values; * duration bound facets use the XSD 1.0 §3.2.6.2 four-reference-date partial order, and an indeterminate comparison is false for every bound facet, as §3.2.6.3 requires; * ``anyURI`` uses the XSD 1.0 lexical space: the XLink 5.4 escaping procedure is applied, and the result must be an RFC 2396 URI reference as amended by RFC 2732. XSD 1.1's "any XML character sequence" rule is not used. Equality remains identity of the lexical URI-reference representation; XSD 1.0 defines no anyURI normalization. Scheme-specific URI syntax is not checked, matching XSD 1.0's conformance note; the generic URI-reference grammar is. The XSD adapter rejects the ``whiteSpace`` and ``enumeration`` parameters as the RELAX NG datatype guidelines require. Repeated ``pattern`` parameters are intersected; every other parameter may appear at most once. Facets are validated when the datatype is compiled, and an inapplicable facet is a parameter error. ``allows``, ``equal``, ``decode`` and ``encode`` are independent operations. ``decode`` and ``encode`` are optional pyxsd capabilities, not RELAX NG conformance: a custom datatype may implement only validation, in which case the inherited methods raise :class:`UnsupportedDatatypeOperation`. A datatype that genuinely provides decoding must declare ``supports_decode = True``; overriding :meth:`Datatype.decode` is not by itself a capability declaration, because an implementation may override it only to report the operation as unsupported. Value equality deliberately ignores facet parameters because RELAX NG's ``datatypeEqual`` service has no parameters. A compiled datatype is frozen, hashable, and value-comparable. Its ``repr`` is deterministic because the canonical choice order of the pattern IR sorts leaves by ``repr``. The schema-side :class:`DatatypeContext` is retained for later ``value`` matching; for context-insensitive types it does not participate in datatype equality, while QName and NOTATION datatypes compare by their schema-side namespace bindings. Attributes ---------- .. autoapisummary:: pyxsd.relaxng.datatypes.BUILTIN_LIBRARY pyxsd.relaxng.datatypes.XSD_LIBRARY pyxsd.relaxng.datatypes.BUILTIN_FINGERPRINT pyxsd.relaxng.datatypes.XSD_FINGERPRINT pyxsd.relaxng.datatypes.DatatypeSelector Exceptions ---------- .. autoapisummary:: pyxsd.relaxng.datatypes.DatatypeError pyxsd.relaxng.datatypes.UnsupportedDatatypeError pyxsd.relaxng.datatypes.DatatypeParameterError pyxsd.relaxng.datatypes.DatatypeRegistrationError pyxsd.relaxng.datatypes.UnsupportedDatatypeOperation Classes ------- .. autoapisummary:: pyxsd.relaxng.datatypes.DatatypeContext pyxsd.relaxng.datatypes.DatatypeParameter pyxsd.relaxng.datatypes.Datatype pyxsd.relaxng.datatypes.DatatypeFactory pyxsd.relaxng.datatypes.ScalarDatatype pyxsd.relaxng.datatypes.BuiltinDatatypeFactory pyxsd.relaxng.datatypes.XsdDatatypeFactory pyxsd.relaxng.datatypes.DatatypeLibraryRegistration pyxsd.relaxng.datatypes.DatatypeLibraryRegistry Functions --------- .. autoapisummary:: pyxsd.relaxng.datatypes.datatype_supports_decode pyxsd.relaxng.datatypes.datatype_supports_encode pyxsd.relaxng.datatypes.resolve_datatype_selector Module Contents --------------- .. py:data:: BUILTIN_LIBRARY :value: '' .. py:data:: XSD_LIBRARY :value: 'http://www.w3.org/2001/XMLSchema-datatypes' .. py:data:: BUILTIN_FINGERPRINT :value: 'pyxsd.relaxng.builtin/1' .. py:data:: XSD_FINGERPRINT :value: 'pyxsd.relaxng.xsd-1.0/3' .. py:data:: DatatypeSelector .. py:exception:: DatatypeError Base class for datatype capability and parameter failures. .. py:exception:: UnsupportedDatatypeError A datatype library or type is outside the adapter's capabilities. .. py:exception:: DatatypeParameterError A datatype parameter is unknown, inapplicable, or inconsistent. .. py:exception:: DatatypeRegistrationError A datatype-library registration is invalid or would replace an identity. .. py:exception:: UnsupportedDatatypeOperation The datatype has no decoder or encoder for the requested operation. .. py:class:: DatatypeContext The schema- or instance-side context a lexical value is read in. ``base_uri`` is retained for datatypes that resolve relative references. ``namespaces`` maps in-scope prefixes to namespace URIs; the empty prefix is the default namespace. The mapping is copied into an immutable, hashable form at construction. .. py:attribute:: base_uri :type: str | None .. py:attribute:: namespaces :type: collections.abc.Mapping[str, str] .. py:class:: DatatypeParameter One RELAX NG datatype parameter: an XSD facet name and its lexical value. .. py:attribute:: name :type: str .. py:attribute:: value :type: str .. py:class:: Datatype The compiled behavior of one RELAX NG datatype. ``supports_decode`` is an explicit, schema-static capability declaration for :meth:`decode`; ``supports_encode`` is the matching declaration for :meth:`encode`. The base class does not infer capability from method overrides: a subclass may override ``decode`` or ``encode`` only to raise :class:`UnsupportedDatatypeOperation`, and such a datatype remains nondecodable/nonencodable for shape selection until it declares support. The flags are implementation behavior, not datatype semantic identity; they do not participate in equality, hashing, or pattern identity. Implementations that serve as pattern payloads must also be immutable, hashable, value-comparable, and deterministically ``repr``-able, because the pattern IR interns and canonically orders them. .. py:attribute:: supports_decode :type: ClassVar[bool] :value: False .. py:attribute:: supports_encode :type: ClassVar[bool] :value: False .. py:method:: allows(lexical, context) :abstractmethod: Return whether *lexical* is a legal value in *context*. .. py:method:: equal(left, left_context, right, right_context) :abstractmethod: Return whether two lexical values denote the same datatype value. .. py:method:: decode(lexical, context) Decode *lexical* into a Python value when support is declared. .. py:method:: encode(value, context) Encode a Python value as a lexical form, when the datatype supports it. .. py:function:: datatype_supports_decode(datatype) Return whether *datatype* explicitly declares decoder support. This reads the class-level :attr:`Datatype.supports_decode` declaration. It is not an inference from a ``decode`` override and does not call ``decode``. Instance state cannot turn capability on or off. .. py:function:: datatype_supports_encode(datatype) Return whether *datatype* explicitly declares encoder support. This reads the class-level :attr:`Datatype.supports_encode` declaration. It never infers support from an ``encode`` override and never calls ``encode``. Instance state cannot turn capability on or off. .. py:class:: DatatypeFactory A datatype library: compile a type name and parameters into a datatype. .. py:method:: create(type_name, parameters, context) :abstractmethod: Compile one datatype or report why it is unavailable. .. py:class:: ScalarDatatype A frozen scalar datatype backed by one pyxsd XSD built-in class. ``context`` is the schema-side context retained for later ``value`` matching. It is excluded from equality for context-insensitive types; :attr:`semantic_context` carries the namespace bindings that actually affect QName/NOTATION identity. .. py:attribute:: supports_decode :type: ClassVar[bool] :value: True .. py:attribute:: supports_encode :type: ClassVar[bool] :value: True .. py:attribute:: library_uri :type: str .. py:attribute:: type_name :type: str .. py:attribute:: xsd_type :type: type[Any] .. py:attribute:: parameters :type: tuple[DatatypeParameter, ...] .. py:attribute:: context :type: DatatypeContext .. py:attribute:: constraints :type: pyxsd.facets.FacetConstraints .. py:attribute:: white_space :type: str .. py:attribute:: semantic_context :type: tuple[tuple[str, str], ...] .. py:attribute:: temporal_bounds :type: tuple[tuple[str, fractions.Fraction, str, bool], ...] :value: () .. py:attribute:: duration_bounds :type: tuple[tuple[str, _Xsd10Duration, str], ...] :value: () .. py:method:: allows(lexical, context) Return whether *lexical* is a legal value in *context*. .. py:method:: equal(left, left_context, right, right_context) Compare two values in this datatype's value space. Facet parameters do not participate: RELAX NG's ``datatypeEqual`` service has no parameters. A lexical form outside the base datatype has no value and therefore compares unequal to everything. .. py:method:: decode(lexical, context) Decode a validated lexical form into its Python value. ``hexBinary`` and ``base64Binary`` return ``bytes``; ``boolean`` returns a Python ``bool``; QName and NOTATION return a context-resolved :class:`~pyxsd.xsd_data_types.QName`; the four year-bearing temporal types return a validated lexical ``str`` subclass with a ``lexical()`` method; every other type returns its XSD value object (an ``int``, ``Decimal``, ``float``, or ``str`` subclass). Facets are enforced, so an out-of-range or pattern-violating lexical form raises ``TypeError`` or ``ValueError`` instead of producing a value outside the datatype. .. py:method:: encode(value, context) Encode a Python value as a canonical lexical form. Expected inputs are ``bytes``-like for binary types, ``bool`` or ``int`` for boolean, ``int`` for the integer family, ``Decimal`` or ``int`` for decimal, ``int`` or ``float`` for float/double, ``str`` for string and temporal types, and ``QName`` or ``str`` for QName and NOTATION. The produced lexical form is validated against the datatype's facets. A QName prefix must be bound in *context*; a resolved QName must denote the same namespace there, so no unresolved prefix is invented. .. py:class:: BuiltinDatatypeFactory RELAX NG's built-in datatype library: ``string`` and ``token``. .. py:method:: create(type_name, parameters, context) Compile one datatype or report why it is unavailable. .. py:class:: XsdDatatypeFactory The RELAX NG XSD datatype library with XSD 1.0 semantics. .. py:method:: create(type_name, parameters, context) Compile one datatype or report why it is unavailable. .. py:function:: resolve_datatype_selector(selector) Resolve a selector to ``(library_uri, type_name)``. A bare string names a type in the built-in library. The documented ``xsd:`` shorthand names a type in the XSD library; it is a fixed alias, never a prefix lookup against the datatype context. A tuple is an explicit ``(library_uri, type_name)`` selector. .. py:class:: DatatypeLibraryRegistration A custom datatype library factory and its stable configuration identity. .. py:attribute:: factory :type: DatatypeFactory .. py:attribute:: fingerprint :type: str .. py:class:: DatatypeLibraryRegistry The available datatype libraries with fixed built-in/XSD identities. .. py:method:: library_uris() The registered library URIs in deterministic order. .. py:method:: registration(uri) Return the registration for *uri* or raise an unsupported failure. .. py:method:: register(uri, registration) Register a custom datatype library under *uri*. The built-in and XSD identities are fixed and cannot be replaced. Every custom fingerprint must be stable and process-independent, so a blank string or a captured object address is rejected. .. py:method:: resolve(selector) Resolve a selector without constructing a datatype. .. py:method:: create(selector, parameters, context) Compile the selected datatype.