Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to all defs and enable disallow_untyped_defs #96

Merged
merged 2 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions nextline/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class OnStartRun(Event):
run_no: RunNo
statement: Statement

def __post_init__(self):
def __post_init__(self) -> None:
_assert_aware_datetime(self.started_at)


Expand All @@ -36,7 +36,7 @@ class OnEndRun(Event):
returned: str
raised: str

def __post_init__(self):
def __post_init__(self) -> None:
_assert_aware_datetime(self.ended_at)


Expand All @@ -48,7 +48,7 @@ class OnStartTrace(Event):
thread_no: ThreadNo
task_no: Optional[TaskNo]

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.started_at)


Expand All @@ -58,7 +58,7 @@ class OnEndTrace(Event):
run_no: RunNo
trace_no: TraceNo

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.ended_at)


Expand All @@ -73,7 +73,7 @@ class OnStartTraceCall(Event):
frame_object_id: int
event: str

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.started_at)


Expand All @@ -84,7 +84,7 @@ class OnEndTraceCall(Event):
trace_no: TraceNo
trace_call_no: TraceCallNo

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.ended_at)


Expand All @@ -95,7 +95,7 @@ class OnStartCmdloop(Event):
trace_no: TraceNo
trace_call_no: TraceCallNo

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.started_at)


Expand All @@ -106,7 +106,7 @@ class OnEndCmdloop(Event):
trace_no: TraceNo
trace_call_no: TraceCallNo

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.ended_at)


Expand All @@ -123,7 +123,7 @@ class OnStartPrompt(Event):
frame_object_id: int
event: str

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.started_at)


Expand All @@ -136,7 +136,7 @@ class OnEndPrompt(Event):
prompt_no: PromptNo
command: str

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.ended_at)


Expand All @@ -147,15 +147,15 @@ class OnWriteStdout(Event):
trace_no: TraceNo
text: str

def __post_init__(self):
def __post_init__(self) -> None:
_assert_naive_datetime(self.written_at)


def _assert_naive_datetime(dt: datetime.datetime):
def _assert_naive_datetime(dt: datetime.datetime) -> None:
if is_timezone_aware(dt):
raise ValueError(f'Not a timezone-naive object: {dt!r}')


def _assert_aware_datetime(dt: datetime.datetime):
def _assert_aware_datetime(dt: datetime.datetime) -> None:
if not is_timezone_aware(dt):
raise ValueError(f'Not a timezone-aware object: {dt!r}')
3 changes: 1 addition & 2 deletions nextline/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ async def __aenter__(self) -> 'Nextline':
await self.start()
return self

async def __aexit__(self, exc_type, exc_value, traceback) -> None:
del exc_type, exc_value, traceback
async def __aexit__(self, *_: Any, **__: Any) -> None:
await asyncio.wait_for(self.close(), timeout=self._timeout_on_exit)

async def run(self) -> None:
Expand Down
5 changes: 4 additions & 1 deletion nextline/utils/multiprocessing_logging.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import asyncio
from collections.abc import AsyncIterator, Callable
import contextlib
import logging
import multiprocessing as mp
Expand All @@ -19,7 +20,9 @@ def example_func() -> None:


@contextlib.asynccontextmanager
async def MultiprocessingLogging(mp_context: Optional[BaseContext] = None):
async def MultiprocessingLogging(
mp_context: Optional[BaseContext] = None,
) -> AsyncIterator[Callable[[], None]]:
'''Collect logging from other processes in the main process.

Example:
Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ target_version = ['py310', 'py311', 'py312']
profile = "black"

[tool.mypy]
# disallow_untyped_defs = true
exclude = ['script\.py']
disallow_untyped_defs = true
exclude = '''(?x)(
example/.*\.py$
| test_script\.py$
)'''


[[tool.mypy.overrides]]
Expand Down
5 changes: 3 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import sys
import threading
from collections.abc import Iterator

import pytest


@pytest.fixture(autouse=True)
def recover_trace():
def recover_trace() -> Iterator[None]:
"""Set the original trace function back after each test"""
trace_org = sys.gettrace()
yield
sys.settrace(trace_org)
threading.settrace(trace_org)
threading.settrace(trace_org) # type: ignore
2 changes: 1 addition & 1 deletion tests/fsm/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_restore_from_markup() -> None:


@pytest.mark.skip
def test_graph(tmp_path: Path):
def test_graph(tmp_path: Path) -> None:
FILE_NAME = 'states.png'
path = tmp_path / FILE_NAME
# print(f'Saving the state diagram to {path}...')
Expand Down
4 changes: 2 additions & 2 deletions tests/main/scenarios/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .funcs import extract_comment


async def test_run(statement: str):
async def test_run(statement: str) -> None:
nextline = Nextline(statement, trace_threads=True, trace_modules=True)
assert nextline.state == 'created'
plugin = Plugin()
Expand Down Expand Up @@ -131,7 +131,7 @@ def find_command(line: str) -> Optional[str]:


@pytest.fixture
def statement(script_dir, monkey_patch_syspath) -> str:
def statement(script_dir : str, monkey_patch_syspath: None) -> str:
del monkey_patch_syspath
return (Path(script_dir) / 'script.py').read_text()

Expand Down
2 changes: 1 addition & 1 deletion tests/main/scenarios/test_interruption.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def on_finished(self, context: Context) -> None:
assert 'KeyboardInterrupt' in fmt_exc


async def run(nextline: Nextline):
async def run(nextline: Nextline) -> None:
async with nextline.run_session():
pass
fmt_exc = nextline.format_exception()
Expand Down
2 changes: 1 addition & 1 deletion tests/main/scenarios/test_kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def on_finished(self, context: Context) -> None:
assert exited_process.process.exitcode == -signal.SIGKILL


async def run(nextline: Nextline):
async def run(nextline: Nextline) -> None:
async with nextline.run_session():
pass
assert not nextline.format_exception()
Expand Down
2 changes: 1 addition & 1 deletion tests/main/scenarios/test_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def on_finished(self, context: Context) -> None:
assert 'RuntimeError' in fmt_exc


async def run(nextline: Nextline):
async def run(nextline: Nextline) -> None:
async with nextline.run_session():
pass
fmt_exc = nextline.format_exception()
Expand Down
2 changes: 1 addition & 1 deletion tests/main/scenarios/test_raise_dynamic_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async def on_finished(self, context: Context) -> None:
assert 'MyError' in fmt_exc


async def run(nextline: Nextline):
async def run(nextline: Nextline) -> None:
async with nextline.run_session():
pass
fmt_exc = nextline.format_exception()
Expand Down
2 changes: 1 addition & 1 deletion tests/main/scenarios/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async def on_finished(self, context: Context) -> None:
assert exited_process.process.exitcode == 0


async def run(nextline: Nextline):
async def run(nextline: Nextline) -> None:
async with nextline.run_session():
pass
assert not nextline.format_exception()
Expand Down
2 changes: 1 addition & 1 deletion tests/main/scenarios/test_terminate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ async def on_finished(self, context: Context) -> None:
assert exited_process.process.exitcode == -signal.SIGTERM


async def run(nextline: Nextline):
async def run(nextline: Nextline) -> None:
async with nextline.run_session():
pass
assert not nextline.format_exception()
Expand Down
8 changes: 4 additions & 4 deletions tests/main/test_nextline.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
""".strip()


def test_init_sync():
def test_init_sync() -> None:
'''Assert the init without the running loop.'''
with pytest.raises(RuntimeError):
asyncio.get_running_loop()
nextline = Nextline(SOURCE)
assert nextline


async def test_repr():
async def test_repr() -> None:
nextline = Nextline(SOURCE)
assert repr(nextline)
async with nextline:
Expand All @@ -48,8 +48,8 @@ async def test_one() -> None:
)


async def test_timeout(imp: Mock):
async def close():
async def test_timeout(imp: Mock) -> None:
async def close() -> None:
await asyncio.sleep(5)

imp.aclose.side_effect = close
Expand Down
2 changes: 1 addition & 1 deletion tests/main/test_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from nextline import Nextline


def func():
def func() -> None:
time.sleep(0.001)


Expand Down
2 changes: 1 addition & 1 deletion tests/main/test_register.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from nextline.plugin.spec import Context, hookimpl


def func():
def func() -> None:
time.sleep(0.001)


Expand Down
2 changes: 1 addition & 1 deletion tests/script.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def script():
def script() -> None:
for i in range(3):
print("in script() i={}".format(i))
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from nextline.utils import profile_func


def test_timeit(plugin: FilterByModuleName):
def test_timeit(plugin: FilterByModuleName) -> None:
n_calls = 200_000

thread_id = threading.current_thread().ident
Expand All @@ -24,7 +24,7 @@ def test_timeit(plugin: FilterByModuleName):
assert sec < 1


def test_profile(plugin: FilterByModuleName):
def test_profile(plugin: FilterByModuleName) -> None:
'''Used to print the profile.'''

n_calls = 20_000
Expand All @@ -34,7 +34,7 @@ def test_profile(plugin: FilterByModuleName):

frame = sys._current_frames()[thread_id]

def func():
def func() -> None:
for _ in range(n_calls):
plugin.filter((frame, "line", None))

Expand All @@ -44,7 +44,7 @@ def func():


@pytest.fixture()
def plugin():
def plugin() -> FilterByModuleName:
p = FilterByModuleName()
p.init(modules_to_skip=MODULES_TO_SKIP)
return p
2 changes: 1 addition & 1 deletion tests/spawned/run/example/lib.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
def g():
def g() -> None:
print('here!')
pass
Loading