fix: handling of rdf:HTML literals (#2490) · RDFLib/rdflib@588286b (original) (raw)

``

1

`+

from future import annotations

`

``

2

+

``

3

`+

import builtins

`

``

4

`+

import datetime

`

``

5

`+

import logging

`

``

6

`+

from decimal import Decimal

`

``

7

`+

from test.utils import affix_tuples

`

``

8

`+

from test.utils.literal import LiteralChecker, literal_idfn

`

``

9

`+

from test.utils.namespace import EGDC

`

``

10

`+

from test.utils.outcome import OutcomeChecker, OutcomePrimitive, OutcomePrimitives

`

``

11

`+

from typing import Any, Callable, Generator, Optional, Type, Union

`

``

12

+

1

13

`# NOTE: The config below enables strict mode for mypy.

`

2

14

`# mypy: no_ignore_errors

`

3

15

`# mypy: warn_unused_configs, disallow_any_generics

`

`@@ -7,14 +19,13 @@

`

7

19

`# mypy: no_implicit_optional, warn_redundant_casts, warn_unused_ignores

`

8

20

`# mypy: warn_return_any, no_implicit_reexport, strict_equality

`

9

21

``

10

``

`-

import datetime

`

11

``

`-

import logging

`

12

``

`-

from decimal import Decimal

`

13

``

`-

from test.utils import affix_tuples

`

14

``

`-

from test.utils.literal import LiteralChecker

`

15

``

`-

from test.utils.namespace import EGDC

`

16

``

`-

from test.utils.outcome import OutcomeChecker, OutcomePrimitive, OutcomePrimitives

`

17

``

`-

from typing import Any, Callable, Generator, Optional, Type, Union

`

``

22

+

``

23

`+

try:

`

``

24

`+

import html5lib as _ # noqa: F401

`

``

25

+

``

26

`+

_HAVE_HTML5LIB = True

`

``

27

`+

except ImportError:

`

``

28

`+

_HAVE_HTML5LIB = False

`

18

29

``

19

30

`import isodate

`

20

31

`import pytest

`

`@@ -915,6 +926,21 @@ def unlexify(s: str) -> str:

`

915

926

` )

`

916

927

``

917

928

``

``

929

`+

class _UnknownType:

`

``

930

`+

"""

`

``

931

`+

A class that is not known to rdflib, used to test the how

`

``

932

`+

rdflib.term.Literal handles unknown python types.

`

``

933

`+

"""

`

``

934

+

``

935

`+

def repr(self) -> str:

`

``

936

`+

return "_UnknownType()"

`

``

937

+

``

938

`+

def eq(self, __value: object) -> bool:

`

``

939

`+

if isinstance(__value, _UnknownType):

`

``

940

`+

return True

`

``

941

`+

return False

`

``

942

+

``

943

+

918

944

`@pytest.mark.parametrize(

`

919

945

` ["literal_maker", "outcome"],

`

920

946

` [

`

`@@ -951,7 +977,30 @@ def unlexify(s: str) -> str:

`

951

977

`lambda: Literal(Literal("blue sky", "en")),

`

952

978

`Literal("blue sky", "en"),

`

953

979

` ),

`

``

980

`+

(

`

``

981

`+

lambda: Literal("", datatype=RDF.HTML),

`

``

982

`+

LiteralChecker(

`

``

983

`+

..., None, RDF.HTML, True if _HAVE_HTML5LIB else None, ""

`

``

984

`+

),

`

``

985

`+

),

`

``

986

`+

(

`

``

987

`+

lambda: Literal("

", datatype=RDF.HTML),

`

``

988

`+

LiteralChecker(

`

``

989

`+

...,

`

``

990

`+

None,

`

``

991

`+

RDF.HTML,

`

``

992

`+

False if _HAVE_HTML5LIB else None,

`

``

993

`+

"

",

`

``

994

`+

),

`

``

995

`+

),

`

``

996

`+

(

`

``

997

`+

lambda: Literal(_UnknownType(), datatype=EGDC.UnknownType),

`

``

998

`+

LiteralChecker(

`

``

999

`+

_UnknownType(), None, EGDC.UnknownType, None, "_UnknownType()"

`

``

1000

`+

),

`

``

1001

`+

),

`

954

1002

` ],

`

``

1003

`+

ids=literal_idfn,

`

955

1004

`)

`

956

1005

`def test_literal_construction(

`

957

1006

`literal_maker: Callable[[], Literal],

`

`@@ -961,3 +1010,41 @@ def test_literal_construction(

`

961

1010

`with checker.context():

`

962

1011

`actual_outcome = literal_maker()

`

963

1012

`checker.check(actual_outcome)

`

``

1013

+

``

1014

+

``

1015

`+

@pytest.mark.parametrize(

`

``

1016

`+

["literal_maker", "normalize_literals", "outcome"],

`

``

1017

`+

[

`

``

1018

`+

(

`

``

1019

`+

lambda: Literal("001000", datatype=XSD.integer),

`

``

1020

`+

...,

`

``

1021

`+

LiteralChecker(1000, None, XSD.integer, False, "1000"),

`

``

1022

`+

),

`

``

1023

`+

(

`

``

1024

`+

lambda: Literal("001000", datatype=XSD.integer),

`

``

1025

`+

True,

`

``

1026

`+

LiteralChecker(1000, None, XSD.integer, False, "1000"),

`

``

1027

`+

),

`

``

1028

`+

(

`

``

1029

`+

lambda: Literal("001000", datatype=XSD.integer),

`

``

1030

`+

False,

`

``

1031

`+

LiteralChecker(1000, None, XSD.integer, False, "001000"),

`

``

1032

`+

),

`

``

1033

`+

],

`

``

1034

`+

ids=literal_idfn,

`

``

1035

`+

)

`

``

1036

`+

def test_global_normalize(

`

``

1037

`+

literal_maker: Callable[[], Literal],

`

``

1038

`+

normalize_literals: Union[builtins.ellipsis, bool],

`

``

1039

`+

outcome: OutcomePrimitives[Literal],

`

``

1040

`+

) -> None:

`

``

1041

`+

_normalize_literals = rdflib.NORMALIZE_LITERALS

`

``

1042

`+

try:

`

``

1043

`+

if normalize_literals is not ...:

`

``

1044

`+

rdflib.NORMALIZE_LITERALS = normalize_literals

`

``

1045

`+

checker = OutcomeChecker[Literal].from_primitives(outcome)

`

``

1046

`+

with checker.context():

`

``

1047

`+

actual_outcome = literal_maker()

`

``

1048

`+

checker.check(actual_outcome)

`

``

1049

`+

finally:

`

``

1050

`+

rdflib.NORMALIZE_LITERALS = _normalize_literals

`