python / python/mypy

type-checking of __delete__ in descriptor protocol is insufficient

Open
#15,000 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug topic-descriptors
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report

I have a descriptor which prevents del t.a by changing the "instance" type on delete. pyright fails on this correctly, but mypy does not.

To Reproduce

from typing import Literal, NewType, TypeVar


NotSupported = NewType("NotSupported", bool)


class Prop:
    """This is a descriptor class."""
    def __get__(self, o: object, otype: object) -> str:
        return "get-result"
    
    def __delete__(self, o: NotSupported) -> None:
        raise NotImplementedError("Don't do this")

class A:
    p = Prop()

a = A()
del a.p

Expected Behavior

I expect mypy to report an error at the line del a.p. pyright reports the error

Cannot delete member "p" for type "A"PylancereportGeneralTypeIssues

Actual Behavior

No error is reported.

Your Environment

  • Mypy version used: 1.1.1
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
    show_error_codes = True
    check_untyped_defs = True
    strict_optional = True
    no_implicit_optional = False
    warn_redundant_casts = True
    
  • Python version used: 3.11

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running mypy 1.1.1 against the descriptor reproducer in the issue, especially the __delete__ method and del a.p. Trace the descriptor type-checking path and make the check reject the deletion as shown; done means mypy reports an error at del a.p without breaking valid descriptor deletion.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.