Skip to content
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

Linter and type hinting fixes #415

Merged
merged 5 commits into from
Jun 2, 2023
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
5 changes: 4 additions & 1 deletion .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
- '**.py'
- '**.js'
- '**.jinja2'
- pyproject.toml
- test_requirements.txt
- .github/workflows/pytest.yml

permissions:
contents: read
Expand All @@ -19,7 +22,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.7', '3.10']
python-version: ['3.7', '3.8', '3.9', '3.10', '3.11']

services:
redis:
Expand Down
7 changes: 5 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,13 @@ repos:
# https://github.com/python/mypy/issues/2960
args: ['--no-warn-unused-ignores', '--ignore-missing-imports']
additional_dependencies:
- flask
- sqlalchemy
- toml
- types-all
- types-bleach
- types-python-dateutil
- types-pytz
- types-requests
- typing-extensions
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
Expand All @@ -87,7 +91,6 @@ repos:
'--disable=import-error',
'-rn', # Disable full report
'-sn', # Disable evaluation score
'--ignore-paths=tests,migrations',
]
additional_dependencies:
- tomli
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ classifiers = [
]
dependencies = [
'bleach',
'blinker',
'coaster',
'cssmin',
'dnspython',
Expand Down Expand Up @@ -136,7 +137,6 @@ pythonpath = 'src'
minversion = '6.0'
addopts = '--doctest-modules --ignore setup.py --cov-report=term-missing'
doctest_optionflags = ['ALLOW_UNICODE', 'ALLOW_BYTES']
filterwarnings = ['ignore::DeprecationWarning', 'ignore::UserWarning']

[tool.pylint.master]
# max-parents = 10
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# This is invoked from ``pyproject.toml``
class BaseframeBuildPy(build_py):
def run(self):
def run(self) -> None:
build_py.run(self)
if not self._dry_run:
curdir = os.getcwd()
Expand Down
70 changes: 70 additions & 0 deletions src/baseframe/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

from . import __version__

__all__ = ['Version', 'assets']

#: Semantic-versioned assets
assets = VersionedAssets()

Expand Down Expand Up @@ -191,6 +193,18 @@
assets['codemirror.mode.css.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/mode/css/css.js'
assets['codemirror.mode.javascript.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/mode/javascript/javascript.js'
assets['codemirror.addon.display.fullscreen.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/display/fullscreen.js'
assets['codemirror.addon.display.fullscreen.css'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/display/fullscreen.css'
assets['codemirror.addon.display.rulers.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/display/rulers.js'
assets['codemirror.addon.mode.overlay.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/mode/overlay.js'
Expand All @@ -203,12 +217,36 @@
assets['codemirror.addon.edit.matchbrackets.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/edit/matchbrackets.js'
assets['codemirror.addon.hint.show-hint.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/hint/show-hint.js'
assets['codemirror.addon.hint.show-hint.css'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/hint/show-hint.css'
assets['codemirror.addon.hint.css-hint.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/hint/css-hint.js'
assets['codemirror.addon.hint.javascript-hint.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/hint/javascript-hint.js'
assets['codemirror.addon.lint.css-lint.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/lint/css-lint.js'
assets['codemirror.addon.lint.json-lint.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/lint/json-lint.js'
assets['codemirror.addon.fold.brace-fold.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/fold/brace-fold.js'
assets['codemirror.addon.fold.foldcode.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/fold/foldcode.js'
assets['codemirror.addon.fold.foldgutter.js'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/fold/foldgutter.js'
assets['codemirror.addon.fold.foldgutter.css'][
Version('4.11.0')
] = 'baseframe/js/codemirror/addon/fold/foldgutter.css'

assets['codemirror-markdown.js'][Version('4.11.0')] = {
'requires': [
Expand All @@ -229,11 +267,43 @@
'requires': [
'codemirror.js==4.11.0',
'codemirror.mode.css.js==4.11.0',
'codemirror.addon.hint.show-hint.js==4.11.0',
'codemirror.addon.hint.css-hint.js==4.11.0',
'codemirror.addon.lint.css-lint.js==4.11.0',
'codemirror.addon.edit.matchbrackets.js==4.11.0',
'codemirror.addon.edit.closebrackets.js==4.11.0',
]
}
assets['codemirror-css.css'][Version('4.11.0')] = {
'requires': [
'codemirror.css==4.11.0',
'codemirror.addon.hint.show-hint.css==4.11.0',
]
}
assets['codemirror-json.js'][Version('4.11.0')] = {
'requires': [
'codemirror.js==4.11.0',
'codemirror.mode.javascript.js==4.11.0',
'codemirror.addon.display.rulers.js==4.11.0',
'codemirror.addon.display.fullscreen.js==4.11.0',
'codemirror.addon.hint.show-hint.js==4.11.0',
'codemirror.addon.hint.javascript-hint.js==4.11.0',
'codemirror.addon.lint.json-lint.js==4.11.0',
'codemirror.addon.fold.foldcode.js==4.11.0',
'codemirror.addon.fold.foldgutter.js==4.11.0',
'codemirror.addon.fold.brace-fold.js==4.11.0',
'codemirror.addon.edit.matchbrackets.js==4.11.0',
'codemirror.addon.edit.closebrackets.js==4.11.0',
]
}
assets['codemirror-json.css'][Version('4.11.0')] = {
'requires': [
'codemirror.css==4.11.0',
'codemirror.addon.fold.foldgutter.css==4.11.0',
'codemirror.addon.display.fullscreen.css==4.11.0',
'codemirror.addon.hint.show-hint.css==4.11.0',
]
}

assets['codemirror.js'][
Version('5.53.2')
Expand Down
27 changes: 12 additions & 15 deletions src/baseframe/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from sentry_sdk.integrations.rq import RqIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
import sentry_sdk
import typing_extensions as te

from coaster.app import RotatingKeySecureCookieSessionInterface
from coaster.assets import split_namespec
Expand Down Expand Up @@ -84,13 +85,12 @@ def init_app(
app: Flask,
requires: t.Iterable[str] = (),
ext_requires: t.Iterable[t.Union[str, t.List[str], t.Tuple[str, ...]]] = (),
bundle_js=None,
bundle_css=None,
assetenv=None,
theme: str = 'bootstrap3',
asset_modules=(),
error_handlers=True,
):
bundle_js: t.Optional[Bundle] = None,
bundle_css: t.Optional[Bundle] = None,
assetenv: t.Optional[Environment] = None,
theme: te.Literal['bootstrap3', 'mui'] = 'bootstrap3',
error_handlers: bool = True,
) -> None:
"""
Initialize an app with Baseframe and load necessary assets.

Expand Down Expand Up @@ -217,12 +217,12 @@ def init_app(
css_all = Bundle(css_all, bundle_css)

if assetenv is None:
app.assets = Environment(app)
app.assets = Environment(app) # type: ignore[attr-defined]
else:
app.assets = assetenv
app.assets.register('js_all', js_all)
app.assets.register('css_all', css_all)
app.assets.register(
app.assets = assetenv # type: ignore[attr-defined]
app.assets.register('js_all', js_all) # type: ignore[attr-defined]
app.assets.register('css_all', css_all) # type: ignore[attr-defined]
app.assets.register( # type: ignore[attr-defined]
'fa5-sprite',
Bundle(
assets.require('font-awesome5-sprite.svg'),
Expand All @@ -231,9 +231,6 @@ def init_app(
)
app.register_blueprint(self, static_subdomain=subdomain)

if asset_modules:
app.logger.warning("Baseframe no longer supports asset modules")

# Optional config for a client app to use a manifest file
# to load fingerprinted assets
# If used with webpack, the client app is expected to specify its own
Expand Down
19 changes: 9 additions & 10 deletions src/baseframe/errors.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
"""Default error handling."""

import typing as t
from __future__ import annotations

from flask import current_app, redirect, request
from werkzeug.exceptions import MethodNotAllowed, NotFound
from werkzeug.routing import RequestRedirect
from werkzeug.wrappers import Response

from coaster.views import render_with
from coaster.views import ReturnRenderWith, render_with

from .extensions import baseframe_translations


@render_with('errors/400.html.jinja2', json=True)
def error400(_exc) -> t.Tuple[dict, int]: # TODO: Use ReturnRenderWith
def error400(_exc: Exception) -> ReturnRenderWith:
"""Render a 400 error."""
baseframe_translations.as_default()
return {'error': "400 Bad Request"}, 400


@render_with('errors/403.html.jinja2', json=True)
def error403(_exc) -> t.Tuple[dict, int]: # TODO: Use ReturnRenderWith
def error403(_exc: Exception) -> ReturnRenderWith:
"""Render a 403 error."""
baseframe_translations.as_default()
return {'error': "403 Forbidden"}, 403


@render_with('errors/404.html.jinja2', json=True)
def error404(
_exc,
) -> t.Union[Response, t.Tuple[dict, int]]: # TODO: Use ReturnRenderWith
_exc: Exception,
) -> ReturnRenderWith:
"""Render a 404 error."""
if request.path.endswith('/') and request.method == 'GET':
# If the URL has a trailing slash, check if there's an endpoint handler that
Expand All @@ -53,21 +52,21 @@ def error404(


@render_with('errors/422.html.jinja2', json=True)
def error422(_exc) -> t.Tuple[dict, int]: # TODO: Use ReturnRenderWith
def error422(_exc: Exception) -> ReturnRenderWith:
"""Render a 422 error (substitute for 400 when syntax is ok, contents not)."""
baseframe_translations.as_default()
return {'error': "422 Unprocessable Entity"}, 422


@render_with('errors/429.html.jinja2', json=True)
def error429(_exc) -> t.Tuple[dict, int]: # TODO: Use ReturnRenderWith
def error429(_exc: Exception) -> ReturnRenderWith:
"""Render a 429 error."""
baseframe_translations.as_default()
return {'error': "429 Too Many Requests"}, 429


@render_with('errors/500.html.jinja2', json=True)
def error500(_exc) -> t.Tuple[dict, int]: # TODO: Use ReturnRenderWith
def error500(_exc: Exception) -> ReturnRenderWith:
"""Render a 500 error."""
if current_app.extensions and 'sqlalchemy' in current_app.extensions:
current_app.extensions['sqlalchemy'].db.session.rollback()
Expand Down
Loading