Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove (TypeCheck|SubjectType|PredicateType|ObjectType)Error and related #1640

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 0 additions & 60 deletions rdflib/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

__all__ = [
"Error",
"TypeCheckError",
"SubjectTypeError",
"PredicateTypeError",
"ObjectTypeError",
"ContextTypeError",
"ParserError",
]

Expand All @@ -21,61 +16,6 @@ def __init__(self, msg=None):
self.msg = msg


class TypeCheckError(Error):
"""Parts of assertions are subject to type checks."""

def __init__(self, node):
Error.__init__(self, node)
self.type = type(node)
self.node = node


class SubjectTypeError(TypeCheckError):
"""Subject of an assertion must be an instance of URIRef."""

def __init__(self, node):
TypeCheckError.__init__(self, node)
self.msg = "Subject must be instance of URIRef or BNode: %s(%s)" % (
self.node,
self.type,
)


class PredicateTypeError(TypeCheckError):
"""Predicate of an assertion must be an instance of URIRef."""

def __init__(self, node):
TypeCheckError.__init__(self, node)
self.msg = "Predicate must be a URIRef instance: %s(%s)" % (
self.node,
self.type,
)


class ObjectTypeError(TypeCheckError):
"""Object of an assertion must be an instance of URIRef, Literal,
or BNode."""

def __init__(self, node):
TypeCheckError.__init__(self, node)
self.msg = (
"\
Object must be instance of URIRef, Literal, or BNode: %s(%s)"
% (self.node, self.type)
)


class ContextTypeError(TypeCheckError):
"""Context of an assertion must be an instance of URIRef."""

def __init__(self, node):
TypeCheckError.__init__(self, node)
self.msg = "Context must be instance of URIRef or BNode: %s(%s)" % (
self.node,
self.type,
)


class ParserError(Error):
"""RDF Parser error."""

Expand Down
68 changes: 0 additions & 68 deletions rdflib/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
* date_time
* parse_date_time

Statement and component type checkers

* check_context
* check_subject
* check_predicate
* check_object
* check_statement
* check_pattern

"""

from calendar import timegm
Expand All @@ -41,10 +32,6 @@

from os.path import splitext

from rdflib.exceptions import ContextTypeError
from rdflib.exceptions import ObjectTypeError
from rdflib.exceptions import PredicateTypeError
from rdflib.exceptions import SubjectTypeError
import rdflib.graph # avoid circular dependency
from rdflib.namespace import Namespace, XSD
from rdflib.namespace import NamespaceManager
Expand All @@ -62,12 +49,6 @@
"from_n3",
"date_time",
"parse_date_time",
"check_context",
"check_subject",
"check_predicate",
"check_object",
"check_statement",
"check_pattern",
"guess_format",
"find_roots",
"get_tree",
Expand Down Expand Up @@ -235,55 +216,6 @@ def from_n3(s: str, default=None, backend=None, nsm=None):
return BNode(s)


def check_context(c):
if not (isinstance(c, URIRef) or isinstance(c, BNode)):
raise ContextTypeError("%s:%s" % (c, type(c)))


def check_subject(s):
"""Test that s is a valid subject identifier."""
if not (isinstance(s, URIRef) or isinstance(s, BNode)):
raise SubjectTypeError(s)


def check_predicate(p):
"""Test that p is a valid predicate identifier."""
if not isinstance(p, URIRef):
raise PredicateTypeError(p)


def check_object(o):
"""Test that o is a valid object identifier."""
if not (isinstance(o, URIRef) or isinstance(o, Literal) or isinstance(o, BNode)):
raise ObjectTypeError(o)


def check_statement(triple):
(s, p, o) = triple
if not (isinstance(s, URIRef) or isinstance(s, BNode)):
raise SubjectTypeError(s)

if not isinstance(p, URIRef):
raise PredicateTypeError(p)

if not (isinstance(o, URIRef) or isinstance(o, Literal) or isinstance(o, BNode)):
raise ObjectTypeError(o)


def check_pattern(triple):
(s, p, o) = triple
if s and not (isinstance(s, URIRef) or isinstance(s, BNode)):
raise SubjectTypeError(s)

if p and not isinstance(p, URIRef):
raise PredicateTypeError(p)

if o and not (
isinstance(o, URIRef) or isinstance(o, Literal) or isinstance(o, BNode)
):
raise ObjectTypeError(o)


def date_time(t=None, local_time_zone=False):
"""http://www.w3.org/TR/NOTE-datetime ex: 1997-07-16T19:20:30Z

Expand Down
61 changes: 0 additions & 61 deletions test/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
from rdflib.term import URIRef
from rdflib import util
from rdflib import XSD
from rdflib.exceptions import SubjectTypeError
from rdflib.exceptions import PredicateTypeError
from rdflib.exceptions import ObjectTypeError
from rdflib.exceptions import ContextTypeError

n3source = """\
@prefix : <http://www.w3.org/2000/10/swap/Primer#>.
Expand Down Expand Up @@ -347,60 +343,3 @@ def test_util_from_n3_not_escapes(self, string: str) -> None:
def test_util_from_n3_not_escapes_xf(self, string: str) -> None:
literal_str = str(util.from_n3(f'"{string}"'))
assert literal_str == f"{string}"


class TestUtilCheckers:
def setup_method(self):
self.c = URIRef("http://example.com")
self.s = BNode("http://example.com")
self.p = URIRef("http://example.com/predicates/isa")
self.o = Literal("Objectification")

def test_util_checker_exceptions(self):
c = "http://example.com"
with pytest.raises(ContextTypeError):
util.check_context(c)
with pytest.raises(SubjectTypeError):
util.check_subject(c)
with pytest.raises(PredicateTypeError):
util.check_predicate(c)
with pytest.raises(ObjectTypeError):
util.check_object(c)

def test_util_check_context(self):
res = util.check_context(self.c)
assert res == None

def test_util_check_subject(self):
res = util.check_subject(self.s)
assert res == None

def test_util_check_predicate(self):
res = util.check_predicate(self.p)
assert res == None

def test_util_check_object(self):
res = util.check_object(self.o)
assert res == None

def test_util_check_statement(self):
c = "http://example.com"
with pytest.raises(SubjectTypeError):
util.check_statement((c, self.p, self.o))
with pytest.raises(PredicateTypeError):
util.check_statement((self.s, c, self.o))
with pytest.raises(ObjectTypeError):
util.check_statement((self.s, self.p, c))
res = util.check_statement((self.s, self.p, self.o))
assert res == None

def test_util_check_pattern(self):
c = "http://example.com"
with pytest.raises(SubjectTypeError):
util.check_pattern((c, self.p, self.o))
with pytest.raises(PredicateTypeError):
util.check_pattern((self.s, c, self.o))
with pytest.raises(ObjectTypeError):
util.check_pattern((self.s, self.p, c))
res = util.check_pattern((self.s, self.p, self.o))
assert res == None
33 changes: 0 additions & 33 deletions test/type_check.py

This file was deleted.