quora / pyanalyze

A Python type checker
Apache License 2.0
333 stars 36 forks source link

invalid_context_manager raised on open() calls in a context manager #722

Closed raghavj98 closed 6 months ago

raghavj98 commented 6 months ago
with open("test.txt", "r") as fp:
    lines = fp.readline()

Simple example above should be enough to recreate the issue. Since open() calls return a IO[Any], which have __enter__ and __exit__ defined, this should be considered a valid context manager. Here is the complete warning for reference.

_io.TextIOWrapper is not a context manager (code: invalid_context_manager)
  _io.TextIOWrapper has no attribute '__callable_proto_members_only__'

In test.py at line 1
   1: with open("test.txt", "r") as fp:
           ^
   2:     lines = fp.readline()
JelleZijlstra commented 6 months ago

Thanks for the report! Are you on the latest release? I believe I already fixed this; it's related to relatively recent changes in CPython.

raghavj98 commented 6 months ago

Thanks for the report! Are you on the latest release? I believe I already fixed this; it's related to relatively recent changes in CPython.

Thanks for the prompt response!

The issue is indeed fixed on 0.11.0. I was on 0.9.0 earlier.