diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75aa911..4e28eab 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -24,7 +24,10 @@ jobs: python-version: "3.9" - uses: Gr1N/setup-poetry@v4 - run: poetry install - - run: poetry run mypy --py2 sysaudit/ + - run: poetry run mypy --py2 sysaudit/ tests/ + name: Mypy Check Python2 + - run: poetry run mypy sysaudit/ tests/ + name: Mypy Check Python3 test: runs-on: ubuntu-latest strategy: diff --git a/sysaudit/__init__.pyi b/sysaudit/__init__.pyi index 881a3f3..cd66550 100644 --- a/sysaudit/__init__.pyi +++ b/sysaudit/__init__.pyi @@ -1,6 +1,6 @@ import typing -AuditHookCallable = typing.Callable[[str, typing.Tuple[typing.Any, ...]], None] - def audit(event: str, *args: typing.Any) -> None: ... -def addaudithook(AuditHookCallable) -> None: ... +def addaudithook( + hook: typing.Callable[[str, typing.Tuple[typing.Any, ...]], None] +) -> None: ... diff --git a/sysaudit/_csysaudit.pyi b/sysaudit/_csysaudit.pyi new file mode 100644 index 0000000..cd66550 --- /dev/null +++ b/sysaudit/_csysaudit.pyi @@ -0,0 +1,6 @@ +import typing + +def audit(event: str, *args: typing.Any) -> None: ... +def addaudithook( + hook: typing.Callable[[str, typing.Tuple[typing.Any, ...]], None] +) -> None: ... diff --git a/tests/audit-tests.py b/tests/audit-tests.py index 99e00f0..4358408 100644 --- a/tests/audit-tests.py +++ b/tests/audit-tests.py @@ -6,6 +6,7 @@ module with arguments identifying each test. """ import contextlib +import typing import sys import sysaudit @@ -24,7 +25,7 @@ class TestHook: self.seen = [] self.closed = False - def __enter__(self, *a): + def __enter__(self, *a): # type: (typing.Any) -> TestHook sysaudit.addaudithook(self) return self @@ -38,7 +39,9 @@ class TestHook: def seen_events(self): return [i[0] for i in self.seen] - def __call__(self, event, args): + def __call__( + self, event, args + ): # type: (str, typing.Tuple[typing.Any, ...]) -> None if self.closed: return self.seen.append((event, args)) @@ -246,12 +249,12 @@ def test_mmap(): assertEqual(hook.seen[0][1][:2], (-1, 8)) -def test_excepthook(): +def test_excepthook(): # type: () -> None def excepthook(exc_type, exc_value, exc_tb): if exc_type is not RuntimeError: sys.__excepthook__(exc_type, exc_value, exc_tb) - def hook(event, args): + def hook(event, args): # type: (str, typing.Tuple[typing.Any, ...]) -> None if event == "sys.excepthook": if not isinstance(args[2], args[1]): raise TypeError( @@ -266,27 +269,27 @@ def test_excepthook(): raise RuntimeError("fatal-error") -def test_unraisablehook(): - from _testcapi import write_unraisable_exc +def test_unraisablehook(): # type: () -> None + from _testcapi import write_unraisable_exc # type: ignore def unraisablehook(hookargs): pass - def hook(event, args): + def hook(event, args): # type: (str, typing.Tuple[typing.Any, ...]) -> None if event == "sys.unraisablehook": if args[0] != unraisablehook: raise ValueError("Expected {} == {}".format(args[0], unraisablehook)) print(event, repr(args[1].exc_value), args[1].err_msg) sysaudit.addaudithook(hook) - sys.unraisablehook = unraisablehook + sys.unraisablehook = unraisablehook # type: ignore [attr-defined] write_unraisable_exc(RuntimeError("nonfatal-error"), "for audit hook test", None) -def test_winreg(): - from winreg import OpenKey, EnumKey, CloseKey, HKEY_LOCAL_MACHINE +def test_winreg(): # type: () -> None + from winreg import OpenKey, EnumKey, CloseKey, HKEY_LOCAL_MACHINE # type: ignore - def hook(event, args): + def hook(event, args): # type: (str, typing.Tuple[typing.Any, ...]) -> None if not event.startswith("winreg."): return print(event, args) @@ -306,10 +309,10 @@ def test_winreg(): CloseKey(kv) -def test_socket(): +def test_socket(): # type: () -> None import socket - def hook(event, args): + def hook(event, args): # type: (str, typing.Tuple[typing.Any, ...]) -> None if event.startswith("socket."): print(event, args) diff --git a/tests/test_import.py b/tests/test_import.py index 064b148..7842bf5 100644 --- a/tests/test_import.py +++ b/tests/test_import.py @@ -3,10 +3,10 @@ import sys import sysaudit -def test_module(): +def test_module(): # type: () -> None if sys.version_info >= (3, 8, 0): - assert sysaudit.audit == sys.audit - assert sysaudit.addaudithook == sys.addaudithook + assert sysaudit.audit == sys.audit # type: ignore [attr-defined] + assert sysaudit.addaudithook == sys.addaudithook # type: ignore [attr-defined] else: assert sysaudit.audit == sysaudit._csysaudit.audit assert sysaudit.addaudithook == sysaudit._csysaudit.addaudithook