1+ from __future__ import annotations
2+
13import os
24import errno
5+ from typing import TYPE_CHECKING
36
47from ._abc import Stream
58from ._util import ConflictDetector , Final
69
710import trio
811
12+ if TYPE_CHECKING :
13+ from typing_extensions import Final as FinalType
14+
915if os .name != "posix" :
1016 # We raise an error here rather than gating the import in lowlevel.py
1117 # in order to keep jedi static analysis happy.
1218 raise ImportError
1319
1420# XX TODO: is this a good number? who knows... it does match the default Linux
1521# pipe capacity though.
16- DEFAULT_RECEIVE_SIZE = 65536
22+ DEFAULT_RECEIVE_SIZE : FinalType = 65536
1723
1824
1925class _FdHolder :
@@ -34,7 +40,9 @@ class _FdHolder:
3440 # impossible to make this mistake – we'll just get an EBADF.
3541 #
3642 # (This trick was copied from the stdlib socket module.)
37- def __init__ (self , fd : int ):
43+ fd : int
44+
45+ def __init__ (self , fd : int ) -> None :
3846 # make sure self.fd is always initialized to *something*, because even
3947 # if we error out here then __del__ will run and access it.
4048 self .fd = - 1
@@ -46,10 +54,10 @@ def __init__(self, fd: int):
4654 os .set_blocking (fd , False )
4755
4856 @property
49- def closed (self ):
57+ def closed (self ) -> bool :
5058 return self .fd == - 1
5159
52- def _raw_close (self ):
60+ def _raw_close (self ) -> None :
5361 # This doesn't assume it's in a Trio context, so it can be called from
5462 # __del__. You should never call it from Trio context, because it
5563 # skips calling notify_fd_close. But from __del__, skipping that is
@@ -64,10 +72,10 @@ def _raw_close(self):
6472 os .set_blocking (fd , self ._original_is_blocking )
6573 os .close (fd )
6674
67- def __del__ (self ):
75+ def __del__ (self ) -> None :
6876 self ._raw_close ()
6977
70- def close (self ):
78+ def close (self ) -> None :
7179 if not self .closed :
7280 trio .lowlevel .notify_closing (self .fd )
7381 self ._raw_close ()
@@ -93,7 +101,7 @@ class FdStream(Stream, metaclass=Final):
93101 thrust upon them. For example, you can use
94102 ``FdStream(os.dup(sys.stdin.fileno()))`` to obtain a stream for reading
95103 from standard input, but it is only safe to do so with heavy caveats: your
96- stdin must not be shared by any other processes and you must not make any
104+ stdin must not be shared by any other processes, and you must not make any
97105 calls to synchronous methods of `sys.stdin` until the stream returned by
98106 `FdStream` is closed. See `issue #174
99107 <https://github.com/python-trio/trio/issues/174>`__ for a discussion of the
@@ -106,7 +114,7 @@ class FdStream(Stream, metaclass=Final):
106114 A new `FdStream` object.
107115 """
108116
109- def __init__ (self , fd : int ):
117+ def __init__ (self , fd : int ) -> None :
110118 self ._fd_holder = _FdHolder (fd )
111119 self ._send_conflict_detector = ConflictDetector (
112120 "another task is using this stream for send"
@@ -115,7 +123,7 @@ def __init__(self, fd: int):
115123 "another task is using this stream for receive"
116124 )
117125
118- async def send_all (self , data : bytes ):
126+ async def send_all (self , data : bytes ) -> None :
119127 with self ._send_conflict_detector :
120128 # have to check up front, because send_all(b"") on a closed pipe
121129 # should raise
@@ -151,7 +159,7 @@ async def wait_send_all_might_not_block(self) -> None:
151159 # of sending, which is annoying
152160 raise trio .BrokenResourceError from e
153161
154- async def receive_some (self , max_bytes = None ) -> bytes :
162+ async def receive_some (self , max_bytes : int | None = None ) -> bytes :
155163 with self ._receive_conflict_detector :
156164 if max_bytes is None :
157165 max_bytes = DEFAULT_RECEIVE_SIZE
@@ -179,12 +187,12 @@ async def receive_some(self, max_bytes=None) -> bytes:
179187
180188 return data
181189
182- def close (self ):
190+ def close (self ) -> None :
183191 self ._fd_holder .close ()
184192
185- async def aclose (self ):
193+ async def aclose (self ) -> None :
186194 self .close ()
187195 await trio .lowlevel .checkpoint ()
188196
189- def fileno (self ):
197+ def fileno (self ) -> int :
190198 return self ._fd_holder .fd
0 commit comments