Skip to content

isinstance(x, typing.Protocol-class) unexpectedly evaluates properties #89445

Description

@daitakahashi
BPO 45282
Nosy @AlexWaygood, @daitakahashi
Files
  • test_protocol_property.py: testcases
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2021-09-24.13:25:14.862>
    labels = ['type-bug', '3.8', '3.9']
    title = 'isinstance(x, typing.Protocol-class) unexpectedly evaluates properties'
    updated_at = <Date 2021-09-24.14:52:18.540>
    user = 'https://github.com/daitakahashi'

    bugs.python.org fields:

    activity = <Date 2021-09-24.14:52:18.540>
    actor = 'AlexWaygood'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = []
    creation = <Date 2021-09-24.13:25:14.862>
    creator = 'daitakahashi'
    dependencies = []
    files = ['50303']
    hgrepos = []
    issue_num = 45282
    keywords = []
    message_count = 2.0
    messages = ['402562', '402567']
    nosy_count = 2.0
    nosy_names = ['AlexWaygood', 'daitakahashi']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45282'
    versions = ['Python 3.8', 'Python 3.9']

    Activity

    1. daitakahashi commented on Sep 24, 2021

      daitakahashimannequin
      MannequinAuthor

      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.

    2. AlexWaygood commented on Sep 24, 2021

      @AlexWaygood
      Member
    3. transferred this issue fromon Apr 10, 2022
    4. AlexWaygood commented on Mar 5, 2023

      @AlexWaygood
      Member

      Duplicate of #102433, which is a newer issue but has more discussion

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions