Skip to content

Commit

Permalink
Migrate to the Ruff formatter (#300)
Browse files Browse the repository at this point in the history
* Migrate to the Ruff formatter

- Address deprecated linting configs per: https://github.com/microsoft/vscode-python/wiki/Migration-to-Python-Tools-Extensions
- Formatted the codebase with Ruff
- Added poetry-plugin-sort and sorted pyproject.toml with it

* Update ci to use Ruff

- Removed flake8 config file

* Remove ms-python.pylint extension since we're using Ruff

* Move ruff formatter configs to the bottom of pyproject.toml

- Fix docstring that was too long

---------

Co-authored-by: Jumana B <[email protected]>
  • Loading branch information
whabanks and jzbahrai authored Jul 22, 2024
1 parent 9c25f5d commit 56c2f9f
Show file tree
Hide file tree
Showing 56 changed files with 733 additions and 379 deletions.
14 changes: 4 additions & 10 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,24 @@
"dockerfile": "Dockerfile",
"context": ".."
},

"remoteEnv": {
"PATH": "/home/vscode/.local/bin:${containerEnv:PATH}" // give our installed Python modules precedence
},

"containerEnv": {
"SHELL": "/bin/zsh"
},

"settings": {
"python.linting.enabled": true,
"python.linting.pylintEnabled": true,
"python.linting.pylintPath": "/usr/local/bin/pylint",
"python.linting.": true,
"python.pythonPath": "/usr/local/bin/python",
},

"extensions": [
"donjayamanne.python-extension-pack",
"ms-python.python",
"ms-python.vscode-pylance",
"eamodio.gitlens",
"GitHub.copilot",
"bungcip.better-toml",
"tamasfe.even-better-toml",
"charliermarsh.ruff",
],

"postCreateCommand": "notify-dev-entrypoint.sh",
}
}
2 changes: 1 addition & 1 deletion .devcontainer/scripts/notify-dev-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set -ex
echo -e "fpath+=/.zfunc" >> ~/.zshrc
echo -e "autoload -Uz compinit && compinit"

pip install poetry==${POETRY_VERSION} \
pip install poetry==${POETRY_VERSION} poetry-plugin-sort \
&& poetry --version

# Initialize poetry autocompletions
Expand Down
10 changes: 0 additions & 10 deletions .flake8

This file was deleted.

13 changes: 7 additions & 6 deletions .github/actions/waffles/waffles.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,28 @@
--base-url=<url>: Base URL used to hit the application with discovered Flask endpoints.
Example:
waffles.py list --app-loc /Projects/cds/notification-document-download-api --app-lib doc-api-env/Lib/site-packages --flask-mod application --flask-prop application
waffles.py iron --base-url=https://api.document.notification.canada.ca --app-loc /Projects/cds/notification-document-download-api --app-lib doc-api-env/Lib/site-packages --flask-mod application --flask-prop application
waffles.py list --app-loc /Projects/cds/notification-document-download-api \
--app-lib doc-api-env/Lib/site-packages --flask-mod application --flask-prop application
waffles.py iron --base-url=https://api.document.notification.canada.ca --app-loc /Projects/cds/notification-document-download-api \
--app-lib doc-api-env/Lib/site-packages --flask-mod application --flask-prop application
"""

import importlib
import importlib.util
import re
import sys
import urllib.parse
import uuid

from dataclasses import dataclass
from docopt import docopt
from flask import Flask
from os.path import join
from pathlib import Path
import re
from types import ModuleType
from typing import Any, List, NewType
from urllib import request
from urllib.error import URLError

from docopt import docopt
from flask import Flask
from notifications_utils.base64_uuid import uuid_to_base64

ModuleName = NewType("ModuleName", str)
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- name: Install poetry
env:
POETRY_VERSION: "1.7.1"
run: pip install poetry==${POETRY_VERSION} && poetry --version
run: pip install poetry==${POETRY_VERSION} poetry-plugin-sort && poetry --version

- name: Install requirements
run: poetry install
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,8 @@ target/
/cache
/venv

# Ruff formatter cache
.ruff_cache/

# Linux and Mac Dev Environment
.DS_Store
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ clean-docker-containers: ## Clean up any remaining docker containers

.PHONY: format
format:
poetry run black --config pyproject.toml .
poetry run flake8 .
ruff check --select I --fix .
ruff format .
poetry run mypy .
poetry sort

clean:
rm -rf cache venv
2 changes: 1 addition & 1 deletion notifications_utils/base64_uuid.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from base64 import urlsafe_b64decode, urlsafe_b64encode
from uuid import UUID
from base64 import urlsafe_b64encode, urlsafe_b64decode


def base64_to_bytes(key):
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/clients/redis/redis_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from time import time
from typing import Any, Dict

from flask_redis import FlaskRedis
from flask import current_app
from flask_redis import FlaskRedis

# expose redis exceptions so that they can be caught
from redis.exceptions import RedisError # noqa
Expand Down
4 changes: 3 additions & 1 deletion notifications_utils/clients/zendesk/zendesk_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ def create_ticket(
data["ticket"]["requester"] = {"email": user_email, "name": user_name or "(no name supplied)"}

response = requests.post(
self.ZENDESK_TICKET_URL, json=data, auth=("{}/token".format(self.NOTIFY_ZENDESK_EMAIL), self.api_key) # type: ignore
self.ZENDESK_TICKET_URL,
json=data,
auth=("{}/token".format(self.NOTIFY_ZENDESK_EMAIL), self.api_key), # type: ignore
)

if response.status_code != 201:
Expand Down
3 changes: 2 additions & 1 deletion notifications_utils/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import current_app
from functools import wraps

from flask import current_app


def requires_feature(flag):
def decorator_feature_flag(func):
Expand Down
6 changes: 3 additions & 3 deletions notifications_utils/field.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import re
from typing import Any, Callable, Dict, List, Literal, Optional

from ordered_set import OrderedSet
from flask import Markup
from ordered_set import OrderedSet

from notifications_utils.columns import Columns
from notifications_utils.formatters import (
unescaped_formatted_list,
strip_html,
escape_html,
strip_dvla_markup,
strip_html,
unescaped_formatted_list,
)


Expand Down
13 changes: 7 additions & 6 deletions notifications_utils/formatters.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import string
import re
from typing import List
import string
import urllib
from itertools import count
from typing import List

import mistune
import bleach
from itertools import count
import mistune
import smartypants
from flask import Markup
from . import email_with_smart_quotes_regex

from notifications_utils.sanitise_text import SanitiseSMS
import smartypants

from . import email_with_smart_quotes_regex

LINK_STYLE = "word-wrap: break-word;"

Expand Down
3 changes: 2 additions & 1 deletion notifications_utils/international_billing_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
- Dominican Republic
"""

import yaml
import os

import yaml

dir_path = os.path.dirname(os.path.realpath(__file__))

with open("{}/international_billing_rates.yml".format(dir_path)) as f:
Expand Down
7 changes: 3 additions & 4 deletions notifications_utils/letter_timings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import pytz

from datetime import datetime, time, timedelta
from collections import namedtuple
from datetime import datetime, time, timedelta

from notifications_utils.timezones import convert_utc_to_est, utc_string_to_aware_gmt_datetime
import pytz

from notifications_utils.timezones import convert_utc_to_est, utc_string_to_aware_gmt_datetime

LETTER_PROCESSING_DEADLINE = time(17, 30)
CANCELLABLE_JOB_LETTER_STATUSES = [
Expand Down
15 changes: 7 additions & 8 deletions notifications_utils/logging.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
from itertools import product
from pathlib import Path
import logging
import logging.handlers
import re
import sys

from flask import request, g
from flask.ctx import has_request_context
from pythonjsonlogger.jsonlogger import JsonFormatter as BaseJSONFormatter
from itertools import product
from pathlib import Path
from time import monotonic
from typing import Any

import logging
import logging.handlers
from flask import g, request
from flask.ctx import has_request_context
from pythonjsonlogger.jsonlogger import JsonFormatter as BaseJSONFormatter

LOG_FORMAT = "%(asctime)s %(app_name)s %(name)s %(levelname)s " '%(request_id)s "%(message)s" [in %(pathname)s:%(lineno)d]'
TIME_FORMAT = "%Y-%m-%dT%H:%M:%S"
Expand Down
3 changes: 2 additions & 1 deletion notifications_utils/pdf.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import io

import PyPDF2
from PyPDF2 import PdfFileWriter
from PyPDF2.utils import PdfReadError
import io


def pdf_page_count(src_pdf):
Expand Down
27 changes: 15 additions & 12 deletions notifications_utils/recipients.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
import re
import sys
import csv
import phonenumbers
import os
from io import StringIO
import re
import sys
from collections import OrderedDict, namedtuple
from contextlib import suppress
from functools import lru_cache, partial
from io import StringIO
from itertools import islice
from collections import OrderedDict, namedtuple
from ordered_set import OrderedSet
from typing import Callable, Dict, List
from notifications_utils import SMS_CHAR_COUNT_LIMIT

import phonenumbers
from flask import current_app
from notifications_utils.sanitise_text import SanitiseSMS
from . import EMAIL_REGEX_PATTERN, hostname_part, tld_part
from ordered_set import OrderedSet

from notifications_utils import SMS_CHAR_COUNT_LIMIT
from notifications_utils.columns import Cell, Columns, Row
from notifications_utils.formatters import strip_and_remove_obscure_whitespace, strip_whitespace
from notifications_utils.template import SMSMessageTemplate, Template
from notifications_utils.columns import Columns, Row, Cell
from notifications_utils.international_billing_rates import (
INTERNATIONAL_BILLING_RATES,
)
from notifications_utils.sanitise_text import SanitiseSMS
from notifications_utils.template import SMSMessageTemplate, Template

from . import EMAIL_REGEX_PATTERN, hostname_part, tld_part

country_code = os.getenv("PHONE_COUNTRY_CODE", "1")
region_code = os.getenv("PHONE_REGION_CODE", "US")
Expand Down Expand Up @@ -139,7 +141,8 @@ def placeholders(self, value):
Columns.make_key(placeholder) for placeholder in self.recipient_column_headers
]
self.recipient_column_headers_lang_check_as_column_keys = [
Columns.make_key(placeholder) for placeholder in self.recipient_column_headers_lang_check # type: ignore
Columns.make_key(placeholder)
for placeholder in self.recipient_column_headers_lang_check # type: ignore
]

@property
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/request_helper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from flask import abort, current_app, request
from flask.wrappers import Request
from flask import request, current_app, abort


class NotifyRequest(Request):
Expand Down
4 changes: 2 additions & 2 deletions notifications_utils/statsd_decorators.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import functools

from flask import current_app
from time import monotonic
from typing import Type

from flask import current_app


def statsd(namespace):
def time_function(func):
Expand Down
1 change: 0 additions & 1 deletion notifications_utils/strftime_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import platform


NO_PAD_POSIX_CHAR = "-"
NO_PAD_WINDOWS_CHAR = "#"

Expand Down
Loading

0 comments on commit 56c2f9f

Please sign in to comment.