[issue45282] isinstance(x, typing.Protocol-class) unexpectedly evaluates properties
Daisuke Takahashi
report at bugs.python.org
Fri Sep 24 09:25:15 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).
Expected behavior:
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.
----------
files: test_protocol_property.py
messages: 402562
nosy: daitakahashi
priority: normal
severity: normal
status: open
title: isinstance(x, typing.Protocol-class) unexpectedly evaluates properties
type: behavior
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>
<https://bugs.python.org/issue45282>
_______________________________________
More information about the Python-bugs-list
mailing list