Skip to content

Commit

Permalink
Merge pull request #225 from Kozea/ruff
Browse files Browse the repository at this point in the history
Use ruff instead of flake8 and isort
  • Loading branch information
liZe authored Apr 27, 2024
2 parents 33501f0 + aa240d3 commit a535d50
Show file tree
Hide file tree
Showing 15 changed files with 34 additions and 28 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions cairocffi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cairocffi/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions cairocffi/surfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 5 additions & 4 deletions cairocffi/test_cairo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 +
Expand All @@ -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 +
Expand Down
3 changes: 2 additions & 1 deletion cairocffi/test_numpy.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cairocffi/test_xcb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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']
5 changes: 3 additions & 2 deletions utils/cairo_coverage.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import inspect

import cairocffi
import pycparser

import cairocffi

ALL_THE_CODE = ''.join(
line
for module in [
Expand All @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion utils/cairocffi_to_pycairo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ctypes

import cairo # pycairo

import cairocffi

pycairo = ctypes.PyDLL(cairo._cairo.__file__)
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions utils/compare_pycairo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import cairo as pycairo

import cairocffi

# We want the real pycairo
Expand Down
4 changes: 2 additions & 2 deletions utils/mkconstants.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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))
else:
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:
Expand Down
3 changes: 2 additions & 1 deletion utils/pango_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cairocffi
import cffi

import cairocffi

ffi = cffi.FFI()
ffi.cdef('''
/* GLib */
Expand Down
3 changes: 2 additions & 1 deletion utils/pycairo_to_cairocffi.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
5 changes: 3 additions & 2 deletions utils/tests.py
Original file line number Diff line number Diff line change
@@ -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))
Expand Down

0 comments on commit a535d50

Please sign in to comment.