python / python/mypy

[mypyc] property setter and deleter don't work with parameterized attributes

Open
#13,231 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

mypyc fails to compile a class with a property setter and deleter and a weakref attribute.

To Reproduce

  1. Defined (in a file foo.py) a class Bar with a property that has a setter and deleter and weakref attribute.
  2. run mypyc foo.py3.

e.g.

from __future__ import annotations
from typing import TypeVar, Generic
import weakref

T = TypeVar("T")

class Bar(Generic[T]):
    _attr: weakref.ReferenceType[T] | None
    def __init__(self) -> None:
        self._attr = None
    @property
    def attr(self) -> T | None:
        return self._attr() if self._attr is not None else None
    @attr.setter
    def attr(self, value: T) -> None:
        self._attr = weakref.ref(value)
    @attr.deleter
    def attr(self) -> None:
        self._attr = None

Actual Behavior

I get the following error:

File "mypyc/irbuild/classdef.py", line 87, in transform_class_def
File "mypyc/irbuild/classdef.py", line 200, in add_method
File "mypyc/irbuild/function.py", line 369, in handle_ext_method
File "mypyc/irbuild/function.py", line 312, in gen_func_item
File "mypyc/irbuild/function.py", line 351, in gen_func_ir
...: KeyError: <mypy.nodes.FuncDef object at 0x7f93d4876b80>

This error does not occur when setter and deleter are omitted.

Your Environment

  • Mypy version used: 0.950
  • Mypy command-line flags: N/A
  • Mypy configuration options from mypy.ini (and other config files): N/A
  • Python version used: 3.9
  • Operating system and version: macOS Monterey, version 12.4

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

Reproduce the failure with the foo.py example, then trace the reported path through mypyc/irbuild/classdef.py and mypyc/irbuild/function.py. Add coverage for a parameterized attribute with property setter and deleter, and consider the issue done when mypyc foo.py compiles it without the KeyError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.