This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: isinstance(x, typing.Protocol-class) unexpectedly evaluates properties
Type: behavior Stage:
Components: Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, daitakahashi
Priority: normal Keywords:

Created on 2021-09-24 13:25 by daitakahashi, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
test_protocol_property.py daitakahashi, 2021-09-24 13:25 testcases
Messages (2)
msg402562 - (view) Author: Daisuke Takahashi (daitakahashi) Date: 2021-09-24 13:25
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.
msg402567 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2021-09-24 14:52
Related: https://bugs.python.org/issue44904
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89445
2021-09-24 14:52:18AlexWaygoodsetnosy: + AlexWaygood
messages: + msg402567
2021-09-24 13:25:14daitakahashicreate