Skip to content

Commit

Permalink
fix: eliminate bare except:
Browse files Browse the repository at this point in the history
Replace bare `except:` with `except Exception`, there are some cases where it
can be narrowed further, but this is already an improvement over the current
situation.

This is somewhat pursuant to eliminating
[flakeheaven](https://github.com/flakeheaven/flakeheaven), as it no longer
supports the latest version of flake8
[[ref](flakeheaven/flakeheaven#132)]. But it also is
just the right thing to do as bare exceptions can cause problems.
  • Loading branch information
aucampia committed Apr 11, 2023
1 parent 5c3c78a commit ae54dbf
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion rdflib/namespace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ def normalizeUri(self, rdfTerm: str) -> str:
if namespace not in self.__strie:
insert_strie(self.__strie, self.__trie, str(namespace))
namespace = URIRef(str(namespace))
except:
except Exception:
if isinstance(rdfTerm, Variable):
return "?%s" % rdfTerm
else:
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/parsers/notation3.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def becauseSubexpression(*args: Any, **kargs: Any) -> None:
def unicodeExpand(m: Match) -> str:
try:
return chr(int(m.group(1), 16))
except:
except Exception:
raise Exception("Invalid unicode code point: " + m.group(1))


Expand Down Expand Up @@ -1711,7 +1711,7 @@ def _unicodeEscape(
)
try:
return i + n, reg.sub(unicodeExpand, "\\" + prefix + argstr[i : i + n])
except:
except Exception:
raise BadSyntax(
self._thisDoc,
startline,
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/parsers/trix.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def startElementNS(

try:
self.lang = attrs.getValue((str(XMLNS), "lang"))
except:
except Exception:
# language not required - ignore
pass
try:
Expand All @@ -122,7 +122,7 @@ def startElementNS(
self.datatype = None
try:
self.lang = attrs.getValue((str(XMLNS), "lang"))
except:
except Exception:
# language not required - ignore
pass

Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/longturtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def getQName(self, uri, gen_prefix=True):

try:
parts = self.store.compute_qname(uri, generate=gen_prefix)
except:
except Exception:
# is the uri a namespace in itself?
pfx = self.store.store.prefix(uri)

Expand Down Expand Up @@ -245,7 +245,7 @@ def isValidList(self, l_):
try:
if self.store.value(l_, RDF.first) is None:
return False
except:
except Exception:
return False
while l_:
if l_ != RDF.nil and len(list(self.store.predicate_objects(l_))) != 2:
Expand Down
2 changes: 1 addition & 1 deletion rdflib/plugins/serializers/rdfxml.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def subject(self, subject: IdentifiedNode, depth: int = 1):
try:
# type error: Argument 1 to "qname" of "NamespaceManager" has incompatible type "Optional[Node]"; expected "str"
self.nm.qname(type) # type: ignore[arg-type]
except:
except Exception:
type = None

element = type or RDFVOC.Description
Expand Down
4 changes: 2 additions & 2 deletions rdflib/plugins/serializers/turtle.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def getQName(self, uri, gen_prefix=True):

try:
parts = self.store.compute_qname(uri, generate=gen_prefix)
except:
except Exception:
# is the uri a namespace in itself?
pfx = self.store.store.prefix(uri)

Expand Down Expand Up @@ -397,7 +397,7 @@ def isValidList(self, l_):
try:
if self.store.value(l_, RDF.first) is None:
return False
except:
except Exception:
return False
while l_:
if l_ != RDF.nil and len(list(self.store.predicate_objects(l_))) != 2:
Expand Down
2 changes: 1 addition & 1 deletion rdflib/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def __eq__(self, other: Any) -> bool:
return self.vars == other.vars and self.bindings == other.bindings
else:
return self.graph == other.graph
except:
except Exception:
return False


Expand Down
2 changes: 1 addition & 1 deletion rdflib/tools/csv2rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ def convert(self, csvreader):
"%d rows, %d triples, elapsed %.2fs.\n"
% (rows, self.triples, time.time() - start)
)
except:
except Exception:
sys.stderr.write("Error processing line: %d\n" % rows)
raise

Expand Down
4 changes: 2 additions & 2 deletions rdflib/tools/rdf2dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def label(x, g):
return l_
try:
return g.namespace_manager.compute_qname(x)[2]
except:
except Exception:
return x

def formatliteral(l, g):
Expand All @@ -113,7 +113,7 @@ def qname(x, g):
try:
q = g.compute_qname(x)
return q[0] + ":" + q[2]
except:
except Exception:
return x

def color(p):
Expand Down
2 changes: 1 addition & 1 deletion rdflib/tools/rdfs2dot.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def label(xx, grf):
if lbl is None:
try:
lbl = grf.namespace_manager.compute_qname(xx)[2]
except:
except Exception:
pass # bnodes and some weird URIs cannot be split
return lbl

Expand Down

0 comments on commit ae54dbf

Please sign in to comment.