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

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

Open
nstarman opened this issue Jul 24, 2022 · 6 comments
Open
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-mypyc mypyc bugs

Comments

@nstarman
Copy link

nstarman commented Jul 24, 2022

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
@nstarman nstarman added the bug mypy got something wrong label Jul 24, 2022
@AlexWaygood AlexWaygood added topic-mypyc mypyc bugs topic-descriptors Properties, class vs. instance attributes labels Jul 24, 2022
@nstarman nstarman changed the title [mypyc] property setter and deleter don't work? [mypyc] property setter and deleter don't work with weakref Aug 1, 2022
@nstarman
Copy link
Author

nstarman commented Aug 1, 2022

@AlexWaygood, I narrowed the source of the problem. It isn't setters and deleters by themselves, but in conjunction with (at least) weakref (and maybe other types).

@nstarman
Copy link
Author

nstarman commented Aug 1, 2022

Just tested on

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


T = TypeVar("T")
O = TypeVar("O", covariant=True)


class Thing(Protocol[O]):
    def __call__(self) -> O: ...


class Bar(Generic[T]):
    _attr: Thing[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.deleter  # compiles if deleter (and what it decorates) is commented out
    def attr(self) -> None:
        self._attr = None

This raises the same exception, so I suspect the problem is actually with setting parameterized attributes?

@nstarman nstarman changed the title [mypyc] property setter and deleter don't work with weakref [mypyc] property setter and deleter don't work with parameterized attributes Aug 1, 2022
@AlexWaygood
Copy link
Member

I'm afraid I'm only a triager here, and mypyc is one of the areas of mypy I know least about, so I'm probably not the person you want to be tagging here, I'm afraid :)

@nstarman
Copy link
Author

nstarman commented Aug 1, 2022

Whom should I tag?

@AlexWaygood
Copy link
Member

97littleleaf11, ichard26 or JukkaL might be interested. (ichard26 isn't a core dev, but he's made several mypyc contributions and uses mypyc for compiling black.)

But we're all volunteers with limited free time :)

@nstarman
Copy link
Author

nstarman commented Aug 2, 2022

Thanks @AlexWaygood.

@JukkaL, I think I've found an issue with how mypyc processes parametrized attributes, at least in the context of decorated methods. I've been trying to understand the error trace to submit a PR resolving this issue, but I don't yet grok Mypy(s)'s internals. Hopefully this and #13304 are easily resolved issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-mypyc mypyc bugs
Projects
None yet
Development

No branches or pull requests

2 participants