From aa240d38e836d105f12e8a94cd4b40ff42e5118b Mon Sep 17 00:00:00 2001 From: Guillaume Ayoub Date: Sat, 27 Apr 2024 14:32:29 +0200 Subject: [PATCH] Use ruff instead of flake8 and isort --- .github/workflows/tests.yml | 4 +--- cairocffi/__init__.py | 4 ++-- cairocffi/matrix.py | 4 ++-- cairocffi/surfaces.py | 4 ++-- cairocffi/test_cairo.py | 9 +++++---- cairocffi/test_numpy.py | 3 ++- cairocffi/test_xcb.py | 2 +- pyproject.toml | 8 ++++---- utils/cairo_coverage.py | 5 +++-- utils/cairocffi_to_pycairo.py | 3 ++- utils/compare_pycairo.py | 1 + utils/mkconstants.py | 4 ++-- utils/pango_example.py | 3 ++- utils/pycairo_to_cairocffi.py | 3 ++- utils/tests.py | 5 +++-- 15 files changed, 34 insertions(+), 28 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 185b789..4971432 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -40,6 +40,4 @@ jobs: env: DYLD_FALLBACK_LIBRARY_PATH: /opt/homebrew/lib - name: Check coding style - run: python -m flake8 - - name: Check imports order - run: python -m isort . --check --diff + run: python -m ruff check diff --git a/cairocffi/__init__.py b/cairocffi/__init__.py index 5bbc544..70350f6 100644 --- a/cairocffi/__init__.py +++ b/cairocffi/__init__.py @@ -41,7 +41,7 @@ def dlopen(ffi, library_names, filenames): for library_name in library_names: library_filename = find_library(library_name) if library_filename: - filenames = (library_filename,) + filenames + filenames = (library_filename, *filenames) else: exceptions.append( 'no library called "{}" was found'.format(library_name)) @@ -62,7 +62,7 @@ def dlopen(ffi, library_names, filenames): ('libcairo.so.2', 'libcairo.2.dylib', 'libcairo-2.dll')) -class _keepref(object): +class _keepref(object): # noqa: N801 """Function wrapper that keeps a reference to another object.""" def __init__(self, ref, func): self.ref = ref diff --git a/cairocffi/matrix.py b/cairocffi/matrix.py index 70488e2..4772a10 100644 --- a/cairocffi/matrix.py +++ b/cairocffi/matrix.py @@ -88,7 +88,7 @@ def __ne__(self, other): def __repr__(self): class_ = type(self) return '%s(%g, %g, %g, %g, %g, %g)' % ( - (class_.__name__,) + self.as_tuple()) + (class_.__name__, *self.as_tuple())) def multiply(self, other): """Multiply with another matrix @@ -235,7 +235,7 @@ def transform_distance(self, dx, dy): cairo.cairo_matrix_transform_distance(self._pointer, xy + 0, xy + 1) return tuple(xy) - def _component_property(name): + def _component_property(name): # noqa: N805 return property( lambda self: getattr(self._pointer, name), lambda self, value: setattr(self._pointer, name, value), diff --git a/cairocffi/surfaces.py b/cairocffi/surfaces.py index cb542f1..4951967 100644 --- a/cairocffi/surfaces.py +++ b/cairocffi/surfaces.py @@ -103,7 +103,7 @@ class KeepAlive(object): or none of them must be called. """ - instances = set() + instances = set() # noqa: RUF012 def __init__(self, *objects): self.objects = objects @@ -655,7 +655,7 @@ def write_to_png(self, target=None): write_func = _make_write_func(target) _check_status(cairo.cairo_surface_write_to_png_stream( self._pointer, write_func, ffi.NULL)) - except (SystemError, MemoryError): # noqa + except (SystemError, MemoryError): # pragma: no cover # Callback creation has failed if hasattr(target, 'name'): # File-like object has a name, write here diff --git a/cairocffi/test_cairo.py b/cairocffi/test_cairo.py index 5fe0599..cc7e871 100644 --- a/cairocffi/test_cairo.py +++ b/cairocffi/test_cairo.py @@ -20,11 +20,12 @@ import sys import tempfile -import cairocffi import pikepdf import pytest -from . import ( +import cairocffi + +from . import ( # isort:skip PDF_METADATA_AUTHOR, PDF_METADATA_CREATE_DATE, PDF_METADATA_CREATOR, PDF_METADATA_KEYWORDS, PDF_METADATA_MOD_DATE, PDF_METADATA_SUBJECT, PDF_METADATA_TITLE, PDF_OUTLINE_FLAG_BOLD, PDF_OUTLINE_FLAG_OPEN, @@ -1033,7 +1034,7 @@ def test_context_mask(): context.mask(SurfacePattern(mask_surface)) o = pixel(b'\x00\x00\x00\x00') b = pixel(b'\x80\x00\x00\x00') - B = pixel(b'\xFF\x00\x00\x00') + B = pixel(b'\xFF\x00\x00\x00') # noqa: N806 assert surface.get_data()[:] == ( B + o + o + o + o + b + o + o + @@ -1046,7 +1047,7 @@ def test_context_mask(): context.mask_surface(mask_surface, surface_x=1, surface_y=2) o = pixel(b'\x00\x00\x00\x00') b = pixel(b'\x80\x00\x00\x00') - B = pixel(b'\xFF\x00\x00\x00') + B = pixel(b'\xFF\x00\x00\x00') # noqa: N806 assert surface.get_data()[:] == ( o + o + o + o + o + o + o + o + diff --git a/cairocffi/test_numpy.py b/cairocffi/test_numpy.py index 7efb674..1b92c7e 100644 --- a/cairocffi/test_numpy.py +++ b/cairocffi/test_numpy.py @@ -1,8 +1,9 @@ import math -import cairocffi as cairo import numpy +import cairocffi as cairo + def test_numpy(): data = numpy.zeros((200, 200, 4), dtype=numpy.uint8) diff --git a/cairocffi/test_xcb.py b/cairocffi/test_xcb.py index b83c569..1241ac0 100644 --- a/cairocffi/test_xcb.py +++ b/cairocffi/test_xcb.py @@ -14,7 +14,7 @@ import pytest -xcffib = pytest.importorskip('xcffib') # noqa +xcffib = pytest.importorskip('xcffib') import xcffib.xproto # noqa isort:skip from xcffib.xproto import ConfigWindow, CW, EventMask, GC # noqa isort:skip diff --git a/pyproject.toml b/pyproject.toml index 278ab0e..48e88bd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -40,7 +40,7 @@ Donation = 'https://opencollective.com/courtbouillon' [project.optional-dependencies] doc = ['sphinx', 'sphinx_rtd_theme'] -test = ['pytest', 'flake8', 'isort', 'numpy', 'pikepdf'] +test = ['pytest', 'ruff', 'numpy', 'pikepdf'] xcb = ['xcffib >= 1.4.0'] [tool.flit.sdist] @@ -54,6 +54,6 @@ include = ['cairocffi/*'] exclude_lines = ['pragma: no cover', 'def __repr__', 'raise NotImplementedError'] omit = ['.*'] -[tool.isort] -default_section = 'FIRSTPARTY' -multi_line_output = 4 +[tool.ruff.lint] +select = ['E', 'W', 'F', 'I', 'N', 'RUF'] +ignore = ['RUF001', 'RUF002', 'RUF003'] diff --git a/utils/cairo_coverage.py b/utils/cairo_coverage.py index 591572f..964e3cd 100644 --- a/utils/cairo_coverage.py +++ b/utils/cairo_coverage.py @@ -1,8 +1,9 @@ import inspect -import cairocffi import pycparser +import cairocffi + ALL_THE_CODE = ''.join( line for module in [ @@ -12,7 +13,7 @@ class Visitor(pycparser.c_ast.NodeVisitor): - def visit_Decl(self, node): + def visit_Decl(self, node): # noqa: N802 for _, child in node.children(): if isinstance(child, pycparser.c_ast.FuncDecl): if ('cairo.' + node.name) not in ALL_THE_CODE and not ( diff --git a/utils/cairocffi_to_pycairo.py b/utils/cairocffi_to_pycairo.py index a64fba5..61953cf 100644 --- a/utils/cairocffi_to_pycairo.py +++ b/utils/cairocffi_to_pycairo.py @@ -1,6 +1,7 @@ import ctypes import cairo # pycairo + import cairocffi pycairo = ctypes.PyDLL(cairo._cairo.__file__) @@ -9,7 +10,7 @@ ctypes.pythonapi.PyList_Append.argtypes = 2 * [ctypes.c_void_p] -def _UNSAFE_cairocffi_context_to_pycairo(cairocffi_context): +def _UNSAFE_cairocffi_context_to_pycairo(cairocffi_context): # noqa: N802 # Sanity check. Continuing with another type would probably segfault. if not isinstance(cairocffi_context, cairocffi.Context): raise TypeError('Expected a cairocffi.Context, got %r' diff --git a/utils/compare_pycairo.py b/utils/compare_pycairo.py index 2b98167..c42213b 100644 --- a/utils/compare_pycairo.py +++ b/utils/compare_pycairo.py @@ -1,4 +1,5 @@ import cairo as pycairo + import cairocffi # We want the real pycairo diff --git a/utils/mkconstants.py b/utils/mkconstants.py index e584214..8f685ad 100644 --- a/utils/mkconstants.py +++ b/utils/mkconstants.py @@ -16,7 +16,7 @@ def parse_constant(node): class PrintEnumsVisitor(pycparser.c_ast.NodeVisitor): - def visit_Decl(self, node): + def visit_Decl(self, node): # noqa: N802 if node.name and node.name.startswith('CAIRO_'): # len('CAIRO_') == 6 if node.init.type == 'string': print('%s = b%s' % (node.name[6:], node.init.value)) @@ -24,7 +24,7 @@ def visit_Decl(self, node): print('%s = %s' % (node.name[6:], node.init.value)) print('') - def visit_Enum(self, node): + def visit_Enum(self, node): # noqa: N802 value = 0 for enumerator in node.values.enumerators: if enumerator.value is not None: diff --git a/utils/pango_example.py b/utils/pango_example.py index ce198bd..323e6bf 100644 --- a/utils/pango_example.py +++ b/utils/pango_example.py @@ -1,6 +1,7 @@ -import cairocffi import cffi +import cairocffi + ffi = cffi.FFI() ffi.cdef(''' /* GLib */ diff --git a/utils/pycairo_to_cairocffi.py b/utils/pycairo_to_cairocffi.py index 1447022..5bafa4b 100644 --- a/utils/pycairo_to_cairocffi.py +++ b/utils/pycairo_to_cairocffi.py @@ -1,8 +1,9 @@ import cairo # pycairo + import cairocffi -def _UNSAFE_pycairo_context_to_cairocffi(pycairo_context): +def _UNSAFE_pycairo_context_to_cairocffi(pycairo_context): # noqa: N802 # Sanity check. Continuing with another type would probably segfault. if not isinstance(pycairo_context, cairo.Context): raise TypeError('Expected a cairo.Context, got %r' % pycairo_context) diff --git a/utils/tests.py b/utils/tests.py index bf907ab..009d8e9 100644 --- a/utils/tests.py +++ b/utils/tests.py @@ -1,11 +1,12 @@ import io -import cairo # noqa, pycairo -import cairocffi +import cairo # noqa: F401 import pango_example from cairocffi_to_pycairo import _UNSAFE_cairocffi_context_to_pycairo from pycairo_to_cairocffi import _UNSAFE_pycairo_context_to_cairocffi +import cairocffi + def test(): cairocffi_context = cairocffi.Context(cairocffi.PDFSurface(None, 10, 20))