Skip to content

INTPYTHON-380 Add linters and formatters #174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ruff formatting of python files
e7ea851e9b6298be479cdda6656f8c5a5dbe2614
11 changes: 11 additions & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- [Steven Silvester](https://github.com/blink1073)
3 changes: 2 additions & 1 deletion examples/wiki/wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
1 change: 0 additions & 1 deletion flask_pymongo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
__all__ = ("PyMongo", "ASCENDING", "DESCENDING")

import hashlib
from functools import partial
from mimetypes import guess_type

import pymongo
Expand Down
3 changes: 1 addition & 2 deletions flask_pymongo/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@

__version__ = '3.0.0.dev0'
__version__ = "3.0.0.dev0"
2 changes: 1 addition & 1 deletion flask_pymongo/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions flask_pymongo/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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}"
Expand All @@ -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")
4 changes: 2 additions & 2 deletions flask_pymongo/tests/test_gridfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions flask_pymongo/tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -18,19 +18,19 @@ 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)

def tearDown(self):
self.mongo.cx.drop_database(self.dbname)

super(FlaskPyMongoTest, self).tearDown()
super().tearDown()
12 changes: 6 additions & 6 deletions flask_pymongo/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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_
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
Loading