From 5a146efb4612a9d8d49424c31b6abd89ead366cc Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 13 Jan 2025 20:58:15 -0600 Subject: [PATCH 1/6] INTPYTHON-380 Add linters and formatters --- .git-blame-ignore-revs | 2 ++ .github/workflows/test-python.yml | 11 +++++++ .pre-commit-config.yaml | 49 ++++++++++++++++++++++++++++++ examples/wiki/wiki.py | 3 +- flask_pymongo/__init__.py | 1 - flask_pymongo/_version.py | 3 +- flask_pymongo/helpers.py | 2 +- flask_pymongo/tests/test_config.py | 8 ++--- flask_pymongo/tests/test_gridfs.py | 4 +-- flask_pymongo/tests/util.py | 8 ++--- flask_pymongo/wrappers.py | 12 ++++---- pyproject.toml | 15 +++++++++ 12 files changed, 97 insertions(+), 21 deletions(-) create mode 100644 .git-blame-ignore-revs create mode 100644 .pre-commit-config.yaml 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..6cf8883 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,49 @@ + +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.5.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 + exclude_types: [json] + exclude: | + (?x)^(.*.ambr)$ + +# 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.9.0.6 + hooks: + - id: shellcheck + name: shellcheck + args: ["--severity=warning"] + stages: [manual] + +- repo: https://github.com/sirosen/check-jsonschema + rev: 0.29.4 + hooks: + - id: check-github-workflows + args: ["--verbose"] + +- repo: https://github.com/codespell-project/codespell + rev: "v2.2.6" + hooks: + - id: codespell + # args: ["-L", "fle,fo,infinit,isnt,nin,te,aks"] + stages: [manual] + +- repo: https://github.com/astral-sh/ruff-pre-commit + # Ruff version. + rev: v0.8.5 + hooks: + # Run the linter. + - id: ruff + args: [ --fix ] + # Run the formatter. + - id: ruff-format 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..4017774 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 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 +] From 803a6c1dd6ee06baf8e3d354c730e811cf88103f Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 13 Jan 2025 21:00:04 -0600 Subject: [PATCH 2/6] fix test --- flask_pymongo/tests/test_config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_pymongo/tests/test_config.py b/flask_pymongo/tests/test_config.py index 4017774..9667da0 100644 --- a/flask_pymongo/tests/test_config.py +++ b/flask_pymongo/tests/test_config.py @@ -87,7 +87,7 @@ class CustomDict(dict): mongo.db.things.insert_one({"_id": "thing", "val": "foo"}) - assert mongo.db.things.find_one() is 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}" From 003f65b23a3f048303ab455b974726b7158ee482 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 13 Jan 2025 21:08:31 -0600 Subject: [PATCH 3/6] codespell fix --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 6cf8883..28ffc39 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -35,7 +35,7 @@ repos: rev: "v2.2.6" hooks: - id: codespell - # args: ["-L", "fle,fo,infinit,isnt,nin,te,aks"] + args: ["-L", "nd"] stages: [manual] - repo: https://github.com/astral-sh/ruff-pre-commit From 1bdba2f9f97a880cc093b4c9ee60987bd8b6ab4c Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 13 Jan 2025 21:11:15 -0600 Subject: [PATCH 4/6] update pre-commit --- .pre-commit-config.yaml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 28ffc39..8effa01 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -11,9 +11,6 @@ repos: - id: end-of-file-fixer - id: forbid-new-submodules - id: trailing-whitespace - exclude_types: [json] - exclude: | - (?x)^(.*.ambr)$ # We use the Python version instead of the original version which seems to require Docker # https://github.com/koalaman/shellcheck-precommit From c0b87ab02f404ddad061e66130f8399a30b1b3c1 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 13 Jan 2025 21:13:48 -0600 Subject: [PATCH 5/6] update pre-commit --- .pre-commit-config.yaml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 8effa01..03b3d45 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,7 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.5.0 + rev: v5.0.0 hooks: - id: check-added-large-files - id: check-case-conflict @@ -15,7 +15,7 @@ repos: # 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.9.0.6 + rev: v0.10.0.1 hooks: - id: shellcheck name: shellcheck @@ -23,13 +23,13 @@ repos: stages: [manual] - repo: https://github.com/sirosen/check-jsonschema - rev: 0.29.4 + rev: 0.31.0 hooks: - id: check-github-workflows args: ["--verbose"] - repo: https://github.com/codespell-project/codespell - rev: "v2.2.6" + rev: "v2.3.0" hooks: - id: codespell args: ["-L", "nd"] @@ -37,7 +37,7 @@ repos: - repo: https://github.com/astral-sh/ruff-pre-commit # Ruff version. - rev: v0.8.5 + rev: v0.9.1 hooks: # Run the linter. - id: ruff From c8d2b827e585cde126be4b1cd9f5f56c5b8d0605 Mon Sep 17 00:00:00 2001 From: Steven Silvester Date: Mon, 13 Jan 2025 21:15:13 -0600 Subject: [PATCH 6/6] format --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)