python / python/mypy

Read/write property does not work with "allow_interpreted_subclasses"

Open
#15,828 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

Code generated by mypyc crashes at run-time when accessing a read/write property of an interpreted subclass.

To Reproduce

The mypyc code:

#entity.py
from mypy_extensions import mypyc_attr


@mypyc_attr(allow_interpreted_subclasses=True)
class EntityBase:

    def __init__(self) -> None:
        self._name = 'hello'

    @property
    def name(self) -> str:
        return self._name

    @name.setter
    def name(self, name: str):
        self._name = name


def print_name_property(entity: EntityBase):
    print(f'Property {type(entity)}: {entity.name}')

The driver code:

from reproducer import print_name_property, EntityBase


class EntitySubclass(EntityBase):

    def __init__(self):
        super().__init__()


def main():
    entity_subclass = EntitySubclass()

    print_name_property(entity_subclass)


if __name__ == '__main__':
    main()

Expected Behavior

The code should print 'hello' regardless if the attribute is accessed directly or via property.

Actual Behavior

Program crashes, when calling print_name_property(entity_subclass):

Traceback (most recent call last):
  File "/home/python/mypyc/reproducer.py", line 17, in <module>
    main()
  File "/home/python/mypyc/reproducer.py", line 13, in main
    print_name_property(entity_subclass)
  File "entity.py", line 20, in print_name_property
    print(f'Property {type(entity)}: {entity.name}')

AttributeError: 'EntitySubclass' object has no attribute '__mypyc_setter__name'

Process finished with exit code 1

Your Environment

  • Mypy version used: 1.4.1
  • Python version used: 3.11.0

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 with the mypyc-generated handling of properties on classes marked with allow_interpreted_subclasses, using the EntityBase and EntitySubclass reproducer in the issue. Run the example with the reported environment and verify that accessing entity.name no longer raises AttributeError and prints the expected hello output.

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
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.