Browse Source

fix type hinting and checking

pull/2/head
Brett Langdon 5 years ago
parent
commit
3a5d035a0b
Signed by: brettlangdon GPG Key ID: A70042D88B95AA2B
5 changed files with 32 additions and 20 deletions
  1. +4
    -1
      .github/workflows/test.yml
  2. +3
    -3
      sysaudit/__init__.pyi
  3. +6
    -0
      sysaudit/_csysaudit.pyi
  4. +16
    -13
      tests/audit-tests.py
  5. +3
    -3
      tests/test_import.py

+ 4
- 1
.github/workflows/test.yml View File

@ -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:


+ 3
- 3
sysaudit/__init__.pyi View File

@ -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: ...

+ 6
- 0
sysaudit/_csysaudit.pyi View File

@ -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: ...

+ 16
- 13
tests/audit-tests.py View File

@ -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)


+ 3
- 3
tests/test_import.py View File

@ -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

Loading…
Cancel
Save