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

RecursionError when using property() inside classes #90932

Closed
cheny0y0 mannequin opened this issue Feb 17, 2022 · 3 comments
Closed

RecursionError when using property() inside classes #90932

cheny0y0 mannequin opened this issue Feb 17, 2022 · 3 comments
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@cheny0y0
Copy link
Mannequin

cheny0y0 mannequin commented Feb 17, 2022

BPO 46776
Nosy @stevendaprano, @cheny0y0

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2022-02-17.13:13:30.966>
created_at = <Date 2022-02-17.12:48:24.227>
labels = ['interpreter-core', 'type-bug', '3.8', '3.9', '3.10', '3.11', '3.7', 'invalid']
title = 'RecursionError when using property() inside classes'
updated_at = <Date 2022-02-17.13:18:51.286>
user = 'https://github.com/cheny0y0'

bugs.python.org fields:

activity = <Date 2022-02-17.13:18:51.286>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2022-02-17.13:13:30.966>
closer = 'steven.daprano'
components = ['Interpreter Core']
creation = <Date 2022-02-17.12:48:24.227>
creator = 'prasechen'
dependencies = []
files = []
hgrepos = []
issue_num = 46776
keywords = []
message_count = 3.0
messages = ['413403', '413406', '413407']
nosy_count = 2.0
nosy_names = ['steven.daprano', 'prasechen']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue46776'
versions = ['Python 3.7', 'Python 3.8', 'Python 3.9', 'Python 3.10', 'Python 3.11']

@cheny0y0
Copy link
Mannequin Author

cheny0y0 mannequin commented Feb 17, 2022

A simple class definition:
class Foo: bar = property(lambda self: self.bar)
And get the value of Foo.bar, it returns correctly, <property object at 0x**********>.
And get the value of Foo().bar, it raises RecursionError:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 1, in <lambda>
  File "<stdin>", line 1, in <lambda>
  File "<stdin>", line 1, in <lambda>
  [Previous line repeated 996 more times]
RecursionError: maximum recursion depth exceeded

@cheny0y0 cheny0y0 mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Feb 17, 2022
@stevendaprano
Copy link
Member

This is not a bug, Python is working correctly here. The interpreter is doing exactly what you told it to do, and then raising a RecursionError before you can crash the system.

You have a property instance.bar which returns instance.bar, which returns instance.bar, which returns instance.bar... forever, or until RecursionError is triggered.

Your class is the same as:

class Foo:
    @property
    def bar(self):
        return self.bar

So when you call instance.bar, it looks up bar, which is the property, which looks up bar, which is the property, which looks up bar, and so on.

What did you expect to happen?

@stevendaprano
Copy link
Member

Maybe you expected to do this:

class C:
    def __init__(self):
        self._value = 999

    @property
    def bar(self):
        return self._value

obj = C()
obj.bar  # returns 999

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant