From 1c37eb20c85d45ccf7acb08462bc4a1d0d27bc25 Mon Sep 17 00:00:00 2001
From: BUI Van Tuan <buivantuan07@gmail.com>
Date: Fri, 8 Dec 2023 14:10:37 +0100
Subject: [PATCH] fix flake8

---
 dnadna/utils/config.py             | 44 ++++++++++++++----------------
 dnadna/utils/jsonschema.py         | 44 +++++++++++++-----------------
 dnadna/utils/jsonschema_pyref.py   |  4 +--
 dnadna/utils/torch_plugin_mixin.py |  3 +-
 4 files changed, 44 insertions(+), 51 deletions(-)

diff --git a/dnadna/utils/config.py b/dnadna/utils/config.py
index 6c8524cb..25a649b5 100644
--- a/dnadna/utils/config.py
+++ b/dnadna/utils/config.py
@@ -16,7 +16,8 @@ from referencing import Resource
 
 from .. import DEFAULTS_DIR
 from .decorators import cached_classproperty, lru_cache_with_dict
-from .jsonschema import make_config_validator, normpath, SCHEMA_DIRS, CustomValidationError, REGISTRY
+from .jsonschema import make_config_validator, normpath, SCHEMA_DIRS, \
+                        CustomValidationError, REGISTRY
 from .serializers import DictSerializer, JSONSerializer, YAMLSerializer
 from .yaml import CommentedMapping, CommentedYAMLDumper
 
@@ -179,7 +180,7 @@ def save_dict_annotated(obj, filename, schema=None, validate=False,
                 resource = Resource.from_contents(parent_schema, DRAFT7)
                 # Resolver with a specific root resource in case '$ref' is relative
                 schema = validator.registry.resolver_with_root(resource).lookup(ref).contents
-            
+
             if not ref.startswith('#'):
                 return get_comment(schema, schema)
             else:
@@ -215,7 +216,7 @@ def save_dict_annotated(obj, filename, schema=None, validate=False,
                 resource = Resource.from_contents(parent_schema, DRAFT7)
                 # Resolver with a specific root resource in case '$ref' is relative
                 schema = validator.registry.resolver_with_root(resource).lookup(ref).contents
-            
+
             if not ref.startswith('#'):
                 get_comments_recursive(schema, schema)
             else:
@@ -1487,8 +1488,8 @@ def _get_config_key_path(config, key):
 
 class ConfigValidator():
     """
-    A custom validator wrapping `jsonschema.Draft7Validator` class which supports special validation
-    functionality for DNADNA `Config` objects::
+    A custom validator wrapping `jsonschema.Draft7Validator` class which
+    supports special validation functionality for DNADNA `Config` objects::
 
     * Recognizes `Config` objects as JSON ``object`` s.
 
@@ -1546,7 +1547,7 @@ class ConfigValidator():
     Config({'abspath': '/bar/baz/qux', 'relpath': '/foo/bar/fred',
         'nonpath': 'barney', 'has_default_2': 'c', 'has_default_1': 'a'})
     """
-    
+
     def __init__(self, schema, *args, resolve_plugins=True,
                  resolve_defaults=True, resolve_filenames=True,
                  posixify_filenames=False, **kwargs):
@@ -1554,23 +1555,22 @@ class ConfigValidator():
         self._resolve_defaults = resolve_defaults
         self._resolve_filenames = resolve_filenames
         self._posixify_filenames = posixify_filenames
-        
+
         # Creates a new `jsonschema.Draft7Validator` class fully overriding the built in of the
         # `jsonschema.Draft7Validator` by default
-        validator = make_config_validator(get_path=_get_config_key_path, 
+        validator = make_config_validator(get_path=_get_config_key_path,
                                           resolve_plugins=self._resolve_plugins,
                                           resolve_defaults=self._resolve_defaults,
                                           resolve_filenames=self._resolve_filenames,
                                           posixify_filenames=self._posixify_filenames)
-        
-        self.validator = validator(schema, registry=REGISTRY, format_checker=validator.FORMAT_CHECKER)
+
+        self.validator = validator(schema, registry=REGISTRY,
+                                   format_checker=validator.FORMAT_CHECKER)
         self.registry = REGISTRY
-        
-    
+
     def iter_errors(self, config, *args, **kwargs):
         return self.validator.iter_errors(config, *args, **kwargs)
-    
-    
+
     @staticmethod
     def relevance_with_const_select(error):
         """
@@ -1735,17 +1735,16 @@ class ConfigValidator():
             return (True,) + rank
         else:
             return (False,) + rank
-        
-        
+
     def best_match(self, errors, key=relevance_with_const_select):
         """
         Wraps the `jsonschema.exceptions.best_match` to return `CustomValidatonError`
         See the `relevance_with_const_select` documentation above
         """
-        
+
         error = best_match(errors,
                        key=key)
-        
+
         if error is None:
             return
         else:
@@ -1757,8 +1756,7 @@ class ConfigValidator():
                 error = CustomValidationError.create_from(error)
                 error.message = errormsg
             return error
-        
-    
+
     def validate(self, config, *args, **kwargs):
         """
         Validate the config against the schema and raise a `ConfigError` if
@@ -1875,10 +1873,10 @@ class ConfigValidator():
         try:
             error = best_match(self.iter_errors(config, *args, **kwargs),
                            key=self.relevance_with_const_select)
-            
+
             if error is None:
                 return
-            
+
             raise error
         except jsonschema.ValidationError as exc:
             # if the error came from a sub-schema in a one/all/anyOf then the
@@ -1892,7 +1890,7 @@ class ConfigValidator():
             if errormsg is not None:
                 exc = CustomValidationError.create_from(exc)
                 exc.message = errormsg
-            
+
             path = ()
             while parent is not None:
                 if len(parent.path) > len(path):
diff --git a/dnadna/utils/jsonschema.py b/dnadna/utils/jsonschema.py
index cc376608..40a6a206 100644
--- a/dnadna/utils/jsonschema.py
+++ b/dnadna/utils/jsonschema.py
@@ -127,7 +127,7 @@ def make_config_validator(validator_cls=jsonschema.Draft7Validator,
     ``validator_cls`` (`jsonschema.Draft7Validator` by default) which supports
     special functionality for DNADNA `.Config` objects, though it can be adapted
     to other types.
-    
+
     The new validator has additional options for controlling how to resolve
     relative filenames in the schema and how to handle the ``default`` property
     in schemas.
@@ -151,16 +151,16 @@ def make_config_validator(validator_cls=jsonschema.Draft7Validator,
     # "between releases of jsonschema. Instead, prefer "
     # "composition of validators, wrapping them in an object "
     # "owned entirely by the downstream library."
-    
-    # From now on, the attributs 'resolves' are to be passed to ConfigValidaror class 
+
+    # From now on, the attributs 'resolves' are to be passed to ConfigValidaror class
     # that wraps the new Draft7Validator
     def resolve_filename(instance, prop, subschema, get_path=None,
                          posixify=False):
         errors = []
-        
+
         if subschema is True:
             return []
-        
+
         # Handle format: filename
         format_ = subschema.get('format')
         if (subschema.get('type') == 'string' and
@@ -203,7 +203,7 @@ def make_config_validator(validator_cls=jsonschema.Draft7Validator,
 
         err_occurred = False
         orig_instance = copy.deepcopy(instance)
-        
+
         # If there are no errors then the instance matches this schema or
         # sub-schema (in the case of a oneOf/allOf/anyOf); now we assign any
         # defaults so that defaults are only added from a schema that this
@@ -215,19 +215,19 @@ def make_config_validator(validator_cls=jsonschema.Draft7Validator,
             for prop, subschema in properties.items():
                 if subschema is True:
                     continue
-                
+
                 # Fill missing props with their defaults
                 if (isinstance(instance, (dict, Mapping)) and
                         'default' in subschema and prop not in instance):
                     instance[prop] = subschema['default']
                     added_defaults = True
-                    
+
             if added_defaults:
                 # We only need to re-validate if any defaults were actually
                 # assigned
                 errors = validate_properties(validator, properties, instance,
                                              schema)
-                
+
         try:
             for error in errors:
                 err_occurred = True
@@ -240,7 +240,6 @@ def make_config_validator(validator_cls=jsonschema.Draft7Validator,
             if err_occurred:
                 instance.clear()
                 instance.update(orig_instance)
-                
 
     # Create the format checker; filename and filename! are already handled
     # specially in validate_config_properties since it requires special support
@@ -254,18 +253,16 @@ def make_config_validator(validator_cls=jsonschema.Draft7Validator,
             __import__(instance)
 
         return True
-    
+
     # supports special functionality for DNADNA `.Config` objects
     def is_config(checker, instance):
         return (
             isinstance(instance, (dict, Mapping))
         )
-    type_checker = validator_cls.TYPE_CHECKER.redefine(
-            "object", is_config,
-        )
-    
+    type_checker = validator_cls.TYPE_CHECKER.redefine("object", is_config)
+
     # Creates a new `jsonschema.Draft7Validator` class fully overriding the built in of the
-    # ``validator_cls`` (`jsonschema.Draft7Validator` by default) 
+    # ``validator_cls`` (`jsonschema.Draft7Validator` by default)
     # This is to work around the issue in
     # https://github.com/python-jsonschema/jsonschema/issues/1197
     validator_cls = jsonschema.validators.create(
@@ -326,7 +323,7 @@ def _resolver_file_handler(uri):
     Slightly hackish, but supported workaround to the larger issue discussed at
     https://github.com/Julian/jsonschema/issues/420
     """
-    
+
     filename = url2pathname(urlparse(uri).path)
     return DictSerializer.load(filename)
 
@@ -348,30 +345,27 @@ def _retrieve(uri: str):
 
     """
     base_url = f'file:///{SCHEMA_DIRS[0].as_posix()}/'
-    
+
     scheme = urlparse(uri).scheme
     if not scheme:
         uri = base_url + uri
         scheme = urlparse(uri).scheme
     if scheme == 'file':
-        try:
-            contents = _resolver_file_handler(uri)
-        except:
-            contents = {}
+        contents = _resolver_file_handler(uri)
     elif scheme == 'py-obj':
         contents = _resolve_url_py_obj(uri)
     else:
         contents = {}
-    
+
     if not contents.get('$id'):
         contents['$id'] = 'urn:unknown-dialect'
-    
+
     return Resource.from_contents(contents, DRAFT7)
 
 
 # a specific immutable set of in-memory schemas to be available in addition to the dynamic schemas
 REGISTRY = Registry(retrieve=_retrieve)
-# include all in-memory schemas in the directory SCHEMA_DIRS, 
+# include all in-memory schemas in the directory SCHEMA_DIRS,
 # making them available for use during validation.
 for schema_dir in SCHEMA_DIRS:
     for curdir, dirs, files in os.walk(schema_dir):
diff --git a/dnadna/utils/jsonschema_pyref.py b/dnadna/utils/jsonschema_pyref.py
index 88802399..38e5f9a8 100644
--- a/dnadna/utils/jsonschema_pyref.py
+++ b/dnadna/utils/jsonschema_pyref.py
@@ -10,8 +10,8 @@ try:
     import yaml  # type: ignore
 except ImportError:
     yaml = None
-    
-    
+
+
 __all__ = ['urljoin', 'urlparse']
 
 
diff --git a/dnadna/utils/torch_plugin_mixin.py b/dnadna/utils/torch_plugin_mixin.py
index 9f441c95..94f7eb18 100644
--- a/dnadna/utils/torch_plugin_mixin.py
+++ b/dnadna/utils/torch_plugin_mixin.py
@@ -291,7 +291,8 @@ class TorchPluginMixin:
             param_schema = {'type': 'array'}
             # param_schema['items'] = [cls._schema_from_default(v, False)
             #                          for v in value]
-            # the items keyword to a single schema that will be used to validate all of the items in the array.
+            # the items keyword to a single schema that will be used to
+            # validate all of the items in the array.
             param_schema['items'] = cls._schema_from_default(value[0], False)
             value = list(value)
         elif value is None:
-- 
GitLab