Skip to content

Commit

Permalink
Adding MyPy to CI (#90)
Browse files Browse the repository at this point in the history
* added dependency, added config file

* added some type declarations, ignore comments

* simplified test data
  • Loading branch information
smcmurtry authored Jul 12, 2021
1 parent 98dbf4c commit befc52c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 53 deletions.
38 changes: 38 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[mypy]
python_version = 3.9

[mypy-orderedset.*]
ignore_missing_imports = True

[mypy-setuptools.*]
ignore_missing_imports = True

[mypy-PyPDF2.*]
ignore_missing_imports = True

[mypy-requests_mock.*]
ignore_missing_imports = True

[mypy-boto3.*]
ignore_missing_imports = True

[mypy-botocore.*]
ignore_missing_imports = True

[mypy-flask_redis.*]
ignore_missing_imports = True

[mypy-mistune.*]
ignore_missing_imports = True

[mypy-smartypants.*]
ignore_missing_imports = True

[mypy-statsd.*]
ignore_missing_imports = True

[mypy-pythonjsonlogger.*]
ignore_missing_imports = True

[mypy-phonenumbers.*]
ignore_missing_imports = True
3 changes: 2 additions & 1 deletion notifications_utils/clients/redis/redis_client.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import numbers
import uuid
from time import time
from typing import Any, Dict

from flask_redis import FlaskRedis
from flask import current_app
Expand Down Expand Up @@ -39,7 +40,7 @@ def prepare_value(val):
class RedisClient:
redis_store = FlaskRedis()
active = False
scripts = {}
scripts: Dict[str, Any] = {}

def init_app(self, app):
self.active = app.config.get("REDIS_ENABLED")
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/clients/statsd/statsd_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self, host, port, prefix):
def _resolve(self, addr):
return gethostbyname(addr)

@cachetools.func.ttl_cache(maxsize=2, ttl=10, timer=time_monotonic_with_jitter)
@cachetools.func.ttl_cache(maxsize=2, ttl=10, timer=time_monotonic_with_jitter) # type: ignore
def _cached_host(self):
try:
return self._resolve(self._host)
Expand Down
2 changes: 1 addition & 1 deletion notifications_utils/recipients.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def is_local_phone_number(number):
return True


international_phone_info = namedtuple(
international_phone_info = namedtuple( # type: ignore
"PhoneNumber",
[
"international",
Expand Down
3 changes: 2 additions & 1 deletion notifications_utils/sanitise_text.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import unicodedata
from typing import Set


class SanitiseText:
ALLOWED_CHARACTERS = set()
ALLOWED_CHARACTERS: Set = set()

REPLACEMENT_CHARACTERS = {
"–": "-", # EN DASH (U+2013)
Expand Down
1 change: 1 addition & 0 deletions requirements_for_test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ requests-mock==1.8.0
freezegun==1.1.0
flake8==3.9.1
flake8-print==4.0.0
mypy==0.812
black==21.5b2
75 changes: 26 additions & 49 deletions tests/test_recipient_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,55 +40,32 @@
valid_phone_numbers = valid_local_phone_numbers + valid_international_phone_numbers


invalid_local_phone_numbers = sum(
[
[(phone_number, error) for phone_number in group]
for error, group in [
(
"Not a valid local number",
(
"712345678910",
"0712345678910",
"0044712345678910",
"0044712345678910",
"+44 (0)7123 456 789 10",
),
),
(
"Not a valid local number",
(
"0712345678",
"004471234567",
"00447123456",
"+44 (0)7123 456 78",
),
),
(
"Not a valid local number",
(
"08081 570364",
"+44 8081 570364",
"0117 496 0860",
"+44 117 496 0860",
"020 7946 0991",
"+44 20 7946 0991",
),
),
(
"Not a valid local number",
(
"07890x32109",
"07123 456789...",
"07123 ☟☜⬇⬆☞☝",
"07123☟☜⬇⬆☞☝",
"+44 07ab cde fgh",
"ALPHANUM3R1C",
),
),
]
],
[],
)
invalid_local_phone_numbers = [
(phone_number, "Not a valid local number")
for phone_number in (
"712345678910",
"0712345678910",
"0044712345678910",
"0044712345678910",
"+44 (0)7123 456 789 10",
"0712345678",
"004471234567",
"00447123456",
"+44 (0)7123 456 78",
"08081 570364",
"+44 8081 570364",
"0117 496 0860",
"+44 117 496 0860",
"020 7946 0991",
"+44 20 7946 0991",
"07890x32109",
"07123 456789...",
"07123 ☟☜⬇⬆☞☝",
"07123☟☜⬇⬆☞☝",
"+44 07ab cde fgh",
"ALPHANUM3R1C",
)
]


invalid_phone_numbers = [
Expand Down

0 comments on commit befc52c

Please sign in to comment.