Skip to content

Commit

Permalink
Added support for typing.IO, TextIO, BinaryIO
Browse files Browse the repository at this point in the history
  • Loading branch information
erezsh committed Mar 9, 2024
1 parent 86014eb commit 9cc39a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions runtype/pytypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Python Types - contains an implementation of a Runtype type system that is parallel to the Python type system.
"""

import io
import typing as t
import contextvars
import types
Expand Down Expand Up @@ -565,6 +566,12 @@ def _to_canon(self, t):
return MutableSequence
elif t is typing.Callable:
return Callable
elif t is typing.IO:
return PythonDataType(io.IOBase)
elif t is typing.TextIO:
return PythonDataType(io.TextIOBase)
elif t is typing.BinaryIO:
return PythonDataType(io.BytesIO)

if origin is None:
if isinstance(t, typing.TypeVar):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from unittest import TestCase
import typing
import collections.abc as cabc
import io

from runtype.base_types import DataType, GenericType, PhantomType, Variance
from runtype.pytypes import type_caster, List, Dict, Int, Any, Constraint, String, Tuple, Iter, Literal, NoneType, Sequence, Mapping
Expand Down Expand Up @@ -295,6 +296,20 @@ class _Str(str):
assert make_type(typing.Callable[[str, int], _Str]) <= repeat
assert repeat <= make_type(typing.Callable[[_Str, int], str])

def test_io(self):
IO = make_type(io.IOBase)
TextIO = make_type(io.TextIOBase)
assert IO <= IO
assert TextIO <= IO
assert IO.test_instance(sys.stdout)

def test_typing_io(self):
IO = make_type(typing.IO)
TextIO = make_type(typing.TextIO)
assert IO <= IO
assert TextIO <= IO
assert IO.test_instance(sys.stdout)



if __name__ == '__main__':
Expand Down

0 comments on commit 9cc39a9

Please sign in to comment.