Skip to content

Commit

Permalink
Introduce pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
jasujm committed May 14, 2020
1 parent 0b23ca7 commit e81be76
Show file tree
Hide file tree
Showing 12 changed files with 649 additions and 28 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
Unreleased
----------

Added
- Pylint (test target, configurations)

Fixed
- Several pylint errors

Version 0.4
-----------

Expand Down
6 changes: 5 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ if(Python_FOUND)
add_test(NAME EnumECGTest
COMMAND Python::Interpreter -m tox
WORKING_DIRECTORY ${PYTHON_SOURCE_DIR})
endif()

add_test(NAME EnumECGLint
COMMAND Python::Interpreter -m pylint enumecg
WORKING_DIRECTORY ${PYTHON_SOURCE_DIR})
endif()
else()
message(STATUS "Python not found. Skipping building python packages.")
endif()
1 change: 1 addition & 0 deletions python/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include LICENSE
include tox.ini
include pylintrc
8 changes: 4 additions & 4 deletions python/enumecg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
from . import definitions, generators, exceptions


def _convert_to_enumerator(Enum, value, parameter):
def _convert_to_enumerator(enum_type, value, parameter):
if value is not None:
try:
value = Enum(value)
except ValueError as e:
raise exceptions.Error(f"Invalid value for {parameter}: {value!r}") from e
value = enum_type(value)
except ValueError as ex:
raise exceptions.Error(f"Invalid value for {parameter}: {value!r}") from ex
return value


Expand Down
6 changes: 6 additions & 0 deletions python/enumecg/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""
Entry point to the EnumECG CLI
..............................
"""

from .cli import cli

# pylint: disable=no-value-for-parameter
cli()
10 changes: 4 additions & 6 deletions python/enumecg/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
:func:`cli()`.
"""

import sys
import traceback

import click
Expand All @@ -15,11 +14,10 @@
from . import generate
from .generators import DocumentationStyle
from .definitions import PrimaryType
from .exceptions import Error


def _get_enum_values(Enum):
return [e.value for e in Enum.__members__.values()]
def _get_enum_values(enum_type):
return [e.value for e in enum_type.__members__.values()]


def _report_error_and_fail(message):
Expand Down Expand Up @@ -60,7 +58,7 @@ def cli(file, documentation, primary_type, value_type):
"""
try:
enum = yaml.safe_load(file)
except Exception:
except Exception: # pylint: disable=broad-except
_report_error_and_fail(f"Failed to load {file.name}")

try:
Expand All @@ -70,7 +68,7 @@ def cli(file, documentation, primary_type, value_type):
primary_type=primary_type,
value_type=value_type,
)
except Exception:
except Exception: # pylint: disable=broad-except
_report_error_and_fail(f"Failed to generate code from {file.name}")

click.echo(output)
11 changes: 5 additions & 6 deletions python/enumecg/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,10 @@ def make_definition(
"""
if isinstance(enum, EnumDefinition):
return enum
elif isinstance(enum, cabc.Mapping):
pass
elif isinstance(enum, py_enum.EnumMeta):

if isinstance(enum, py_enum.EnumMeta):
enum = _extract_python_enum_attrs(enum)
else:
elif not isinstance(enum, cabc.Mapping):
raise exceptions.Error(
f"Could not convert {enum!r} of type {type(enum)} into EnumDefinition"
)
Expand All @@ -173,7 +172,7 @@ def make_definition(
return _make_definition_from_dict(
enum, primary_type=primary_type, value_type=value_type
)
except (KeyError, AttributeError, TypeError, ValueError) as e:
except (KeyError, AttributeError, TypeError, ValueError) as ex:
raise exceptions.Error(
f"Failed to convert {enum!r} into an enum definition"
) from e
) from ex
7 changes: 4 additions & 3 deletions python/enumecg/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
"""

import collections.abc as cabc
import enum
import enum as py_enum
import typing

import jinja2

from . import definitions, exceptions


class DocumentationStyle(enum.Enum):
class DocumentationStyle(py_enum.Enum):
"""Possible documentation styles
These are the accepted choices for the ``documentation`` argument
Expand All @@ -29,7 +29,7 @@ class DocumentationStyle(enum.Enum):
def _make_initializer_list(value):
if isinstance(value, str):
return value
elif isinstance(value, cabc.Iterable):
if isinstance(value, cabc.Iterable):
value_initializer_list = ", ".join(_make_initializer_list(v) for v in value)
return f"{{ {value_initializer_list} }}"
raise exceptions.Error("Argument not str or an iterable: {value!r}")
Expand All @@ -52,6 +52,7 @@ def _create_jinja_env():
return env


# pylint: disable=too-few-public-methods
class CodeGenerator:
"""Code generator for an enhanced enum type
Expand Down
10 changes: 5 additions & 5 deletions python/enumecg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def _join_lower_camel_case(parts):
return first + "".join(_capitalize_word(part) for part in rest)


def _all_are_instances(values, type):
return values and all(isinstance(v, type) for v in values)
def _all_are_instances(values, type_):
return values and all(isinstance(v, type_) for v in values)


class NameFormatter:
Expand Down Expand Up @@ -190,11 +190,11 @@ def get_initializer(cls, value) -> str:
if isinstance(value, bytes):
value = value.decode()
return f'"{repr(value)[1:-1]}"'
elif isinstance(value, bool):
if isinstance(value, bool):
return "true" if value else "false"
elif isinstance(value, numbers.Real):
if isinstance(value, numbers.Real):
return repr(value)
elif isinstance(value, cabc.Sequence):
if isinstance(value, cabc.Sequence):
return [cls.get_initializer(v) for v in value]
raise exceptions.Error(f"Could not generate initializer for {value!r}")

Expand Down

0 comments on commit e81be76

Please sign in to comment.