From e94732f1329b86e9515d14d270f575b457294d86 Mon Sep 17 00:00:00 2001 From: Avasam Date: Mon, 26 Aug 2024 00:04:46 -0400 Subject: [PATCH] Strict typing and link issues --- jaraco/pmxbot/__init__.py | 4 ++-- jaraco/pmxbot/notification.py | 18 ++++++++++-------- jaraco/pmxbot/py.typed | 0 mypy.ini | 10 +++++++++- pyproject.toml | 4 ---- tests/test_notification.py | 16 ++++++++++------ 6 files changed, 31 insertions(+), 21 deletions(-) create mode 100644 jaraco/pmxbot/py.typed diff --git a/jaraco/pmxbot/__init__.py b/jaraco/pmxbot/__init__.py index a5fcf1d..a4550c7 100644 --- a/jaraco/pmxbot/__init__.py +++ b/jaraco/pmxbot/__init__.py @@ -3,8 +3,8 @@ from pmxbot.core import command -@command("resolv", doc="resolve a hostname") -def resolve(rest): +@command("resolv", doc="resolve a hostname") # type: ignore[misc] # pmxbot/pmxbot#113 +def resolve(rest: str) -> str: """ >>> resolve("localhost") '...' diff --git a/jaraco/pmxbot/notification.py b/jaraco/pmxbot/notification.py index a0ee1fb..544e45d 100644 --- a/jaraco/pmxbot/notification.py +++ b/jaraco/pmxbot/notification.py @@ -1,15 +1,17 @@ +from __future__ import annotations + import re import twilio.rest + import pmxbot from pmxbot.core import command - from_number = '+15712573984' -@command() -def send_text(rest): +@command() # type: ignore[misc] # pmxbot/pmxbot#113 +def send_text(rest: str) -> str | None: """ Send an SMS message: pass the phone number and message to send. """ @@ -18,17 +20,17 @@ def send_text(rest): number, _, msg = rest.partition(' ') number = parse_number(number) if not msg: - return - msg = msg.encode('ascii')[:160] + return None + encoded_msg = msg.encode('ascii')[:160] client = twilio.rest.Client(username=account, password=token) - client.messages.create(to=number, from_=from_number, body=msg) + client.messages.create(to=number, from_=from_number, body=encoded_msg) return "Sent {count} chars to {number}".format( - count=len(msg), + count=len(encoded_msg), number=number, ) -def parse_number(input_): +def parse_number(input_: str) -> str: """ Strip everything but digits and + sign; ensure it begins with a country code. diff --git a/jaraco/pmxbot/py.typed b/jaraco/pmxbot/py.typed new file mode 100644 index 0000000..e69de29 diff --git a/mypy.ini b/mypy.ini index 83b0d15..fb93117 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,6 +1,6 @@ [mypy] # Is the project well-typed? -strict = False +strict = True # Early opt-in even when strict = False warn_unused_ignores = True @@ -12,3 +12,11 @@ explicit_package_bases = True # Disable overload-overlap due to many false-positives disable_error_code = overload-overlap + +# pmxbot/pmxbot#113 +[mypy-pmxbot.*] +ignore_missing_imports = True + +# twilio/twilio-python#568 +[mypy-twilio.*] +ignore_missing_imports = True diff --git a/pyproject.toml b/pyproject.toml index 2d3298e..46f0e98 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -73,7 +73,3 @@ notification = "jaraco.pmxbot.notification" [tool.setuptools_scm] - - -[tool.pytest-enabler.mypy] -# Disabled due to jaraco/skeleton#143 diff --git a/tests/test_notification.py b/tests/test_notification.py index 99324ab..8e96252 100644 --- a/tests/test_notification.py +++ b/tests/test_notification.py @@ -1,13 +1,15 @@ -import pytest +from typing import Dict + import pmxbot -from jaraco.collections import ItemsAsAttributes +import pytest +from jaraco.collections import ItemsAsAttributes from jaraco.pmxbot import notification @pytest.fixture -def twilio_test_credentials(monkeypatch): - class ConfigDict(ItemsAsAttributes, dict): +def twilio_test_credentials(monkeypatch: pytest.MonkeyPatch) -> None: + class ConfigDict(ItemsAsAttributes, Dict[str, str]): pass monkeypatch.setattr(pmxbot, 'config', ConfigDict(), raising=False) @@ -24,10 +26,12 @@ class ConfigDict(ItemsAsAttributes, dict): monkeypatch.setattr(notification, 'from_number', '+15005550006') -def test_send_text(twilio_test_credentials): +@pytest.mark.usefixtures("twilio_test_credentials") +def test_send_text() -> None: res = notification.send_text(rest='+12026837967 <3 pmxbot') assert res == 'Sent 9 chars to +12026837967' -def test_no_message(twilio_test_credentials): +@pytest.mark.usefixtures("twilio_test_credentials") +def test_no_message() -> None: assert not notification.send_text(rest='')