diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..e1e9fc7 --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +# ruff formatting of python files +e7ea851e9b6298be479cdda6656f8c5a5dbe2614 diff --git a/.github/workflows/test-python.yml b/.github/workflows/test-python.yml index 4c8b45d..c8fd1c9 100644 --- a/.github/workflows/test-python.yml +++ b/.github/workflows/test-python.yml @@ -17,6 +17,17 @@ env: MONGODB_VERSION: "7.0" jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - uses: actions/setup-python@v5 + - name: "Run pre-commit" + run: | + pip install -U -q pre-commit + pre-commit run --all-files --hook-stage manual build: runs-on: ${{ matrix.os }} strategy: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..03b3d45 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,46 @@ + +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: check-added-large-files + - id: check-case-conflict + - id: check-toml + - id: check-yaml + - id: debug-statements + - id: end-of-file-fixer + - id: forbid-new-submodules + - id: trailing-whitespace + +# We use the Python version instead of the original version which seems to require Docker +# https://github.com/koalaman/shellcheck-precommit +- repo: https://github.com/shellcheck-py/shellcheck-py + rev: v0.10.0.1 + hooks: + - id: shellcheck + name: shellcheck + args: ["--severity=warning"] + stages: [manual] + +- repo: https://github.com/sirosen/check-jsonschema + rev: 0.31.0 + hooks: + - id: check-github-workflows + args: ["--verbose"] + +- repo: https://github.com/codespell-project/codespell + rev: "v2.3.0" + hooks: + - id: codespell + args: ["-L", "nd"] + stages: [manual] + +- repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.9.1 + hooks: + # Run the linter. + - id: ruff + args: [ --fix ] + # Run the formatter. + - id: ruff-format diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a6399ff..006c6fe 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,4 +87,4 @@ Open `_build/html/index.html` in your browser to view the docs. - [MinJae Kwon](https://github.com/mingrammer) - [yarobob](https://github.com/yarobob) - [Andrew C. Hawkins](https://github.com/achawkins) -- [Steven Silvester](https://github.com/blink1073) \ No newline at end of file +- [Steven Silvester](https://github.com/blink1073) diff --git a/examples/wiki/wiki.py b/examples/wiki/wiki.py index 67008af..b4b64ec 100644 --- a/examples/wiki/wiki.py +++ b/examples/wiki/wiki.py @@ -30,7 +30,8 @@ def wikify(value): for i, part in enumerate(parts): if WIKIWORD.match(part): name = totitle(part) - parts[i] = "[%s](%s)" % (name, url_for("show_page", pagepath=part)) + url = url_for("show_page", pagepath=part) + parts[i] = f"[{name}]({url})" return markdown2.markdown("".join(parts)) diff --git a/flask_pymongo/__init__.py b/flask_pymongo/__init__.py index ccb4926..e1de50c 100644 --- a/flask_pymongo/__init__.py +++ b/flask_pymongo/__init__.py @@ -27,7 +27,6 @@ __all__ = ("PyMongo", "ASCENDING", "DESCENDING") import hashlib -from functools import partial from mimetypes import guess_type import pymongo diff --git a/flask_pymongo/_version.py b/flask_pymongo/_version.py index 5a64317..e05c531 100644 --- a/flask_pymongo/_version.py +++ b/flask_pymongo/_version.py @@ -1,2 +1 @@ - -__version__ = '3.0.0.dev0' +__version__ = "3.0.0.dev0" diff --git a/flask_pymongo/helpers.py b/flask_pymongo/helpers.py index 42f5c3d..2660562 100644 --- a/flask_pymongo/helpers.py +++ b/flask_pymongo/helpers.py @@ -69,7 +69,7 @@ def to_python(self, value): try: return ObjectId(value) except InvalidId: - raise abort(404) + raise abort(404) from None def to_url(self, value): return str(value) diff --git a/flask_pymongo/tests/test_config.py b/flask_pymongo/tests/test_config.py index 66ab157..9667da0 100644 --- a/flask_pymongo/tests/test_config.py +++ b/flask_pymongo/tests/test_config.py @@ -24,13 +24,13 @@ def doesnt_raise(exc=BaseException): class FlaskPyMongoConfigTest(FlaskRequestTest): def setUp(self): - super(FlaskPyMongoConfigTest, self).setUp() + super().setUp() conn = pymongo.MongoClient(port=self.port) conn.test.command("ping") # wait for server def tearDown(self): - super(FlaskPyMongoConfigTest, self).tearDown() + super().tearDown() conn = pymongo.MongoClient(port=self.port) @@ -87,7 +87,7 @@ class CustomDict(dict): mongo.db.things.insert_one({"_id": "thing", "val": "foo"}) - assert type(mongo.db.things.find_one()) == CustomDict + assert type(mongo.db.things.find_one()) is CustomDict def test_it_doesnt_connect_by_default(self): uri = f"mongodb://localhost:{self.port}/{self.dbname}" @@ -112,4 +112,4 @@ def _wait_until_connected(mongo, timeout=1.0): if mongo.cx.nodes: return time.sleep(0.05) - raise CouldNotConnect("could not prove mongodb connected in %r seconds" % timeout) + raise CouldNotConnect(f"could not prove mongodb connected in {timeout} seconds") diff --git a/flask_pymongo/tests/test_gridfs.py b/flask_pymongo/tests/test_gridfs.py index 9866307..c778167 100644 --- a/flask_pymongo/tests/test_gridfs.py +++ b/flask_pymongo/tests/test_gridfs.py @@ -18,7 +18,7 @@ def tearDown(self): for gridfile in files: gridfs.delete(gridfile._id) - super(GridFSCleanupMixin, self).tearDown() + super().tearDown() class TestSaveFile(GridFSCleanupMixin, FlaskPyMongoTest): @@ -49,7 +49,7 @@ def test_it_returns_id(self): class TestSendFile(GridFSCleanupMixin, FlaskPyMongoTest): def setUp(self): - super(TestSendFile, self).setUp() + super().setUp() # make it bigger than 1 gridfs chunk self.myfile = BytesIO(b"a" * 500 * 1024) diff --git a/flask_pymongo/tests/util.py b/flask_pymongo/tests/util.py index 68c2a67..7372e16 100644 --- a/flask_pymongo/tests/util.py +++ b/flask_pymongo/tests/util.py @@ -9,7 +9,7 @@ class FlaskRequestTest(unittest.TestCase): def setUp(self): - super(FlaskRequestTest, self).setUp() + super().setUp() self.dbname = self.__class__.__name__ self.app = flask.Flask("test") @@ -18,14 +18,14 @@ def setUp(self): self.port = 27017 def tearDown(self): - super(FlaskRequestTest, self).tearDown() + super().tearDown() self.context.pop() class FlaskPyMongoTest(FlaskRequestTest): def setUp(self): - super(FlaskPyMongoTest, self).setUp() + super().setUp() uri = f"mongodb://localhost:{self.port}/{self.dbname}" self.mongo = flask_pymongo.PyMongo(self.app, uri) @@ -33,4 +33,4 @@ def setUp(self): def tearDown(self): self.mongo.cx.drop_database(self.dbname) - super(FlaskPyMongoTest, self).tearDown() + super().tearDown() diff --git a/flask_pymongo/wrappers.py b/flask_pymongo/wrappers.py index 7f6076d..dd30d49 100644 --- a/flask_pymongo/wrappers.py +++ b/flask_pymongo/wrappers.py @@ -38,13 +38,13 @@ class MongoClient(mongo_client.MongoClient): """ def __getattr__(self, name): - attr = super(MongoClient, self).__getattr__(name) + attr = super().__getattr__(name) if isinstance(attr, database.Database): return Database(self, name) return attr def __getitem__(self, item): - attr = super(MongoClient, self).__getitem__(item) + attr = super().__getitem__(item) if isinstance(attr, database.Database): return Database(self, item) return attr @@ -60,13 +60,13 @@ class Database(database.Database): """ def __getattr__(self, name): - attr = super(Database, self).__getattr__(name) + attr = super().__getattr__(name) if isinstance(attr, collection.Collection): return Collection(self, name) return attr def __getitem__(self, item): - item_ = super(Database, self).__getitem__(item) + item_ = super().__getitem__(item) if isinstance(item_, collection.Collection): return Collection(self, item) return item_ @@ -76,14 +76,14 @@ class Collection(collection.Collection): """Sub-class of PyMongo :class:`~pymongo.collection.Collection` with helpers.""" def __getattr__(self, name): - attr = super(Collection, self).__getattr__(name) + attr = super().__getattr__(name) if isinstance(attr, collection.Collection): db = self._Collection__database return Collection(db, attr.name) return attr def __getitem__(self, item): - item_ = super(Collection, self).__getitem__(item) + item_ = super().__getitem__(item) if isinstance(item_, collection.Collection): db = self._Collection__database return Collection(db, item_.name) diff --git a/pyproject.toml b/pyproject.toml index 69df3fb..fbe543a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -44,3 +44,18 @@ path = "flask_pymongo/_version.py" include = [ "/flask_pymongo", ] + + +[tool.ruff.lint] +select = [ + "E", # pycodestyle + "F", # Pyflakes + "UP", # pyupgrade + "B", # flake8-bugbear + "I", # isort +] +unfixable = [ + "RUF100", # Unused noqa + "T20", # Removes print statements + "F401", # Unused imports +]