擅长:python、mysql、java
<p>改用<code>io.StringIO</code></p>
<pre class="lang-py prettyprint-override"><code>import io
import sys
import typing
class MyIO(io.StringIO):
def write(self, text: str):
pass
def hello(f: typing.TextIO):
f.write("hello")
hello(sys.stdout) # type checks
hello(open("temp.txt", "w")) # type checks
hello(MyIO()) # type checks
</code></pre>