Skip to content

Commit

Permalink
Replaced pyupgrade, isort and flake8 with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
agronholm committed Sep 2, 2023
1 parent 7744a65 commit b02fbd7
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 60 deletions.
21 changes: 4 additions & 17 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,20 @@ repos:
args: [ "--fix=lf" ]
- id: trailing-whitespace

- repo: https://github.com/asottile/pyupgrade
rev: v3.3.2
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.0.286
hooks:
- id: pyupgrade
args: [ "--py37-plus" ]
- id: ruff
args: [--fix, --show-fixes]

- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black

- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort

- repo: https://github.com/csachs/pyproject-flake8
rev: v6.0.0.post1
hooks:
- id: pyproject-flake8

- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-eval
- id: rst-backticks
- id: rst-directive-colons
- id: rst-inline-touching-normal
35 changes: 19 additions & 16 deletions cbor2/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from .decoder import CBORDecoder, load, loads # noqa: F401
from .encoder import CBOREncoder, dump, dumps, shareable_encoder # noqa: F401
from .types import ( # noqa: F401
CBORDecodeEOF,
CBORDecodeError,
CBORDecodeValueError,
CBOREncodeError,
CBOREncodeTypeError,
CBOREncodeValueError,
CBORError,
CBORSimpleValue,
CBORTag,
undefined,
)
from .decoder import CBORDecoder as CBORDecoder
from .decoder import load as load
from .decoder import loads as loads
from .encoder import CBOREncoder as CBOREncoder
from .encoder import dump as dump
from .encoder import dumps as dumps
from .encoder import shareable_encoder as shareable_encoder
from .types import CBORDecodeEOF as CBORDecodeEOF
from .types import CBORDecodeError as CBORDecodeError
from .types import CBORDecodeValueError as CBORDecodeValueError
from .types import CBOREncodeError as CBOREncodeError
from .types import CBOREncodeTypeError as CBOREncodeTypeError
from .types import CBOREncodeValueError as CBOREncodeValueError
from .types import CBORError as CBORError
from .types import CBORSimpleValue as CBORSimpleValue
from .types import CBORTag as CBORTag
from .types import undefined as undefined

try:
from _cbor2 import * # noqa: F401,F403
from _cbor2 import * # noqa: F403
except ImportError:
# Couldn't import the optimized C version; ignore the failure and leave the
# pure Python implementations in place.
Expand All @@ -30,7 +33,7 @@ def _init_cbor2():
import _cbor2

from .encoder import canonical_encoders, default_encoders
from .types import CBORSimpleValue, CBORTag, undefined # noqa: F8
from .types import CBORSimpleValue, CBORTag, undefined # noqa: F811

_cbor2.default_encoders = OrderedDict(
[
Expand Down
8 changes: 4 additions & 4 deletions cbor2/decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ def str_errors(self, value):
self._str_errors = value
else:
raise ValueError(
"invalid str_errors value {!r} (must be one of 'strict', "
"'error', or 'replace')".format(value)
f"invalid str_errors value {value!r} (must be one of 'strict', "
"'error', or 'replace')"
)

def set_shareable(self, value):
Expand Down Expand Up @@ -164,8 +164,8 @@ def read(self, amount):
data = self._fp_read(amount)
if len(data) < amount:
raise CBORDecodeEOF(
"premature end of stream (expected to read {} bytes, got {} "
"instead)".format(amount, len(data))
f"premature end of stream (expected to read {amount} bytes, got {len(data)} "
"instead)"
)

return data
Expand Down
10 changes: 5 additions & 5 deletions cbor2/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ def _find_encoder(self, obj_type):
modname, typename = type_
except (TypeError, ValueError):
raise CBOREncodeValueError(
"invalid deferred encoder type {!r} (must be a "
f"invalid deferred encoder type {type_!r} (must be a "
"2-tuple of module name and type name, e.g. "
"('collections', 'defaultdict'))".format(type_)
"('collections', 'defaultdict'))"
)
imported_type = getattr(modules.get(modname), typename, None)
if imported_type is not None:
Expand Down Expand Up @@ -329,7 +329,7 @@ def encode_shared(self, encoder, value):
self.encode_int(index)
else:
raise CBOREncodeValueError(
"cyclic data structure detected but value sharing is " "disabled"
"cyclic data structure detected but value sharing is disabled"
)

def _stringref(self, value):
Expand Down Expand Up @@ -476,8 +476,8 @@ def encode_datetime(self, value):
value = value.replace(tzinfo=self._timezone)
else:
raise CBOREncodeValueError(
"naive datetime {!r} encountered and no default timezone "
"has been set".format(value)
f"naive datetime {value!r} encountered and no default timezone "
"has been set"
)

if self.datetime_as_timestamp:
Expand Down
2 changes: 1 addition & 1 deletion cbor2/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __le__(self, other):

@recursive_repr()
def __repr__(self):
return "CBORTag({self.tag}, {self.value!r})".format(self=self)
return f"CBORTag({self.tag}, {self.value!r})"

def __hash__(self):
return hash((self.tag, self.value))
Expand Down
18 changes: 10 additions & 8 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@ benchmarks = [
[tool.setuptools.packages.find]
include = ["cbor2"]

[tool.isort]
src_paths = ["src"]
skip_gitignore = true
profile = "black"

[tool.flake8]
max-line-length = 99
exclude = ".tox,build,docs"
[tool.ruff]
line-length = 99
select = [
"E", "F", "W", # default flake-8
"I", # isort
"ISC", # flake8-implicit-str-concat
"PGH", # pygrep-hooks
"RUF100", # unused noqa (yesqa)
"UP", # pyupgrade
]

[tool.pytest.ini_options]
addopts = "-rsx --tb=short"
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import platform
import struct

import pytest

import cbor2.decoder
import cbor2.encoder
import cbor2.types
import pytest

load_exc = ""
try:
Expand Down
3 changes: 2 additions & 1 deletion tests/hypothesis_strategies.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from collections import OrderedDict, defaultdict
from datetime import timedelta, timezone

from cbor2.types import FrozenDict
from hypothesis import strategies

from cbor2.types import FrozenDict

# Tune these for test run time
MAX_SIZE = 5
MAX_LEAVES = 2
Expand Down
13 changes: 8 additions & 5 deletions tests/test_decoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from uuid import UUID

import pytest

from cbor2.types import FrozenDict


Expand Down Expand Up @@ -40,7 +41,7 @@ def test_tag_hook_attr(impl):
decoder = impl.CBORDecoder(stream)

def tag_hook(decoder, tag):
return None # noqa: E731
return None

decoder.tag_hook = tag_hook
assert decoder.tag_hook is tag_hook
Expand All @@ -55,7 +56,7 @@ def test_object_hook_attr(impl):
decoder = impl.CBORDecoder(stream)

def object_hook(decoder, data):
return None # noqa: E731
return None

decoder.object_hook = object_hook
assert decoder.object_hook is object_hook
Expand Down Expand Up @@ -526,7 +527,9 @@ def test_ipaddress(impl, payload, expected):
def test_bad_ipaddress(impl):
with pytest.raises(impl.CBORDecodeError) as exc:
impl.loads(unhexlify("d9010443c00a0a"))
assert str(exc.value).endswith("invalid ipaddress value %r" % b"\xc0\x0a\x0a")
assert str(exc.value).endswith(
"invalid ipaddress value {!r}".format(b"\xc0\x0a\x0a")
)
assert isinstance(exc, ValueError)
with pytest.raises(impl.CBORDecodeError) as exc:
impl.loads(unhexlify("d9010401"))
Expand Down Expand Up @@ -719,14 +722,14 @@ def test_load_from_file(impl, tmpdir):

def test_nested_dict(impl):
value = impl.loads(unhexlify("A1D9177082010201"))
assert type(value) is dict
assert type(value) is dict # noqa: E721
assert value == {impl.CBORTag(6000, (1, 2)): 1}


def test_set(impl):
payload = unhexlify("d9010283616361626161")
value = impl.loads(payload)
assert type(value) is set
assert type(value) is set # noqa: E721
assert value == {"a", "b", "c"}


Expand Down
3 changes: 2 additions & 1 deletion tests/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
from uuid import UUID

import pytest
from hypothesis import given

from cbor2 import shareable_encoder
from cbor2.types import FrozenDict
from hypothesis import given

from .hypothesis_strategies import compound_types_strategy

Expand Down
3 changes: 2 additions & 1 deletion tests/test_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import json
from io import BytesIO, TextIOWrapper

import cbor2.tool
import pytest

import cbor2.tool


@pytest.mark.parametrize(
"value, expected",
Expand Down
1 change: 1 addition & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest

from cbor2.types import FrozenDict


Expand Down

0 comments on commit b02fbd7

Please sign in to comment.