Skip to content

Commit fb0383c

Browse files
authored
MAINT Update to ruff for linting (#69)
1 parent 8da3a87 commit fb0383c

File tree

6 files changed

+42
-71
lines changed

6 files changed

+42
-71
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,20 @@
11
repos:
22
- repo: https://github.com/pre-commit/pre-commit-hooks
3-
rev: v4.4.0
3+
rev: v4.6.0
44
hooks:
55
- id: check-yaml
66
- id: end-of-file-fixer
77
- id: trailing-whitespace
8-
9-
10-
- repo: https://github.com/psf/black
11-
rev: 23.3.0
12-
hooks:
13-
- id: black
14-
15-
- repo: https://github.com/asottile/pyupgrade
16-
rev: v3.3.1
17-
hooks:
18-
- id: pyupgrade
19-
args: ["--py38-plus"]
20-
21-
- repo: https://github.com/hadialqattan/pycln
22-
rev: "v2.1.3"
8+
- repo: https://github.com/charliermarsh/ruff-pre-commit
9+
rev: "v0.3.7"
2310
hooks:
24-
- id: pycln
25-
args: [--config=pyproject.toml]
26-
stages: [manual]
11+
- id: ruff
12+
args: [--fix]
13+
- id: ruff-format
2714

2815

2916
- repo: https://github.com/pre-commit/mirrors-mypy
30-
rev: v1.1.1
17+
rev: v1.9.0
3118
hooks:
3219
- id: mypy
3320
files: ".+.py"

doc/sphinxext/github_link.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
4242
return
4343

4444
class_name = info["fullname"].split(".")[0]
45-
if type(class_name) != str:
45+
if isinstance(class_name, str):
4646
# Python 2 only
4747
class_name = class_name.encode("utf-8")
4848
module = __import__(info["module"], fromlist=[class_name])
@@ -60,16 +60,12 @@ def _linkcode_resolve(domain, info, package, url_fmt, revision):
6060
if not fn:
6161
return
6262

63-
fn = os.path.relpath(
64-
fn, start=os.path.dirname(__import__(package).__file__)
65-
)
63+
fn = os.path.relpath(fn, start=os.path.dirname(__import__(package).__file__))
6664
try:
6765
lineno = inspect.getsourcelines(obj)[1]
6866
except Exception:
6967
lineno = ""
70-
return url_fmt.format(
71-
revision=revision, package=package, path=fn, lineno=lineno
72-
)
68+
return url_fmt.format(revision=revision, package=package, path=fn, lineno=lineno)
7369

7470

7571
def make_linkcode_resolve(package, url_fmt):

pgeocode.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import urllib.request
88
import warnings
99
from io import BytesIO
10-
from typing import Any, Tuple, List, Optional
10+
from typing import Any, List, Optional, Tuple
1111
from zipfile import ZipFile
1212

1313
import numpy as np
@@ -226,11 +226,9 @@ def __init__(self, country: str = "fr", unique: bool = True):
226226
country = country.upper()
227227
if country not in COUNTRIES_VALID:
228228
raise ValueError(
229-
(
230-
"country={} is not a known country code. "
231-
"See the README for a list of supported "
232-
"countries"
233-
).format(country)
229+
f"country={country} is not a known country code. "
230+
"See the README for a list of supported "
231+
"countries"
234232
)
235233
if country == "AR":
236234
warnings.warn(
@@ -259,9 +257,7 @@ def _get_data(country: str) -> Tuple[str, pd.DataFrame]:
259257
keep_default_na=False,
260258
)
261259
else:
262-
download_urls = [
263-
val.format(country=country) for val in DOWNLOAD_URL
264-
]
260+
download_urls = [val.format(country=country) for val in DOWNLOAD_URL]
265261
with _open_extract_cycle_url(download_urls, country) as fh:
266262
data = pd.read_csv(
267263
fh,
@@ -344,9 +340,7 @@ def query_postal_code(self, codes):
344340
codes = pd.DataFrame(codes, columns=["postal_code"])
345341

346342
codes = self._normalize_postal_code(codes)
347-
response = pd.merge(
348-
codes, self._data_frame, on="postal_code", how="left"
349-
)
343+
response = pd.merge(codes, self._data_frame, on="postal_code", how="left")
350344
if self.unique and single_entry:
351345
response = response.iloc[0]
352346
return response
@@ -387,9 +381,7 @@ def query_location(
387381
return contains_matches.iloc[:top_k]
388382

389383
if fuzzy_threshold is not None:
390-
fuzzy_matches = self._fuzzy_search(
391-
name, col, threshold=fuzzy_threshold
392-
)
384+
fuzzy_matches = self._fuzzy_search(name, col, threshold=fuzzy_threshold)
393385
if len(fuzzy_matches) > 0:
394386
return fuzzy_matches.iloc[:top_k]
395387

@@ -400,9 +392,7 @@ def _str_contains_search(self, text: str, col: str) -> pd.DataFrame:
400392
match_mask.fillna(False, inplace=True)
401393
return self._data[match_mask]
402394

403-
def _fuzzy_search(
404-
self, text: str, col: str, threshold: float = 80
405-
) -> pd.DataFrame:
395+
def _fuzzy_search(self, text: str, col: str, threshold: float = 80) -> pd.DataFrame:
406396
try:
407397
# thefuzz is not required to install pgeocode,
408398
# it is an optional dependency for enabling fuzzy search
@@ -413,9 +403,7 @@ def _fuzzy_search(
413403
"It can be installed with: pip install thefuzz[speedup]"
414404
) from err
415405

416-
fuzzy_scores = self._data[col].apply(
417-
lambda x: fuzz.ratio(str(x), text)
418-
)
406+
fuzzy_scores = self._data[col].apply(lambda x: fuzz.ratio(str(x), text))
419407
return self._data[fuzzy_scores >= threshold]
420408

421409

pyproject.toml

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
[build-system]
22
requires = ["setuptools>=42", "wheel"]
33

4-
[tool.black]
5-
line-length = 79
4+
[tool.ruff]
5+
target-version = "py38"
6+
7+
[tool.ruff.lint]
8+
select = [
9+
"B904", # bugbear (Within an except clause, raise exceptions with raise ... from err)
10+
"B905", # bugbear (zip() without an explicit strict= parameter set.)
11+
# "C9", # mccabe complexity
12+
"E", # pycodestyles
13+
"W", # pycodestyles
14+
"F", # pyflakes
15+
"I", # isort
16+
"PLC", # pylint conventions
17+
"PLE", # pylint errors
18+
"UP", # pyupgrade
19+
]
20+
ignore = ["E402", "E501", "E731", "E741"]
621

722
[tool.mypy]
823
python_version = "3.8"
@@ -15,11 +30,6 @@ disallow_untyped_defs = false
1530
disallow_incomplete_defs = true
1631
no_implicit_optional = true
1732

18-
[tool.pycln]
19-
all = true
20-
21-
[tool.isort]
22-
profile = "black"
2333

2434
[tool.tox]
2535
legacy_tox_ini = """

setup.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import io
21
import os
32
import re
43

@@ -16,9 +15,7 @@ def read(*names, **kwargs):
1615

1716
def find_version(*file_paths):
1817
version_file = read(*file_paths)
19-
version_match = re.search(
20-
r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M
21-
)
18+
version_match = re.search(r"^__version__ = ['\"]([^'\"]*)['\"]", version_file, re.M)
2219
if version_match:
2320
return version_match.group(1)
2421

test_pgeocode.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def test_haversine_distance():
165165
try:
166166
from geopy.distance import great_circle
167167
except ImportError:
168-
raise pytest.skip("scikit-learn not installed")
168+
raise pytest.skip("scikit-learn not installed") from None
169169

170170
rng = np.random.RandomState(42)
171171

@@ -213,8 +213,7 @@ def test_open_extract_url(httpserver):
213213
"download_url",
214214
[
215215
"https://download.geonames.org/export/zip/{country}.zip",
216-
"https://symerio.github.io/postal-codes-data/data/"
217-
"geonames/{country}.txt",
216+
"https://symerio.github.io/postal-codes-data/data/" "geonames/{country}.txt",
218217
],
219218
ids=["geonames", "gitlab-pages"],
220219
)
@@ -228,13 +227,9 @@ def test_cdn(temp_dir, monkeypatch, download_url):
228227

229228
def test_url_returns_404(httpserver, monkeypatch, temp_dir):
230229
download_url = "/fr.gzip"
231-
httpserver.expect_oneshot_request(download_url).respond_with_data(
232-
"", status=404
233-
)
230+
httpserver.expect_oneshot_request(download_url).respond_with_data("", status=404)
234231

235-
monkeypatch.setattr(
236-
pgeocode, "DOWNLOAD_URL", [httpserver.url_for(download_url)]
237-
)
232+
monkeypatch.setattr(pgeocode, "DOWNLOAD_URL", [httpserver.url_for(download_url)])
238233
# Nominatim("fr")
239234
with pytest.raises(urllib.error.HTTPError, match="HTTP Error 404"):
240235
Nominatim("fr")
@@ -243,9 +238,7 @@ def test_url_returns_404(httpserver, monkeypatch, temp_dir):
243238

244239
def test_first_url_fails(httpserver, monkeypatch, temp_dir):
245240
download_url = "/IE.txt"
246-
httpserver.expect_oneshot_request(download_url).respond_with_data(
247-
"", status=404
248-
)
241+
httpserver.expect_oneshot_request(download_url).respond_with_data("", status=404)
249242

250243
monkeypatch.setattr(
251244
pgeocode,

0 commit comments

Comments
 (0)