'Python, `pathlib.Path.open()` method and built-in function `open()` don't return instances of `typing.IO`

I've read some other answers, and they seem contradictory to what happens IRL.

Try the following:

from typing import IO
from pathlib import Path

example_path = Path(r"D:\Example.txt")
with example_path.open("r") as f:
    print(isinstance(f, IO))
with open(example_path) as f:
    print(isinstance(f, IO))

It will print:

False
False

That's baffling, because according to documentation, it seems to me that one of the very purposes of typing.IO is for type-checking an object for IO type, and this would include the use of isinstance(). Why doesn't it work? Doesn't that defeat the very purpose? What am I missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source