-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Closed
Description
werkzeug.wsgi.wrap_file() and FileWrapper require the file argument to have type BinaryIO. Generally, the use of IO classes is discouraged, especially as arguments, since they are normally much too strict and incompatible with each other. In my particular use case, I got an error message 'Argument 2 to "wrap_file" has incompatible type "IO[bytes]"; expected "BinaryIO"'.
As FileWrapper only requires the read() method to be present (although it uses more methods if they exist), the old typeshed stubs annotated the file arguments with SupportsRead[bytes], which is available in _typeshed during type checking:
if t.TYPE_CHECKING:
from _typeshed import SupportsReadAlternatively a similar protocol can be defined like this:
_T_co = t.TypeVar("_T_co", covariant=True)
class SupportsRead(t.Protocol[_T_co]):
def read(self, __length: int = ...) -> _T_co: ...Reactions are currently unavailable