Am 22.04.21 um 10:42 schrieb Chris Angelico:
File-like objects are used VERY frequently in the stdlib, and actual open file objects have quite a large interface. The use-case is a pretty real one: "if I were to create a simulant file object to pass to json.load(), what methods do I need?".
Maybe in some cases, the "smaller protocols" option is practical, but it would need to have a useful name. For instance, if it needs to be readable, iterable, closeable, and autocloseable via __enter__/__exit__, that's ... uhh.... a readable, iterable, closeable context manager? Not an improvement over "file-like object".
Experience from typeshed shows that many functions in the stdlib and third-party libraries only use one or very few methods, very often just read() or write(). From a practical standpoint, small protocols seem quite feasible. These are quite an improvement over "file-like" objects, where no one knows what that actually means. - Sebastian