New submission from Daisuke Takahashi <[email protected]>:
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 <[email protected]>
<https://bugs.python.org/issue45282>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com