Skip to content

Commit d2bafae

Browse files
committed
Add type annotations to adafruit_fona_socket.py
1 parent a243db1 commit d2bafae

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

adafruit_fona/adafruit_fona_socket.py

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,22 @@
1616
import time
1717
from micropython import const
1818

19+
try:
20+
from typing import Optional, Tuple, Sequence
21+
from adafruit_fona.adafruit_fona import FONA
22+
except ImportError:
23+
pass
24+
1925
_the_interface = None # pylint: disable=invalid-name
2026

2127

22-
def set_interface(iface):
28+
def set_interface(iface: FONA) -> None:
2329
"""Helper to set the global internet interface."""
2430
global _the_interface # pylint: disable=global-statement, invalid-name
2531
_the_interface = iface
2632

2733

28-
def htonl(x):
34+
def htonl(x: int) -> int:
2935
"""Convert 32-bit positive integers from host to network byte order."""
3036
return (
3137
((x) << 24 & 0xFF000000)
@@ -35,7 +41,7 @@ def htonl(x):
3541
)
3642

3743

38-
def htons(x):
44+
def htons(x: int) -> int:
3945
"""Convert 16-bit positive integers from host to network byte order."""
4046
return (((x) << 8) & 0xFF00) | (((x) >> 8) & 0xFF)
4147

@@ -50,7 +56,7 @@ def htons(x):
5056
SOCKETS = []
5157

5258
# pylint: disable=too-many-arguments, unused-argument
53-
def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
59+
def getaddrinfo(host, port: int, family=0, socktype=0, proto=0, flags=0):
5460
"""Translate the host/port argument into a sequence of 5-tuples that
5561
contain all the necessary arguments for creating a socket connected to that service.
5662
"""
@@ -59,9 +65,10 @@ def getaddrinfo(host, port, family=0, socktype=0, proto=0, flags=0):
5965
return [(AF_INET, socktype, proto, "", (gethostbyname(host), port))]
6066

6167

62-
def gethostbyname(hostname):
68+
def gethostbyname(hostname: str) -> str:
6369
"""Translate a host name to IPv4 address format. The IPv4 address
6470
is returned as a string.
71+
6572
:param str hostname: Desired hostname.
6673
"""
6774
addr = _the_interface.get_host_by_name(hostname)
@@ -72,14 +79,19 @@ def gethostbyname(hostname):
7279
class socket:
7380
"""A simplified implementation of the Python 'socket' class
7481
for connecting to a FONA cellular module.
82+
7583
:param int family: Socket address (and protocol) family.
7684
:param int type: Socket type.
77-
7885
"""
7986

8087
def __init__(
81-
self, family=AF_INET, type=SOCK_STREAM, proto=0, fileno=None, socknum=None
82-
):
88+
self,
89+
family: int = AF_INET,
90+
type: int = SOCK_STREAM,
91+
proto: int = 0,
92+
fileno: Optional[int] = None,
93+
socknum: Optional[int] = None,
94+
) -> None:
8395
if family != AF_INET:
8496
raise RuntimeError("Only AF_INET family supported by cellular sockets.")
8597
self._sock_type = type
@@ -94,35 +106,37 @@ def __init__(
94106
self.settimeout(self._timeout)
95107

96108
@property
97-
def socknum(self):
109+
def socknum(self) -> int:
98110
"""Returns the socket object's socket number."""
99111
return self._socknum
100112

101113
@property
102-
def connected(self):
114+
def connected(self) -> bool:
103115
"""Returns whether or not we are connected to the socket."""
104116
return _the_interface.socket_status(self.socknum)
105117

106-
def getpeername(self):
118+
def getpeername(self) -> str:
107119
"""Return the remote address to which the socket is connected."""
108120
return _the_interface.remote_ip(self.socknum)
109121

110-
def inet_aton(self, ip_string):
122+
def inet_aton(self, ip_string: str) -> bytearray:
111123
"""Convert an IPv4 address from dotted-quad string format.
112-
:param str ip_string: IP Address, as a dotted-quad string.
113124
125+
:param str ip_string: IP Address, as a dotted-quad string.
114126
"""
115127
self._buffer = b""
116128
self._buffer = [int(item) for item in ip_string.split(".")]
117129
self._buffer = bytearray(self._buffer)
118130
return self._buffer
119131

120-
def connect(self, address, conn_mode=None):
132+
def connect(
133+
self, address: Tuple[str, int], conn_mode: Optional[int] = None
134+
) -> None:
121135
"""Connect to a remote socket at address. (The format of address depends
122136
on the address family — see above.)
137+
123138
:param tuple address: Remote socket as a (host, port) tuple.
124139
:param int conn_mode: Connection mode (TCP/UDP)
125-
126140
"""
127141
assert (
128142
conn_mode != 0x03
@@ -135,17 +149,18 @@ def connect(self, address, conn_mode=None):
135149
raise RuntimeError("Failed to connect to host", host)
136150
self._buffer = b""
137151

138-
def send(self, data):
152+
def send(self, data: bytes) -> None:
139153
"""Send data to the socket. The socket must be connected to
140154
a remote socket prior to calling this method.
141-
:param bytes data: Desired data to send to the socket.
142155
156+
:param bytes data: Desired data to send to the socket.
143157
"""
144158
_the_interface.socket_write(self._socknum, data, self._timeout)
145159
gc.collect()
146160

147-
def recv(self, bufsize=0):
161+
def recv(self, bufsize: int = 0) -> Sequence[int]:
148162
"""Reads some bytes from the connected remote address.
163+
149164
:param int bufsize: maximum number of bytes to receive
150165
"""
151166
# print("Socket read", bufsize)
@@ -189,7 +204,7 @@ def recv(self, bufsize=0):
189204
gc.collect()
190205
return ret
191206

192-
def readline(self):
207+
def readline(self) -> Sequence[int]:
193208
"""Attempt to return as many bytes as we can up to but not including '\r\n'"""
194209
# print("Socket readline")
195210
stamp = time.monotonic()
@@ -205,26 +220,25 @@ def readline(self):
205220
gc.collect()
206221
return firstline
207222

208-
def available(self):
223+
def available(self) -> int:
209224
"""Returns how many bytes are available to be read from the socket."""
210225
return _the_interface.socket_available(self._socknum)
211226

212-
def settimeout(self, value):
227+
def settimeout(self, value: int) -> None:
213228
"""Sets socket read timeout.
214-
:param int value: Socket read timeout, in seconds.
215229
230+
:param int value: Socket read timeout, in seconds.
216231
"""
217232
if value < 0:
218233
raise Exception("Timeout period should be non-negative.")
219234
self._timeout = value
220235

221-
def gettimeout(self):
236+
def gettimeout(self) -> int:
222237
"""Return the timeout in seconds (float) associated
223238
with socket operations, or None if no timeout is set.
224-
225239
"""
226240
return self._timeout
227241

228-
def close(self):
242+
def close(self) -> bool:
229243
"""Closes the socket."""
230244
return _the_interface.socket_close(self._socknum)

0 commit comments

Comments
 (0)