dynamic casting after a isinstance is not recognized by pytype
Imagine this:
# exception has type Exception
if isinstance(exception, grpc.RpcError):
code = exception.code() # pytype: disable=attribute-error
Without # pytype: disable=attribute-error, we get:
in _add_exception_to_log_entry: No attribute 'code' on Exception [attribute-error]
mypy and PyCharm work just fine in this case. I tried with 2021.09.09.
Any ideas? I tried to look for a similar issue but could not find, apologies if this is a duplicate.
Thanks for the report! Unfortunately, this isn't something pytype can do within its current framework for handling isinstance checks. (Basically, we would need grpc.RpcError to be among the list of possible types for exception before hitting the check, so that we can then tag it as the visible type.) Coincidentally, we've recently discussed improving how pytype handles conditional type information, but any changes on that front are at least months away. For now, you'll need to either keep disabling these errors use typing.cast to tell pytype the type.
@rchen152 thanks for the quick reply. Looking forward to the improvement in the future!