diff options
author | Graham Higgins <gjhiggins@users.noreply.github.com> | 2022-07-18 13:34:31 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-07-18 14:34:31 +0200 |
commit | 8d5a14ffb39a811add09c41ea2c6a9da3d08325a (patch) | |
tree | 0e79ad17d97fc59586f03d8868a72a45fa0f3dfe | |
parent | 8cb0027ab1e9716473a0445ada5b6390df2ed32c (diff) | |
download | rdflib-8d5a14ffb39a811add09c41ea2c6a9da3d08325a.tar.gz |
fix: InfixOWL: eliminate use of mutable data structures in args (#2033)
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
-rw-r--r-- | CHANGELOG.md | 5 | ||||
-rw-r--r-- | rdflib/extras/infixowl.py | 36 | ||||
-rw-r--r-- | test/test_extras/test_infixowl/test_restriction.py | 7 |
3 files changed, 29 insertions, 19 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d20e086d..ab0a8262 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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. <!-- --> <!-- --> diff --git a/rdflib/extras/infixowl.py b/rdflib/extras/infixowl.py index a17eee3f..ae99d0c3 100644 --- a/rdflib/extras/infixowl.py +++ b/rdflib/extras/infixowl.py @@ -310,7 +310,7 @@ def manchesterSyntax( # 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)) @@ -394,7 +394,7 @@ class Individual(object): 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() @@ -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, @@ -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) @@ -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): @@ -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 @@ -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], @@ -1280,11 +1280,11 @@ class Class(AnnotatableTerms): 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 @@ -1306,7 +1306,7 @@ class Class(AnnotatableTerms): 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)) @@ -1591,7 +1591,7 @@ class BooleanClass(OWLRDFListProxy, Class): ): 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) @@ -1712,7 +1712,7 @@ class Restriction(Class): def __init__( self, onProperty, # noqa: N803 - graph=Graph(), + graph=None, allValuesFrom=None, someValuesFrom=None, value=None, @@ -1721,6 +1721,7 @@ class Restriction(Class): minCardinality=None, identifier=None, ): + graph = Graph() if graph is None else graph super(Restriction, self).__init__( identifier, graph=graph, skipOWLClassMembership=True ) @@ -2147,7 +2148,7 @@ class Property(AnnotatableTerms): 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, @@ -2164,7 +2165,7 @@ class Property(AnnotatableTerms): "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") @@ -2289,19 +2290,20 @@ class Property(AnnotatableTerms): 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 diff --git a/test/test_extras/test_infixowl/test_restriction.py b/test/test_extras/test_infixowl/test_restriction.py index ca621a1b..4f022137 100644 --- a/test/test_extras/test_infixowl/test_restriction.py +++ b/test/test_extras/test_infixowl/test_restriction.py @@ -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") |