[New-bugs-announce] [issue45282] isinstance(x, typing.Protocol-class) unexpectedly evaluates properties
report at bugs.python.org
Fri Sep 24 09:25:14 EDT 2021
New submission from Daisuke Takahashi <dtakahashi42 at gmail.com>:
Because __instancecheck__ of _ProtocolMeta uses hasattr() and getattr(), both of which evaluate property attributes, calling isinstance() with an object and a class that inherits typing.Protocol evaluates the input object's properties in some cases.
The attached testcases include three cases of checking subtype relationship of an instance (having a property "x" that may raise RuntimeError on evaluation) and following three protocol classes;
(1) a protocol class having "x" as a property,
(2) a protocol class having "x" as a data attribute, and
(3) a protocol class having "x" as a class property that raises
RuntimeError on evaluation (>= python 3.9 only).
1. The isinstance(obj, Protocol_class) does not evaluate anything but just checks existence of attribute names.
2. All cases in the attached testcases run without any error
Thank you very much in advance.
title: isinstance(x, typing.Protocol-class) unexpectedly evaluates properties
versions: Python 3.8, Python 3.9
Added file: https://bugs.python.org/file50303/test_protocol_property.py
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce