Skip to content

Commit eff7c03

Browse files
authored
INTPYTHON-380 Add linters and formatters (#174)
1 parent 481ad53 commit eff7c03

File tree

13 files changed

+95
-22
lines changed

13 files changed

+95
-22
lines changed

.git-blame-ignore-revs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# ruff formatting of python files
2+
e7ea851e9b6298be479cdda6656f8c5a5dbe2614

.github/workflows/test-python.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ env:
1717
MONGODB_VERSION: "7.0"
1818

1919
jobs:
20+
pre-commit:
21+
runs-on: ubuntu-latest
22+
steps:
23+
- uses: actions/checkout@v4
24+
with:
25+
persist-credentials: false
26+
- uses: actions/setup-python@v5
27+
- name: "Run pre-commit"
28+
run: |
29+
pip install -U -q pre-commit
30+
pre-commit run --all-files --hook-stage manual
2031
build:
2132
runs-on: ${{ matrix.os }}
2233
strategy:

.pre-commit-config.yaml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
repos:
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v5.0.0
5+
hooks:
6+
- id: check-added-large-files
7+
- id: check-case-conflict
8+
- id: check-toml
9+
- id: check-yaml
10+
- id: debug-statements
11+
- id: end-of-file-fixer
12+
- id: forbid-new-submodules
13+
- id: trailing-whitespace
14+
15+
# We use the Python version instead of the original version which seems to require Docker
16+
# https://github.com/koalaman/shellcheck-precommit
17+
- repo: https://github.com/shellcheck-py/shellcheck-py
18+
rev: v0.10.0.1
19+
hooks:
20+
- id: shellcheck
21+
name: shellcheck
22+
args: ["--severity=warning"]
23+
stages: [manual]
24+
25+
- repo: https://github.com/sirosen/check-jsonschema
26+
rev: 0.31.0
27+
hooks:
28+
- id: check-github-workflows
29+
args: ["--verbose"]
30+
31+
- repo: https://github.com/codespell-project/codespell
32+
rev: "v2.3.0"
33+
hooks:
34+
- id: codespell
35+
args: ["-L", "nd"]
36+
stages: [manual]
37+
38+
- repo: https://github.com/astral-sh/ruff-pre-commit
39+
# Ruff version.
40+
rev: v0.9.1
41+
hooks:
42+
# Run the linter.
43+
- id: ruff
44+
args: [ --fix ]
45+
# Run the formatter.
46+
- id: ruff-format

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ Open `_build/html/index.html` in your browser to view the docs.
8787
- [MinJae Kwon](https://github.com/mingrammer)
8888
- [yarobob](https://github.com/yarobob)
8989
- [Andrew C. Hawkins](https://github.com/achawkins)
90-
- [Steven Silvester](https://github.com/blink1073)
90+
- [Steven Silvester](https://github.com/blink1073)

examples/wiki/wiki.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ def wikify(value):
3030
for i, part in enumerate(parts):
3131
if WIKIWORD.match(part):
3232
name = totitle(part)
33-
parts[i] = "[%s](%s)" % (name, url_for("show_page", pagepath=part))
33+
url = url_for("show_page", pagepath=part)
34+
parts[i] = f"[{name}]({url})"
3435
return markdown2.markdown("".join(parts))
3536

3637

flask_pymongo/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
__all__ = ("PyMongo", "ASCENDING", "DESCENDING")
2828

2929
import hashlib
30-
from functools import partial
3130
from mimetypes import guess_type
3231

3332
import pymongo

flask_pymongo/_version.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
2-
__version__ = '3.0.0.dev0'
1+
__version__ = "3.0.0.dev0"

flask_pymongo/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def to_python(self, value):
6969
try:
7070
return ObjectId(value)
7171
except InvalidId:
72-
raise abort(404)
72+
raise abort(404) from None
7373

7474
def to_url(self, value):
7575
return str(value)

flask_pymongo/tests/test_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def doesnt_raise(exc=BaseException):
2424

2525
class FlaskPyMongoConfigTest(FlaskRequestTest):
2626
def setUp(self):
27-
super(FlaskPyMongoConfigTest, self).setUp()
27+
super().setUp()
2828

2929
conn = pymongo.MongoClient(port=self.port)
3030
conn.test.command("ping") # wait for server
3131

3232
def tearDown(self):
33-
super(FlaskPyMongoConfigTest, self).tearDown()
33+
super().tearDown()
3434

3535
conn = pymongo.MongoClient(port=self.port)
3636

@@ -87,7 +87,7 @@ class CustomDict(dict):
8787

8888
mongo.db.things.insert_one({"_id": "thing", "val": "foo"})
8989

90-
assert type(mongo.db.things.find_one()) == CustomDict
90+
assert type(mongo.db.things.find_one()) is CustomDict
9191

9292
def test_it_doesnt_connect_by_default(self):
9393
uri = f"mongodb://localhost:{self.port}/{self.dbname}"
@@ -112,4 +112,4 @@ def _wait_until_connected(mongo, timeout=1.0):
112112
if mongo.cx.nodes:
113113
return
114114
time.sleep(0.05)
115-
raise CouldNotConnect("could not prove mongodb connected in %r seconds" % timeout)
115+
raise CouldNotConnect(f"could not prove mongodb connected in {timeout} seconds")

flask_pymongo/tests/test_gridfs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def tearDown(self):
1818
for gridfile in files:
1919
gridfs.delete(gridfile._id)
2020

21-
super(GridFSCleanupMixin, self).tearDown()
21+
super().tearDown()
2222

2323

2424
class TestSaveFile(GridFSCleanupMixin, FlaskPyMongoTest):
@@ -49,7 +49,7 @@ def test_it_returns_id(self):
4949

5050
class TestSendFile(GridFSCleanupMixin, FlaskPyMongoTest):
5151
def setUp(self):
52-
super(TestSendFile, self).setUp()
52+
super().setUp()
5353

5454
# make it bigger than 1 gridfs chunk
5555
self.myfile = BytesIO(b"a" * 500 * 1024)

flask_pymongo/tests/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class FlaskRequestTest(unittest.TestCase):
1111
def setUp(self):
12-
super(FlaskRequestTest, self).setUp()
12+
super().setUp()
1313

1414
self.dbname = self.__class__.__name__
1515
self.app = flask.Flask("test")
@@ -18,19 +18,19 @@ def setUp(self):
1818
self.port = 27017
1919

2020
def tearDown(self):
21-
super(FlaskRequestTest, self).tearDown()
21+
super().tearDown()
2222

2323
self.context.pop()
2424

2525

2626
class FlaskPyMongoTest(FlaskRequestTest):
2727
def setUp(self):
28-
super(FlaskPyMongoTest, self).setUp()
28+
super().setUp()
2929

3030
uri = f"mongodb://localhost:{self.port}/{self.dbname}"
3131
self.mongo = flask_pymongo.PyMongo(self.app, uri)
3232

3333
def tearDown(self):
3434
self.mongo.cx.drop_database(self.dbname)
3535

36-
super(FlaskPyMongoTest, self).tearDown()
36+
super().tearDown()

flask_pymongo/wrappers.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class MongoClient(mongo_client.MongoClient):
3838
"""
3939

4040
def __getattr__(self, name):
41-
attr = super(MongoClient, self).__getattr__(name)
41+
attr = super().__getattr__(name)
4242
if isinstance(attr, database.Database):
4343
return Database(self, name)
4444
return attr
4545

4646
def __getitem__(self, item):
47-
attr = super(MongoClient, self).__getitem__(item)
47+
attr = super().__getitem__(item)
4848
if isinstance(attr, database.Database):
4949
return Database(self, item)
5050
return attr
@@ -60,13 +60,13 @@ class Database(database.Database):
6060
"""
6161

6262
def __getattr__(self, name):
63-
attr = super(Database, self).__getattr__(name)
63+
attr = super().__getattr__(name)
6464
if isinstance(attr, collection.Collection):
6565
return Collection(self, name)
6666
return attr
6767

6868
def __getitem__(self, item):
69-
item_ = super(Database, self).__getitem__(item)
69+
item_ = super().__getitem__(item)
7070
if isinstance(item_, collection.Collection):
7171
return Collection(self, item)
7272
return item_
@@ -76,14 +76,14 @@ class Collection(collection.Collection):
7676
"""Sub-class of PyMongo :class:`~pymongo.collection.Collection` with helpers."""
7777

7878
def __getattr__(self, name):
79-
attr = super(Collection, self).__getattr__(name)
79+
attr = super().__getattr__(name)
8080
if isinstance(attr, collection.Collection):
8181
db = self._Collection__database
8282
return Collection(db, attr.name)
8383
return attr
8484

8585
def __getitem__(self, item):
86-
item_ = super(Collection, self).__getitem__(item)
86+
item_ = super().__getitem__(item)
8787
if isinstance(item_, collection.Collection):
8888
db = self._Collection__database
8989
return Collection(db, item_.name)

pyproject.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ path = "flask_pymongo/_version.py"
4444
include = [
4545
"/flask_pymongo",
4646
]
47+
48+
49+
[tool.ruff.lint]
50+
select = [
51+
"E", # pycodestyle
52+
"F", # Pyflakes
53+
"UP", # pyupgrade
54+
"B", # flake8-bugbear
55+
"I", # isort
56+
]
57+
unfixable = [
58+
"RUF100", # Unused noqa
59+
"T20", # Removes print statements
60+
"F401", # Unused imports
61+
]

0 commit comments

Comments
 (0)