Skip to content

Commit

Permalink
Added PyLint analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Collins committed Nov 24, 2020
1 parent 63b4c1f commit 4c14e35
Show file tree
Hide file tree
Showing 25 changed files with 77 additions and 131 deletions.
19 changes: 0 additions & 19 deletions .deepsource.toml

This file was deleted.

51 changes: 0 additions & 51 deletions .github/workflows/codeql-analysis.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ jobs:
- name: Analyse with MyPy
run: poetry run mypy src

- name: Lint with PyLint
run: poetry run pylint .\src\labthings\

- name: Test with pytest
run: poetry run pytest

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ jobs:
- name: Analyse with MyPy
run: poetry run mypy src

- name: Lint with PyLint
run: poetry run pylint .\src\labthings\

- name: Test with pytest
run: poetry run pytest

Expand Down
9 changes: 8 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ sphinx-rtd-theme = "^0.5.0"
mypy = "^0.790"

[tool.black]
exclude = '(\.eggs|\.git|\.venv)'
exclude = '(\.eggs|\.git|\.venv|node_modules/)'

[tool.isort]
multi_line_output = 3
Expand All @@ -44,6 +44,13 @@ use_parentheses = true
ensure_newline_before_comments = true
line_length = 88

[tool.pylint.'MESSAGES CONTROL']
disable = "fixme,C,R"
max-line-length = 88

[tool.pylint.'MASTER']
ignore = "marshmallow_jsonschema"

[build-system]
requires = ["poetry>=0.12"]
build-backend = "poetry.masonry.api"
Expand Down
9 changes: 4 additions & 5 deletions src/labthings/actions/thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __init__(

# copy_current_request_context allows threads to access flask current_app
if has_request_context():
logging.debug(f"Copying request context to {self._target}")
logging.debug("Copying request context to %s", self._target)
self._target = copy_current_request_context(self._target)
try:
self.input = request.json
Expand Down Expand Up @@ -298,7 +298,7 @@ def terminate(self, exception=ActionKilledException) -> bool:
:raises which: should cause the thread to exit silently
"""
_LOG.warning(f"Terminating thread {self}")
_LOG.warning("Terminating thread %s", self)
if not (self.is_alive() or self._is_thread_proc_running()):
logging.debug("Cannot kill thread that is no longer running.")
return False
Expand Down Expand Up @@ -336,7 +336,7 @@ def stop(self, timeout=None, exception=ActionKilledException) -> bool:
self._status = "cancelled"
return True
# If the timeout tracker stopped before the thread died, kill it
logging.warning(f"Forcefully terminating thread {self}")
logging.warning("Forcefully terminating thread %s", self)
return self.terminate(exception=exception)


Expand All @@ -346,7 +346,6 @@ def __init__(
thread: ActionThread,
dest: LockableDeque,
level=logging.INFO,
default_log_len: int = 100,
):
"""Set up a log handler that appends messages to a list.
Expand All @@ -371,7 +370,7 @@ def __init__(
self.dest = dest
self.addFilter(self.check_thread)

def check_thread(self, record):
def check_thread(self, *_):
"""Determine if a thread matches the desired record
:param record:
Expand Down
4 changes: 3 additions & 1 deletion src/labthings/apispec/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ def init_attribute_functions(self, *args, **kwargs):
OpenAPIConverter.init_attribute_functions(self, *args, **kwargs)
self.attribute_functions.append(self.jsonschema_type_mapping)

def jsonschema_type_mapping(self, field, **kwargs):
def jsonschema_type_mapping(self, field, **_):
"""
:param field:
:param **kwargs:
"""
ret = {}
if hasattr(field, "_jsonschema_type_mapping"):
# pylint: disable=protected-access
schema = field._jsonschema_type_mapping()
ret.update(schema)
return ret
Expand Down Expand Up @@ -246,6 +247,7 @@ def spec_for_event(cls, event):
)
return d

# pylint: disable=signature-differs
def operation_helper(self, path, operations, **kwargs):
"""Path helper that allows passing a Flask view function."""
# rule = self._rule_for_view(interaction.dispatch_request, app=app)
Expand Down
1 change: 0 additions & 1 deletion src/labthings/default_views/events.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .. import fields
from ..schema import LogRecordSchema
from ..views import EventView

Expand Down
26 changes: 18 additions & 8 deletions src/labthings/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from flask import url_for

from typing import List, Dict, Callable
from typing import List, Dict, Callable, Union

from .utilities import camel_to_snake, get_docstring, snake_to_spine
from .views.builder import static_from
Expand Down Expand Up @@ -36,11 +36,11 @@ def __init__(
self._meta: dict = {} # Extra metadata to add to the extension description

self._on_registers: List[
Dict
Union[Dict, Callable]
] = [] # List of dictionaries of functions to run on registration

self._on_components: List[
Dict
Union[Dict, Callable]
] = [] # List of dictionaries of functions to run as components are added

self._cls = str(self) # String description of extension instance
Expand All @@ -64,6 +64,14 @@ def views(self):
""" """
return self._views

@property
def on_components(self):
return self._on_components

@property
def on_registers(self):
return self._on_registers

def add_view(self, view_class, *urls, endpoint=None, **kwargs):
"""
Expand Down Expand Up @@ -217,7 +225,7 @@ def find_instances_in_module(module, class_to_find):
for attribute in dir(module):
if not attribute.startswith("__"):
if isinstance(getattr(module, attribute), class_to_find):
logging.debug(f"Found extension {getattr(module, attribute).name}")
logging.debug("Found extension %s", getattr(module, attribute).name)
objs.append(getattr(module, attribute))
return objs

Expand All @@ -235,17 +243,19 @@ def find_extensions_in_file(extension_path: str, module_name="extensions") -> li
:rtype: list
"""
logging.debug(f"Loading extensions from {extension_path}")
logging.debug("Loading extensions from %s", extension_path)

spec = util.spec_from_file_location(module_name, extension_path)
mod = util.module_from_spec(spec)
sys.modules[spec.name] = mod

try:
spec.loader.exec_module(mod) # type: ignore
except Exception: # skipcq: PYL-W0703
except Exception: # pylint: disable=broad-except
logging.error(
f"Exception in extension path {extension_path}: \n{traceback.format_exc()}"
"Exception in extension path %s: \n%s",
extension_path,
traceback.format_exc(),
)
return []
else:
Expand All @@ -270,7 +280,7 @@ def find_extensions(extension_dir: str, module_name="extensions") -> list:
:rtype: list
"""
logging.debug(f"Loading extensions from {extension_dir}")
logging.debug("Loading extensions from %s", extension_dir)

extensions = []
extension_paths = glob.glob(os.path.join(extension_dir, "*.py"))
Expand Down
2 changes: 1 addition & 1 deletion src/labthings/find.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def current_labthing(app=None):
# reach the Flask app object. Just using current_app returns
# a wrapper, which breaks it's use in Task threads
try:
app = current_app._get_current_object() # skipcq: PYL-W0212
app = current_app._get_current_object() # pylint: disable=protected-access
except RuntimeError:
return None
ext = app.extensions.get(EXTENSION_NAME, None)
Expand Down
7 changes: 1 addition & 6 deletions src/labthings/json/marshmallow_jsonschema/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _get_schema_for_field(self, obj, field):
schema = FIELD_VALIDATORS[base_class](schema, field, validator, obj)
return schema

def _from_nested_schema(self, obj, field):
def _from_nested_schema(self, _, field):
"""Support nested field.
:param obj:
Expand All @@ -217,17 +217,12 @@ def _from_nested_schema(self, obj, field):
nested = field.nested

if isclass(nested) and issubclass(nested, Schema):
name = nested.__name__
only = field.only
exclude = field.exclude
nested_cls = nested
nested_instance = nested(only=only, exclude=exclude)
else:
nested_cls = nested.__class__
name = nested_cls.__name__
nested_instance = nested

outer_name = obj.__class__.__name__
# If this is not a schema we've seen, and it's not this schema (checking this for recursive schemas),
# put it in our list of schema defs
wrapped_nested = self.__class__(nested=True)
Expand Down
4 changes: 1 addition & 3 deletions src/labthings/json/marshmallow_jsonschema/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
class UnsupportedValueError(Exception):
""" """

pass
""" """
5 changes: 3 additions & 2 deletions src/labthings/json/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def argument_to_param(
"""
param: Dict[str, Any] = {"in": "path", "name": argument, "required": True}
type_, format_ = CONVERTER_MAPPING.get(
# skipcq: PYL-W0212
# pylint: disable=protected-access
type(rule._converters[argument]), # type: ignore
DEFAULT_TYPE,
)
Expand All @@ -88,6 +88,7 @@ def field_to_property(field: fields.Field):
:param field:
"""
# pylint: disable=protected-access
return JSONSchema()._get_schema_for_field(Schema(), field)


Expand All @@ -103,7 +104,7 @@ def schema_to_json(
return None
if isinstance(schema, fields.Field):
return field_to_property(schema)
elif isinstance(schema, Dict):
elif isinstance(schema, dict):
return JSONSchema().dump(Schema.from_dict(schema)())
elif isinstance(schema, Schema):
return JSONSchema().dump(schema)
Expand Down
13 changes: 7 additions & 6 deletions src/labthings/labthing.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def dummy(*_):

for extension_object in self.extensions.values():
# For each on_component function
for com_func in extension_object._on_components:
for com_func in extension_object.on_components:
# If the component matches
if com_func.get("component", "") == component_name:
# Call the function
Expand Down Expand Up @@ -332,14 +332,14 @@ def dummy(*_):
)

# For each on_register function
for reg_func in extension_object._on_registers:
for reg_func in extension_object.on_registers:
# Call the function
reg_func.get("function", dummy)(
*reg_func.get("args"), **reg_func.get("kwargs")
)

# For each on_component function
for com_func in extension_object._on_components:
for com_func in extension_object.on_components:
key = com_func.get("component", "")
# If the component has already been added
if key in self.components:
Expand Down Expand Up @@ -393,7 +393,7 @@ def add_view(
"""
endpoint = endpoint or snake_to_camel(view.__name__)

logging.debug(f"{endpoint}: {type(view)} @ {urls}")
logging.debug("%s: %s @ %s", endpoint, type(view), urls)

if self.app is not None:
self._register_view(self.app, view, *urls, endpoint=endpoint, **kwargs)
Expand Down Expand Up @@ -449,8 +449,9 @@ def _register_view(
app.add_url_rule(rule, view_func=resource_func, endpoint=endpoint, **kwargs)

# There might be a better way to do this than _rules_by_endpoint,
# but I can't find one so this will do for now. Skipping PYL-W0212
flask_rules = app.url_map._rules_by_endpoint.get(endpoint) # skipcq: PYL-W0212
# but I can't find one so this will do for now.
# pylint: disable=protected-access
flask_rules = app.url_map._rules_by_endpoint.get(endpoint)
with app.test_request_context():
self.spec.path(view=resource_func, interaction=view)

Expand Down
2 changes: 1 addition & 1 deletion src/labthings/marshalling/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class use_body:
"""Gets the request body as a single value and adds it as a positional argument"""

def __init__(
self, schema: Union[Schema, Field, Dict[str, Union[Field, type]]], **kwargs
self, schema: Union[Schema, Field, Dict[str, Union[Field, type]]], **_
):
self.schema = schema

Expand Down
Loading

0 comments on commit 4c14e35

Please sign in to comment.