Skip to content

Regression: descriptors don't work on protocols #19262

Closed as duplicate of#19054
Closed as duplicate of#19054
@eltoder

Description

@eltoder

Descriptors don't work when used as attributes on Protocols. The same code works on a regular class. This worked for many years up to and including mypy 1.15, but stopped working in 1.16.

To Reproduce

https://mypy-play.net/?mypy=1.16.0&python=3.11&gist=e1f1bd3254690db213102de71f894854

from typing import Protocol, assert_type

class Desc:
    def __get__(self, obj, owner=None) -> int:
        return 1

class HasDesc(Protocol):
    a: Desc

obj: HasDesc
assert_type(obj.a, int)

Expected Behavior

This code should type-check. If works with mypy 1.15 or when HasDesc is not a protocol.

Actual Behavior

main.py:11: error: Expression is of type "Desc", not "int"  [assert-type]

Your Environment

  • Mypy version used: 1.16.0
  • Mypy command-line flags:
  • Python version used: 3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions