Skip to content

Commit

Permalink
Merge pull request #43 from jelmer/pyupgrade
Browse files Browse the repository at this point in the history
Enable ruff, pyupgrade
  • Loading branch information
jelmer authored Jun 3, 2023
2 parents 46788d7 + 7b56151 commit 822467a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 25 deletions.
40 changes: 23 additions & 17 deletions patiencediff/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
import time
from typing import Type

__all__ = ['PatienceSequenceMatcher', 'unified_diff', 'unified_diff_files']
__all__ = [
'PatienceSequenceMatcher',
'unified_diff',
'unified_diff_files',
'recurse_matches',
'unique_lcs',
]

__version__ = (0, 2, 13)

Expand All @@ -31,8 +37,7 @@
def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
tofiledate='', n=3, lineterm='\n',
sequencematcher=None):
r"""
Compare two sequences of lines; generate the delta as a unified diff.
r"""Compare two sequences of lines; generate the delta as a unified diff.
Unified diffs are a compact way of showing line changes and a few
lines of context. The number of context lines is set by 'n' which
Expand All @@ -53,7 +58,6 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
times are normally expressed in the format returned by time.ctime().
Example:
>>> for line in unified_diff('one two three four'.split(),
... 'zero one tree four'.split(), 'Original', 'Current',
... 'Sat Jan 26 23:30:50 1991', 'Fri Jun 06 10:20:52 2003',
Expand All @@ -80,8 +84,8 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',
started = False
for group in sequencematcher(None, a, b).get_grouped_opcodes(n):
if not started:
yield '--- %s%s%s' % (fromfile, fromfiledate, lineterm)
yield '+++ %s%s%s' % (tofile, tofiledate, lineterm)
yield f'--- {fromfile}{fromfiledate}{lineterm}'
yield f'+++ {tofile}{tofiledate}{lineterm}'
started = True
i1, i2, j1, j2 = group[0][1], group[-1][2], group[0][3], group[-1][4]
yield "@@ -%d,%d +%d,%d @@%s" % (i1+1, i2-i1, j1+1, j2-j1, lineterm)
Expand All @@ -99,24 +103,23 @@ def unified_diff(a, b, fromfile='', tofile='', fromfiledate='',


def unified_diff_files(a, b, sequencematcher=None):
"""Generate the diff for two files.
"""
"""Generate the diff for two files."""
# Should this actually be an error?
if a == b:
return []
if a == '-':
lines_a = sys.stdin.readlines()
time_a = time.time()
else:
with open(a, 'r') as f:
with open(a) as f:
lines_a = f.readlines()
time_a = os.stat(a).st_mtime # noqa: F841

if b == '-':
lines_b = sys.stdin.readlines()
time_b = time.time()
else:
with open(b, 'r') as f:
with open(b) as f:
lines_b = f.readlines()
time_b = os.stat(b).st_mtime # noqa: F841

Expand All @@ -130,13 +133,16 @@ def unified_diff_files(a, b, sequencematcher=None):


try:
from ._patiencediff_c import \
PatienceSequenceMatcher_c as PatienceSequenceMatcher
from ._patiencediff_c import (
PatienceSequenceMatcher_c as PatienceSequenceMatcher,
)
from ._patiencediff_c import recurse_matches_c as recurse_matches
from ._patiencediff_c import unique_lcs_c as unique_lcs
except ImportError:
from ._patiencediff_py import \
PatienceSequenceMatcher_py as PatienceSequenceMatcher # noqa: F401
from ._patiencediff_py import \
recurse_matches_py as recurse_matches # noqa: F401
from ._patiencediff_py import unique_lcs_py as unique_lcs # noqa: F401
from ._patiencediff_py import (
PatienceSequenceMatcher_py as PatienceSequenceMatcher,
)
from ._patiencediff_py import (
recurse_matches_py as recurse_matches,
)
from ._patiencediff_py import unique_lcs_py as unique_lcs
8 changes: 4 additions & 4 deletions patiencediff/_patiencediff_c.pyi
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import difflib
from typing import Any, List, Sequence, Tuple
from typing import Any, Sequence

class PatienceSequenceMatcher_c(difflib.SequenceMatcher):

def get_matching_blocks(self) -> List[difflib.Match]: ...
def get_matching_blocks(self) -> list[difflib.Match]: ...


def unique_lcs_c(a: Sequence[Any], b: Sequence[Any]) -> List[Tuple[int, int]]: ...
def unique_lcs_c(a: Sequence[Any], b: Sequence[Any]) -> list[tuple[int, int]]: ...


def recurse_matches_c(
a: Sequence[Any], b: Sequence[Any],
alo: int, blo: int, ahi: int, bhi: int,
answer: List[Tuple[int, int]], maxrecursion: int) -> None: ...
answer: list[tuple[int, int]], maxrecursion: int) -> None: ...

6 changes: 3 additions & 3 deletions patiencediff/_patiencediff_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

class MaxRecursionDepth(Exception):

def __init__(self):
super(MaxRecursionDepth, self).__init__('max recursion depth reached')
def __init__(self) -> None:
super().__init__('max recursion depth reached')


def unique_lcs_py(a: Sequence[Any], b: Sequence[Any]) -> List[Tuple[int, int]]:
Expand Down Expand Up @@ -213,7 +213,7 @@ class PatienceSequenceMatcher_py(difflib.SequenceMatcher):

_do_check_consistency = True

def __init__(self, isjunk=None, a='', b=''):
def __init__(self, isjunk=None, a='', b='') -> None:
if isjunk is not None:
raise NotImplementedError('Currently we do not support'
' isjunk for sequence matching')
Expand Down
27 changes: 27 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,30 @@ patiencediff = ["py.typed"]

[tool.setuptools.dynamic]
version = {attr = "patiencediff.__version__"}

[tool.ruff]
select = [
"ANN",
"D",
"E",
"F",
"I",
"UP",
]
ignore = [
"ANN001",
"ANN101",
"ANN201",
"ANN202",
"D100",
"D101",
"D102",
"D103",
"D104",
"E501",
]
target-version = "py37"
line-length = 79

[tool.ruff.pydocstyle]
convention = "google"
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# encoding: utf-8

import os

Expand Down

0 comments on commit 822467a

Please sign in to comment.