Skip to content

Commit

Permalink
use ruff linter and formatter (#5331)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Nov 9, 2023
2 parents 6edfd78 + 54ff9b2 commit 9a12f34
Show file tree
Hide file tree
Showing 26 changed files with 99 additions and 128 deletions.
25 changes: 0 additions & 25 deletions .flake8

This file was deleted.

29 changes: 7 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
.DS_Store
.env
.flaskenv
*.pyc
*.pyo
env/
venv/
.venv/
env*
dist/
build/
*.egg
*.egg-info/
.tox/
.cache/
.pytest_cache/
.idea/
docs/_build/
.vscode

# Coverage reports
htmlcov/
.vscode/
__pycache__/
.tox/
.coverage
.coverage.*
*,cover
htmlcov/
docs/_build/
dist/
venv/
29 changes: 6 additions & 23 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,16 @@
ci:
autoupdate_branch: "2.3.x"
autoupdate_schedule: monthly
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v3.10.1
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
hooks:
- id: pyupgrade
args: ["--py38-plus"]
- repo: https://github.com/asottile/reorder-python-imports
rev: v3.10.0
hooks:
- id: reorder-python-imports
name: Reorder Python imports (src, tests)
files: "^(?!examples/)"
args: ["--application-directories", "src"]
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
- repo: https://github.com/PyCQA/flake8
rev: 6.1.0
hooks:
- id: flake8
additional_dependencies:
- flake8-bugbear
- flake8-implicit-str-concat
- id: ruff
- id: ruff-format
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-merge-conflict
- id: debug-statements
- id: fix-byte-order-marker
- id: trailing-whitespace
- id: end-of-file-fixer
3 changes: 3 additions & 0 deletions examples/celery/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ build-backend = "flit_core.buildapi"

[tool.flit.module]
name = "task_app"

[tool.ruff]
src = ["src"]
2 changes: 1 addition & 1 deletion examples/javascript/js_example/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from flask import render_template
from flask import request

from js_example import app
from . import app


@app.route("/", defaults={"js": "fetch"})
Expand Down
3 changes: 3 additions & 0 deletions examples/javascript/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ filterwarnings = ["error"]
[tool.coverage.run]
branch = true
source = ["js_example", "tests"]

[tool.ruff]
src = ["src"]
5 changes: 3 additions & 2 deletions examples/tutorial/flaskr/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ def hello():
return "Hello, World!"

# register the database commands
from flaskr import db
from . import db

db.init_app(app)

# apply the blueprints to the app
from flaskr import auth, blog
from . import auth
from . import blog

app.register_blueprint(auth.bp)
app.register_blueprint(blog.bp)
Expand Down
2 changes: 1 addition & 1 deletion examples/tutorial/flaskr/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from werkzeug.security import check_password_hash
from werkzeug.security import generate_password_hash

from flaskr.db import get_db
from .db import get_db

bp = Blueprint("auth", __name__, url_prefix="/auth")

Expand Down
4 changes: 2 additions & 2 deletions examples/tutorial/flaskr/blog.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
from flask import url_for
from werkzeug.exceptions import abort

from flaskr.auth import login_required
from flaskr.db import get_db
from .auth import login_required
from .db import get_db

bp = Blueprint("blog", __name__)

Expand Down
3 changes: 3 additions & 0 deletions examples/tutorial/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ filterwarnings = ["error"]
[tool.coverage.run]
branch = true
source = ["flaskr", "tests"]

[tool.ruff]
src = ["src"]
21 changes: 21 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@ module = [
"importlib_metadata",
]
ignore_missing_imports = true

[tool.ruff]
src = ["src"]
fix = true
show-fixes = true
show-source = true

[tool.ruff.lint]
select = [
"B", # flake8-bugbear
"E", # pycodestyle error
"F", # pyflakes
"I", # isort
"UP", # pyupgrade
"W", # pycodestyle warning
]
ignore-init-module-imports = true

[tool.ruff.lint.isort]
force-single-line = true
order-by-type = false
9 changes: 6 additions & 3 deletions src/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,8 @@ def make_response(self, rv: ft.ResponseReturnValue) -> Response:
# class to the correct type
try:
rv = self.response_class.force_type(
rv, request.environ # type: ignore[arg-type]
rv, # type: ignore[arg-type]
request.environ,
)
except TypeError as e:
raise TypeError(
Expand Down Expand Up @@ -1272,7 +1273,8 @@ def process_response(self, response: Response) -> Response:
return response

def do_teardown_request(
self, exc: BaseException | None = _sentinel # type: ignore
self,
exc: BaseException | None = _sentinel, # type: ignore[assignment]
) -> None:
"""Called after the request is dispatched and the response is
returned, right before the request context is popped.
Expand Down Expand Up @@ -1305,7 +1307,8 @@ def do_teardown_request(
request_tearing_down.send(self, _async_wrapper=self.ensure_sync, exc=exc)

def do_teardown_appcontext(
self, exc: BaseException | None = _sentinel # type: ignore
self,
exc: BaseException | None = _sentinel, # type: ignore[assignment]
) -> None:
"""Called right before the application context is popped.
Expand Down
5 changes: 2 additions & 3 deletions src/flask/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

if t.TYPE_CHECKING: # pragma: no cover
from werkzeug.wrappers import Response as BaseResponse

from .wrappers import Response


Expand Down Expand Up @@ -48,9 +49,7 @@ def get_load_dotenv(default: bool = True) -> bool:


def stream_with_context(
generator_or_function: (
t.Iterator[t.AnyStr] | t.Callable[..., t.Iterator[t.AnyStr]]
)
generator_or_function: t.Iterator[t.AnyStr] | t.Callable[..., t.Iterator[t.AnyStr]]
) -> t.Iterator[t.AnyStr]:
"""Request contexts disappear when the response is started on the server.
This is done for efficiency reasons and to make it less likely to encounter
Expand Down
4 changes: 1 addition & 3 deletions src/flask/json/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,7 @@ class DefaultJSONProvider(JSONProvider):
method) will call the ``__html__`` method to get a string.
"""

default: t.Callable[[t.Any], t.Any] = staticmethod(
_default
) # type: ignore[assignment]
default: t.Callable[[t.Any], t.Any] = staticmethod(_default) # type: ignore[assignment]
"""Apply this function to any object that :meth:`json.dumps` does
not know how to serialize. It should return a valid JSON type or
raise a ``TypeError``.
Expand Down
7 changes: 5 additions & 2 deletions src/flask/sansio/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@

if t.TYPE_CHECKING: # pragma: no cover
from werkzeug.wrappers import Response as BaseResponse
from .blueprints import Blueprint

from ..testing import FlaskClient
from ..testing import FlaskCliRunner
from .blueprints import Blueprint

T_shell_context_processor = t.TypeVar(
"T_shell_context_processor", bound=ft.ShellContextProcessorCallable
Expand Down Expand Up @@ -905,7 +906,9 @@ def redirect(self, location: str, code: int = 302) -> BaseResponse:
Moved from ``flask.redirect``, which calls this method.
"""
return _wz_redirect(
location, code=code, Response=self.response_class # type: ignore[arg-type]
location,
code=code,
Response=self.response_class, # type: ignore[arg-type]
)

def inject_url_defaults(self, endpoint: str, values: dict) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/flask/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

if t.TYPE_CHECKING: # pragma: no cover
from .app import Flask
from .wrappers import Request, Response
from .wrappers import Request
from .wrappers import Response


class SessionMixin(MutableMapping):
Expand Down
1 change: 0 additions & 1 deletion src/flask/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from .globals import current_app
from .globals import request


http_method_funcs = frozenset(
["get", "post", "head", "options", "delete", "put", "trace", "patch"]
)
Expand Down
9 changes: 3 additions & 6 deletions tests/test_appctx.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,17 +196,14 @@ def test_clean_pop(app):

@app.teardown_request
def teardown_req(error=None):
1 / 0
raise ZeroDivisionError

@app.teardown_appcontext
def teardown_app(error=None):
called.append("TEARDOWN")

try:
with app.test_request_context():
called.append(flask.current_app.name)
except ZeroDivisionError:
pass
with app.app_context():
called.append(flask.current_app.name)

assert called == ["flask_test", "TEARDOWN"]
assert not flask.current_app
1 change: 0 additions & 1 deletion tests/test_apps/subdomaintestmodule/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from flask import Module


mod = Module(__name__, "foo", subdomain="foo")
Loading

0 comments on commit 9a12f34

Please sign in to comment.