Skip to content

How do I type a "file object"? #482

@Michael0x2a

Description

@Michael0x2a

I'm currently trying to add types for the selectors module, and am running into some difficulty figuring out the correct way to type a "file object".

The documentation for BaseSelector states that...

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?

from socket import socket
from io import IOBase
from typing import AnyStr, IO, Union

FileObj = Union[int, socket, IOBase, IO[AnyStr]]

(Oddly, io.IOBase and typing.IO also seem to be two distinct classes despite them being fairly similar -- I'm not sure if that was intentional or not)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions