Skip to content

Commit c79939b

Browse files
committed
refine type hinting
1 parent 441a291 commit c79939b

5 files changed

Lines changed: 14 additions & 15 deletions

File tree

pyproject.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,3 @@ line-length = 90
4242

4343
[tool.mypy]
4444
files = ["src"]
45-
strict_optional = false
46-
allow_redefinition = true
47-
show_error_context = false
48-
show_column_numbers = true

src/findssh/base.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from __future__ import annotations
22
import ipaddress as ip
33
import socket
4-
import typing as T
4+
import logging
5+
from collections.abc import Iterable
56

67

7-
def get_service(b: bytes, service: str | None = None) -> str:
8+
def get_service(b: bytes, service: str | None = None) -> str | None:
89
"""
910
splitlines is in case the ASCII/UTF8 response is less than 32 bytes,
1011
hoping server sends a \r\n
@@ -20,7 +21,7 @@ def get_service(b: bytes, service: str | None = None) -> str:
2021

2122
def is_port_open(
2223
host: ip.IPv4Address, port: int, timeout: float, service: str | None = None
23-
) -> tuple[ip.IPv4Address, str]:
24+
) -> tuple[ip.IPv4Address, str] | None:
2425
"""
2526
is a port open? Without coroutines.
2627
"""
@@ -38,7 +39,8 @@ def is_port_open(
3839
try:
3940
if not (resp := s.recv(32)):
4041
return None
41-
except (socket.timeout, ConnectionError):
42+
except (socket.timeout, ConnectionError) as err:
43+
logging.debug(err)
4244
return None
4345

4446
if svc_txt := get_service(resp, service):
@@ -49,7 +51,7 @@ def is_port_open(
4951

5052
def get_hosts_seq(
5153
net: ip.IPv4Network, port: int, timeout: float, service: str | None = None
52-
) -> T.Iterable[tuple[ip.IPv4Address, str]]:
54+
) -> Iterable[tuple[ip.IPv4Address, str]]:
5355
"""
5456
find hosts sequentially (no parallelism or concurrency)
5557
"""

src/findssh/coro.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ async def get_hosts(
3535

3636

3737
async def waiter(
38-
host: ip.IPv4Address, port: int, service: str, timeout: float
39-
) -> tuple[ip.IPv4Address, str]:
38+
host: ip.IPv4Address, port: int, service: str | None, timeout: float
39+
) -> tuple[ip.IPv4Address, str] | None:
4040
try:
4141
res = await asyncio.wait_for(is_port_open(host, port, service), timeout=timeout)
4242
except asyncio.TimeoutError:
@@ -46,8 +46,8 @@ async def waiter(
4646

4747

4848
async def is_port_open(
49-
host: ip.IPv4Address, port: int, service: str
50-
) -> tuple[ip.IPv4Address, str]:
49+
host: ip.IPv4Address, port: int, service: str | None
50+
) -> tuple[ip.IPv4Address, str] | None:
5151
"""
5252
https://docs.python.org/3/library/asyncio-stream.html#asyncio.open_connection
5353
"""

src/findssh/tests/test_threadpool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
normally we use coroutines, but for demo purposes we have threadpool too.
33
"""
4+
45
import ipaddress
56

67
import findssh.threadpool

src/findssh/threadpool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from __future__ import annotations
88
import concurrent.futures
99
import ipaddress as ip
10-
import typing as T
10+
from collections.abc import Iterable
1111

1212
from .base import is_port_open
1313

@@ -19,7 +19,7 @@ def get_hosts(
1919
port: int,
2020
timeout: float,
2121
service: str | None = None,
22-
) -> T.Iterable[tuple[ip.IPv4Address, str]]:
22+
) -> Iterable[tuple[ip.IPv4Address, str]]:
2323
"""
2424
loops over hosts in network, one thread per address.
2525
This is MUCH slower than asyncio coroutines in coro.py

0 commit comments

Comments
 (0)