Add ability to mark ill-typed literals where the lexical value does not match the datatype given if known
# mypy: warn_return_any, no_implicit_reexport, strict_equality
from decimal import Decimal
-from typing import Any, Optional, Sequence, Tuple, Type
+from typing import Any, Optional, Sequence, Tuple, Type, Union
import unittest
import datetime
@@ -80,6 +80,47 @@ class TestNewPT:
with pytest.raises(exception_type):
Literal("foo", lang=lang)
+ @pytest.mark.parametrize(
+ "lexical, datatype, is_ill_formed",
+ [
+ ("true", XSD.boolean, False),
+ ("1", XSD.boolean, False),
+ (b"false", XSD.boolean, False),
+ (b"0", XSD.boolean, False),
+ ("yes", XSD.boolean, True),
+ ("200", XSD.byte, True),
+ (b"-128", XSD.byte, False),
+ ("127", XSD.byte, False),
+ ("255", XSD.unsignedByte, False),
+ ("-100", XSD.unsignedByte, True),
+ (b"200", XSD.unsignedByte, False),
+ (b"64300", XSD.short, True),
+ ("-6000", XSD.short, False),
+ ("1000000", XSD.nonNegativeInteger, False),
+ ("-100", XSD.nonNegativeInteger, True),
+ ("a", XSD.double, True),
+ ("0", XSD.double, False),
+ ("0.1", XSD.double, False),
+ ("0.1", XSD.decimal, False),
+ ("0.g", XSD.decimal, True),
+ ("b", XSD.integer, True),
+ ("2147483647", XSD.int, False),
+ ("2147483648", XSD.int, True),
+ ("2147483648", XSD.integer, False),
+ ],
+ )
+ def test_ill_formed_literals(
+ self,
+ lexical: Union[bytes, str],
+ datatype: URIRef,
+ is_ill_formed: bool,
+ ) -> None:
+ """
+ Construction of Literal fails if the language tag is invalid.
+ """
+ lit = Literal(lexical, datatype=datatype)
+ assert lit.ill_formed is is_ill_formed
+
class TestNew(unittest.TestCase):
# NOTE: Please use TestNewPT for new tests instead of this which is written
