Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
daitakahashi mannequin opened this issue Sep 24, 2021 · 3 comments
Closed

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

daitakahashi mannequin opened this issue Sep 24, 2021 · 3 comments
Labels
3.8 only security fixes 3.9 only security fixes topic-typing type-bug An unexpected behavior, bug, or error

Comments

@daitakahashi
Copy link
Mannequin

daitakahashi mannequin commented Sep 24, 2021

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']

    @daitakahashi
    Copy link
    Mannequin Author

    daitakahashi mannequin commented Sep 24, 2021

    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.

    @daitakahashi daitakahashi mannequin added 3.8 only security fixes 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Sep 24, 2021
    @AlexWaygood
    Copy link
    Member

    Related: https://bugs.python.org/issue44904

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @AlexWaygood
    Copy link
    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
    Labels
    3.8 only security fixes 3.9 only security fixes topic-typing type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant