Skip to content

Commit

Permalink
chore(pylint): handle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas03 committed Aug 26, 2022
1 parent bd78f67 commit cf59148
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 28 deletions.
5 changes: 0 additions & 5 deletions .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@ disable =
wrong-import-order,
unused-argument,
broad-except,
relative-import,
bad-continuation,
wrong-import-position,
bare-except,
unused-variable,
unsubscriptable-object,
redefined-builtin,
no-self-argument,
no-self-use,
too-many-arguments,
too-many-locals,
protected-access,
Expand All @@ -34,11 +31,9 @@ disable =
bad-classmethod-argument,
too-few-public-methods,
arguments-differ,
redefined-variable-type,
too-many-instance-attributes,
locally-disabled,
too-many-return-statements,
no-init,
too-many-nested-blocks,
abstract-method,
too-many-public-methods,
Expand Down
4 changes: 3 additions & 1 deletion kw/json/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ def wrapper(*args, **kwargs):
str(err)
== "__init__() got an unexpected keyword argument 'use_decimal'"
):
raise KiwiJsonError(__use_decimal_error_message)
raise KiwiJsonError(
__use_decimal_error_message
) # pylint: disable=W0707
raise
return result

Expand Down
16 changes: 7 additions & 9 deletions kw/json/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@


def _fail(obj, *args, **kwargs):
raise TypeError(
"Object of type {} is not JSON serializable".format(obj.__class__.__name__)
)
raise TypeError(f"Object of type {obj.__class__.__name__} is not JSON serializable")


try:
Expand Down Expand Up @@ -54,7 +52,7 @@ def default_encoder(obj, dict_factory=dict, date_as_unix_time=False):
if hasattr(obj, "__module__") and "sqlalchemy" in obj.__module__:
if obj.__class__.__name__ == "RowProxy":
return dict_factory(obj.items())
elif obj.__class__.__name__ == "Row":
if obj.__class__.__name__ == "Row":
return dict_factory(obj)

if obj.__class__.__name__ == "Record": # asyncpg
Expand Down Expand Up @@ -83,18 +81,18 @@ def raw_encoder(obj, date_as_unix_time=False):


class MaskedJSONEncoder(BaseJSONEncoder):
def default(self, obj): # pylint: disable=method-hidden
return default_encoder(obj, mask_dict)
def default(self, o): # pylint: disable=method-hidden
return default_encoder(o, mask_dict)

def encode(self, o):
if isinstance(o, dict):
o = mask_dict(o)
return super(MaskedJSONEncoder, self).encode(o)
return super().encode(o)


class KiwiJSONEncoder(BaseJSONEncoder):
def default(self, obj): # pylint: disable=method-hidden
return default_encoder(obj)
def default(self, o): # pylint: disable=method-hidden
return default_encoder(o)


def modify_kwargs(kwargs):
Expand Down
6 changes: 2 additions & 4 deletions kw/json/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def init_app(self, app, encoder=default_encoder, dict_factory=dict):
from flask import json # pylint: disable=import-outside-toplevel

class JSONEncoder(json.JSONEncoder):
# seems pylint bug, original message
# E0202: An attribute defined in json.encoder line 158 hides this method (method-hidden)
def default(self, obj): # pylint: disable=E0202
return encoder(obj, dict_factory)
def default(self, o):
return encoder(o, dict_factory)

app.json_encoder = JSONEncoder
12 changes: 5 additions & 7 deletions test/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
from json import loads

import arrow
import asyncio
import asyncpg
import attr
import pytest
from pytz import UTC
Expand Down Expand Up @@ -61,7 +63,7 @@ class AttrsItem(object):


class NotDataclassesItem(object):
__dataclass_fields__ = dict()
__dataclass_fields__ = {}


class NotAttrsItem(object):
Expand Down Expand Up @@ -133,7 +135,7 @@ def test_default_encoder_defaults():
(
({1}, "[1]", False),
(Decimal("1"), '"1"', False),
(UUID, '"{}"'.format(str(UUID)), False),
(UUID, f'"{str(UUID)}"', False),
(datetime.datetime(2018, 1, 1), '"2018-01-01T00:00:00"', False),
(
datetime.datetime(2018, 1, 1, tzinfo=UTC),
Expand Down Expand Up @@ -264,7 +266,7 @@ def test_dump_with_default_and_date_as_unix_time():
)
def test_dump(tmpdir, value, expected):
filename = str(tmpdir.join("test_file.json"))
with open(filename, "w+") as fp:
with open(filename, "w+", encoding="UTF-8") as fp:
dump(value, fp)
fp.seek(0, 0)
assert json_load(fp) == expected
Expand Down Expand Up @@ -391,17 +393,13 @@ def test_no_attrs():


async def get_asyncpg_record(dsn):
import asyncpg

connection = await asyncpg.connect(dsn)
result = await connection.fetch("SELECT 1 as value")
await connection.close()
return result


def test_asyncpg():
import asyncio # pylint: disable=import-outside-toplevel

loop = asyncio.get_event_loop()
result = loop.run_until_complete(get_asyncpg_record(os.getenv("DATABASE_URI")))
assert json_dumps(result, default=default_encoder) == '[{"value": 1}]'
4 changes: 2 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ commands = pytest test {posargs:}
[testenv:pylint]
basepython = python3.8
deps =
pylint
pylint==2.14.5
-rtest-requirements.txt
skip_install = True
commands = pylint {posargs:} kw.json test
commands = pylint {posargs:} kw/json test

[testenv:black]
basepython = python3.8
Expand Down

0 comments on commit cf59148

Please sign in to comment.