There is no "exactly" for file-likeness
The notion of an object being "file-like" is one of the most disappointing spots of Python today, in my view.
Remember Python 2?
The idea goes back to Python 2 (and probably Python 1) where duck-typing was the norm and type hints did not even exist.
In such a setting, it makes perfect sense if the meaning of "file-like" is strongly context-dependent:
All the caller needs is an object that will return some bytes data (or string data) upon a call to read()? Then any such object can be considered file-like for this particular caller.
Other callers will want a write() operation that somehow sensibly consumes the data they hand over to it.
Still others may want a seek() as well, and perhaps a read() in addition.
And so on. If these callers get an object with those methods, they are happy and do not care at all what the actual type of those objects is.
Today's Python 3 use ticks differently
Modern Python relies a lot more on knowing about types and so we have started to often declare which ones are used where. And rightly so, because the more you make use of third-party components, the more difficult it becomes to manage duck typing successfully. Type hints are concise and informative documentation in such a setting.
But now we have conflicting explanations of what "file-like" is: old ones that are minimal and talk about methods only, newer ones that make many more requirements and may even mention specific types.
Not nice. But hey: Python is more than 30 years old.
Given that, its number of warts is impressively low!
"file-like" is definitely one of them.
You can roll your own explicit definition of 'file-like'
The stdlib's typing module has the notion of
Protocol
by which you can define a type in (static) duck-typing fashion:
As a list of method signatures.
If you like, you can define a Protocol (or several) for your own
code's notion(s) of file-likeness.
The bare minimum, for an object supporting read() in text mode,
might look like so:
class FileLike(typing.Protocol):
def read(self) -> str:
...
You can then use it in type hints:
def process_file(input: FileLike):
...
json.loadnot to mention "file-like objects": "Deserialize fp (a .read()-supporting text file or binary file containing a JSON document) to a Python object using this conversion table." However, thejson.dumpdescription still mentions the concept. The documentation also hasn't used thesimplejsonname in approximately forever.