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

Lazy logging #462

Merged
merged 9 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 8 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
11 changes: 5 additions & 6 deletions sbol3/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import logging
import os
import posixpath
import warnings
from pathlib import Path
from typing import Dict, Callable, List, Optional, Any, Union, Iterable

# import typing for typing.Sequence, which we don't want to confuse
# with sbol3.Sequence
import typing as pytyping
import warnings
from pathlib import Path
from typing import Any, Callable, Dict, Iterable, List, Optional, Union

import pyshacl
import rdflib
Expand Down Expand Up @@ -124,7 +123,7 @@ def _build_extension_object(self, identity: str, sbol_type: str,
build_type = type_uri
break
except KeyError:
logging.warning(f'No builder found for {type_uri}')
logging.warning('No builder for %s', type_uri)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should maintain the text of the warning message unless there is a good reason to change it.

Suggested change
logging.warning('No builder for %s', type_uri)
logging.warning('No builder found for %s', type_uri)

if builder is None:
builder = custom_types[sbol_type]
build_type = types[0]
Expand Down Expand Up @@ -162,7 +161,7 @@ def _build_object(self, identity: str, types: List[str]) -> Optional[Identified]
try:
builder = self._uri_type_map[sbol_type]
except KeyError:
logging.warning(f'No builder found for {sbol_type}')
logging.warning('No builder found for %s', sbol_type)
raise SBOLError(f'Unknown type {sbol_type}')
result = builder(identity=identity, type_uri=sbol_type)
# Fix https://github.com/SynBioDex/pySBOL3/issues/264
Expand Down
4 changes: 2 additions & 2 deletions sbol3/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import uuid
import warnings
from collections import defaultdict
from typing import Callable, Dict, Optional, Union
from urllib.parse import urlparse
from typing import Dict, Callable, Optional, Union

from . import *

Expand Down Expand Up @@ -115,7 +115,7 @@ def copy(self, target_doc=None, target_namespace=None):
try:
builder = BUILDER_REGISTER[self.type_uri]
except KeyError:
logging.warning(f'No builder found for {self.type_uri}; assuming {self.__class__.__name__}')
logging.warning('No builder found for %s; assuming %s', self.type_uri, self.__class__.__name__)
builder = self.__class__
new_obj = builder(**dict(identity=new_uri, type_uri=self.type_uri))

Expand Down
2 changes: 0 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ disable = abstract-class-instantiated,
disallowed-name,
implicit-str-concat,
import-outside-toplevel,
logging-not-lazy,
consider-using-enumerate,
consider-using-f-string,
cyclic-import,
Expand All @@ -30,7 +29,6 @@ disable = abstract-class-instantiated,
inconsistent-return-statements,
invalid-name,
isinstance-second-argument-not-valid-type,
logging-fstring-interpolation,
missing-class-docstring,
missing-function-docstring,
missing-module-docstring,
Expand Down
9 changes: 5 additions & 4 deletions test/test_examples.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import logging
import os
import shutil
import subprocess
import sys
import unittest
import tempfile
import shutil
import unittest

import rdflib
import rdflib.compare

Expand Down Expand Up @@ -48,8 +49,8 @@ def test_circuit_example(self):
expected_iso = rdflib.compare.to_isomorphic(expected_graph)
rdf_diff = rdflib.compare.graph_diff(expected_iso, actual_iso)
if rdf_diff[1] or rdf_diff[2]:
self.logger.warning('Detected %d different RDF triples in %s' %
(len(rdf_diff[1]) + len(rdf_diff[2]), out_path))
self.logger.warning('Detected %d different RDF triples in %s',
len(rdf_diff[1]) + len(rdf_diff[2]), out_path)
for stmt in rdf_diff[1]:
self.logger.warning('Only in expected: %r', stmt)
for stmt in rdf_diff[2]:
Expand Down
9 changes: 6 additions & 3 deletions test/test_roundtrip.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,11 @@ def run_round_trip_file(self, test_path, file_format):
iso2 = rdflib.compare.to_isomorphic(g2)
rdf_diff = rdflib.compare.graph_diff(iso1, iso2)
if rdf_diff[1] or rdf_diff[2]:
self.logger.warning('Detected %d different RDF triples in %s' %
(len(rdf_diff[1]) + len(rdf_diff[2]), test_path))
self.logger.warning(
'Detected %d different RDF triples in %s',
len(rdf_diff[1]) + len(rdf_diff[2]), test_path
)

if not self.logger.isEnabledFor(logging.DEBUG):
self.logger.warning('Set environment variable %s to see details',
DEBUG_ENV_VAR)
Expand All @@ -165,7 +168,7 @@ def test_sbol3_files(self):
for test_file in self.find_all_files(test_dir):
basename = os.path.basename(test_file)
if os.path.splitext(basename)[0] in skip_list:
self.logger.debug(f'Skipping {test_file}')
self.logger.debug('Skipping %s', test_file)
continue
file_format = self.rdf_type(test_file)
if not file_format:
Expand Down