Skip to content

Commit

Permalink
fix: InfixOWL: eliminate use of mutable data structures in args (#2033)
Browse files Browse the repository at this point in the history
Maintenance-positive changes recommended by
[flake8-bugbear](https://pypi.org/project/flake8-bugbear/), mostly explicitly
indicating unused loop variables with a prefixing underscore (it identified one
case where _none_ of the loop variables were being used) and a couple of
don't-use-mutable-objects-as-args instances.

1. Added leading underscore to unused loop variables, removed mutable objects
   from args, (replacing with None and then performing an explicit “if None”
   assignment in the body of the method).

2. Swapped out an excessively weak, coverage-motivated length test of __hash__()
   (which was causing intermittent test failures) for an equality test of two
   runs, fixes issue #2030
  • Loading branch information
Graham Higgins committed Jul 18, 2022
1 parent 8cb0027 commit 8d5a14f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ and will be removed for release.
<!-- -->

- InfixOWL fixes and cleanup.
[PR #2024](https://github.com/RDFLib/rdflib/pull/2024).
Closed [issue #2030](https://github.com/RDFLib/rdflib/issues/2030).
[PR #2024](https://github.com/RDFLib/rdflib/pull/2024),
and [PR #2033](https://github.com/RDFLib/rdflib/pull/2033).
- `rdflib.extras.infixowl.Restriction.__init__` will now raise a `ValueError`
if there is no restriction value instead of an `AssertionError`.
- Fixed numerous issues with
Expand All @@ -31,6 +33,7 @@ and will be removed for release.
- Added `rdflib.extras.infixowl.MalformedClassError` which will replace
`rdflib.extras.infixowl.MalformedClass` (which is an exception) in the next
major version.
- Eliminated the use of mutable data structures in some argument defaults.

<!-- -->
<!-- -->
Expand Down
36 changes: 19 additions & 17 deletions rdflib/extras/infixowl.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def castToQName(x): # noqa: N802
OWL.minCardinality: "MIN",
OWL.cardinality: "EQUALS",
}
for s, p, o in store.triples_choices((thing, list(cardlookup.keys()), None)):
for _s, p, o in store.triples_choices((thing, list(cardlookup.keys()), None)):
return "( %s %s %s )" % (propstring, cardlookup[p], o)
# is thing a complement of anything
compl = list(store.objects(subject=thing, predicate=OWL.complementOf))
Expand Down Expand Up @@ -394,7 +394,7 @@ def delete(self):
self.clearOutDegree()

def replace(self, other):
for s, p, o in self.graph.triples((None, None, self.identifier)):
for s, p, _o in self.graph.triples((None, None, self.identifier)):
self.graph.add((s, p, classOrIdentifier(other)))
self.delete()

Expand Down Expand Up @@ -716,7 +716,7 @@ def AllClasses(graph): # noqa: N802

def AllProperties(graph): # noqa: N802
prevprops = set()
for s, p, o in graph.triples_choices(
for s, _p, o in graph.triples_choices(
(
None,
RDF.type,
Expand Down Expand Up @@ -787,7 +787,7 @@ def ComponentTerms(cls): # noqa: N802
if OWL.Restriction in cls.type:
try:
cls = CastClass(cls, Individual.factoryGraph)
for s, p, inner_class_id in cls.factoryGraph.triples_choices(
for _s, _p, inner_class_id in cls.factoryGraph.triples_choices(
(cls.identifier, [OWL.allValuesFrom, OWL.someValuesFrom], None)
):
inner_class = Class(inner_class_id, skipOWLClassMembership=True)
Expand Down Expand Up @@ -815,7 +815,7 @@ def ComponentTerms(cls): # noqa: N802
yield _c
else:
yield inner_class
for s, p, o in cls.factoryGraph.triples_choices(
for _s, _p, o in cls.factoryGraph.triples_choices(
(classOrIdentifier(cls), CLASS_RELATIONS, None)
):
if isinstance(o, BNode):
Expand Down Expand Up @@ -917,7 +917,7 @@ def CastClass(c, graph=None): # noqa: N802
for kind in graph.objects(subject=classOrIdentifier(c), predicate=RDF.type):
if kind == OWL.Restriction:
kwargs = {"identifier": classOrIdentifier(c), "graph": graph}
for s, p, o in graph.triples((classOrIdentifier(c), None, None)):
for _s, p, o in graph.triples((classOrIdentifier(c), None, None)):
if p != RDF.type:
if p == OWL.onProperty:
kwargs["onProperty"] = o
Expand All @@ -931,7 +931,7 @@ def CastClass(c, graph=None): # noqa: N802
raise MalformedClassError("Malformed owl:Restriction")
return Restriction(**kwargs)
else:
for s, p, o in graph.triples_choices(
for _s, p, _o in graph.triples_choices(
(
classOrIdentifier(c),
[OWL.intersectionOf, OWL.unionOf, OWL.oneOf],
Expand Down Expand Up @@ -1280,11 +1280,11 @@ def isPrimitive(self): # noqa: N802
return False
# sc = list(self.subClassOf)
ec = list(self.equivalentClass)
for boolclass, p, rdf_list in self.graph.triples_choices(
for _boolclass, p, rdf_list in self.graph.triples_choices(
(self.identifier, [OWL.intersectionOf, OWL.unionOf], None)
):
ec.append(manchesterSyntax(rdf_list, self.graph, boolean=p))
for e in ec:
for _e in ec:
return False
if self.complementOf:
return False
Expand All @@ -1306,7 +1306,7 @@ def __repr__(self, full=False, normalization=True):
exprs = []
sc = list(self.subClassOf)
ec = list(self.equivalentClass)
for boolclass, p, rdf_list in self.graph.triples_choices(
for _boolclass, p, rdf_list in self.graph.triples_choices(
(self.identifier, [OWL.intersectionOf, OWL.unionOf], None)
):
ec.append(manchesterSyntax(rdf_list, self.graph, boolean=p))
Expand Down Expand Up @@ -1591,7 +1591,7 @@ def __init__(
):
if operator is None:
props = []
for s, p, o in graph.triples_choices(
for _s, p, _o in graph.triples_choices(
(identifier, [OWL.intersectionOf, OWL.unionOf], None)
):
props.append(p)
Expand Down Expand Up @@ -1712,7 +1712,7 @@ class Restriction(Class):
def __init__(
self,
onProperty, # noqa: N803
graph=Graph(),
graph=None,
allValuesFrom=None,
someValuesFrom=None,
value=None,
Expand All @@ -1721,6 +1721,7 @@ def __init__(
minCardinality=None,
identifier=None,
):
graph = Graph() if graph is None else graph
super(Restriction, self).__init__(
identifier, graph=graph, skipOWLClassMembership=True
)
Expand Down Expand Up @@ -2147,7 +2148,7 @@ def __repr__(self):
OWL.SymmetricProperty in self.type and " Symmetric" or "",
)
)
for s, p, roletype in self.graph.triples_choices(
for _s, _p, roletype in self.graph.triples_choices(
(
self.identifier,
RDF.type,
Expand All @@ -2164,7 +2165,7 @@ def __repr__(self):
"DatatypeProperty( %s %s"
% (self.qname, first(self.comment) and first(self.comment) or "")
)
for s, p, roletype in self.graph.triples(
for _s, _p, roletype in self.graph.triples(
(self.identifier, RDF.type, OWL.FunctionalProperty)
):
rt.append(" Functional")
Expand Down Expand Up @@ -2289,19 +2290,20 @@ def _del_range(self):

def replace(self, other):
# extension = []
for s, p, o in self.extent:
for s, _p, o in self.extent:
self.graph.add((s, propertyOrIdentifier(other), o))
self.graph.remove((None, self.identifier, None))


def CommonNSBindings(graph, additionalNS={}): # noqa: N802, N803
def CommonNSBindings(graph, additionalNS=None): # noqa: N802, N803
"""
Takes a graph and binds the common namespaces (rdf,rdfs, & owl)
"""
additional_ns = {} if additionalNS is None else additionalNS
namespace_manager = NamespaceManager(graph)
namespace_manager.bind("rdfs", RDFS)
namespace_manager.bind("rdf", RDF)
namespace_manager.bind("owl", OWL)
for prefix, uri in list(additionalNS.items()):
for prefix, uri in list(additional_ns.items()):
namespace_manager.bind(prefix, uri, override=False)
graph.namespace_manager = namespace_manager
7 changes: 6 additions & 1 deletion test/test_extras/test_infixowl/test_restriction.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ def test_restriction_str_and_hash(graph):
r1.serialize(sg)

assert r1.isPrimitive() is False
assert len(str(r1.__hash__())) > 17

r1hashfirstrun = r1.__hash__()

r1hashsecondrun = r1.__hash__()

assert r1hashfirstrun == r1hashsecondrun

assert list(Property(EXNS.someProp, baseType=None).type) == [
URIRef("http://www.w3.org/2002/07/owl#DatatypeProperty")
Expand Down

0 comments on commit 8d5a14f

Please sign in to comment.