You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fileobj is the file object to monitor. It may either be an integer file descriptor or an object with a fileno() method.
However, the problem is that there doesn't seem to an ABC that exactly describes "an object with a fileno() method". I initially tried using io.IOBase and typing.IO, but the socket class doesn't seem to extend either base class (or share many of the methods, for that matter).
What is the correct course of action here? Should I try and petition to add a new ABC/modify the io module to try and unify all these different kinds of "file-like objects", or should I just settle for something more ad-hoc like the following?
This is one more use case for structural typing, but in the meantime I would do something ad-hoc like you describe rather than make yet another custom ABC (those are hard to introduce and hard to get rid of).
I actually expect that the only real use cases are sockets and ints -- by the time someone has an IOBase or IO[...] instance they are probably talking about a disk file, which selectors don't support. Also most subclasses of IOBase do some form of buffering which makes using a selector a no-no. Calling fileno() isn't really a hardship, and callers that don't have any other reference to the object wouldn't know what to do when they got called back.
Regarding IOBase vs. IO[...], that area is a bit of a mess, but basically the concrete implementation classes don't really have a proper subtyping relationship, so for the type system we are probably wise to ignore them.
Could this be revisited now that protocols are supported in MyPy and Python 3.8? Make the type be Union[int, SupportsFileNo], where SupportsFileNo is a custom protocol that checks for the function fileno().
I'd be happy to submit a patch if you all would like.
Activity
gvanrossum commentedon Aug 18, 2016
This is one more use case for structural typing, but in the meantime I would do something ad-hoc like you describe rather than make yet another custom ABC (those are hard to introduce and hard to get rid of).
I actually expect that the only real use cases are sockets and ints -- by the time someone has an IOBase or IO[...] instance they are probably talking about a disk file, which selectors don't support. Also most subclasses of IOBase do some form of buffering which makes using a selector a no-no. Calling fileno() isn't really a hardship, and callers that don't have any other reference to the object wouldn't know what to do when they got called back.
Regarding IOBase vs. IO[...], that area is a bit of a mess, but basically the concrete implementation classes don't really have a proper subtyping relationship, so for the type system we are probably wise to ignore them.
Michael0x2a commentedon Aug 19, 2016
Thanks for the clarifications!
Eric-Arellano commentedon Jun 13, 2019
Could this be revisited now that protocols are supported in MyPy and Python 3.8? Make the type be
Union[int, SupportsFileNo], whereSupportsFileNois a custom protocol that checks for the functionfileno().I'd be happy to submit a patch if you all would like.
srittau commentedon Jun 13, 2019
Please go ahead. This sounds like the right solution.
Fix `Union[..., NoneType]` injection by `get_type_hints` if a `None` …