Skip to content

Commit 5a146ef

Browse files
committed
INTPYTHON-380 Add linters and formatters
1 parent e7ea851 commit 5a146ef

File tree

12 files changed

+97
-21
lines changed

12 files changed

+97
-21
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
repos:
3+
- repo: https://github.com/pre-commit/pre-commit-hooks
4+
rev: v4.5.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+
exclude_types: [json]
15+
exclude: |
16+
(?x)^(.*.ambr)$
17+
18+
# We use the Python version instead of the original version which seems to require Docker
19+
# https://github.com/koalaman/shellcheck-precommit
20+
- repo: https://github.com/shellcheck-py/shellcheck-py
21+
rev: v0.9.0.6
22+
hooks:
23+
- id: shellcheck
24+
name: shellcheck
25+
args: ["--severity=warning"]
26+
stages: [manual]
27+
28+
- repo: https://github.com/sirosen/check-jsonschema
29+
rev: 0.29.4
30+
hooks:
31+
- id: check-github-workflows
32+
args: ["--verbose"]
33+
34+
- repo: https://github.com/codespell-project/codespell
35+
rev: "v2.2.6"
36+
hooks:
37+
- id: codespell
38+
# args: ["-L", "fle,fo,infinit,isnt,nin,te,aks"]
39+
stages: [manual]
40+
41+
- repo: https://github.com/astral-sh/ruff-pre-commit
42+
# Ruff version.
43+
rev: v0.8.5
44+
hooks:
45+
# Run the linter.
46+
- id: ruff
47+
args: [ --fix ]
48+
# Run the formatter.
49+
- id: ruff-format

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 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()

0 commit comments

Comments
 (0)