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

Add __name__ to property #101860

Closed
eltoder opened this issue Feb 13, 2023 · 5 comments
Closed

Add __name__ to property #101860

eltoder opened this issue Feb 13, 2023 · 5 comments
Labels
3.13 bugs and security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement

Comments

@eltoder
Copy link
Contributor

eltoder commented Feb 13, 2023

class C:
    @property
    def foo(self):
        return 1

assert C.foo.__name__ == "foo"

It would be very handy if this worked, so properties can be introspected the same way as functions, classmethods, etc. Name can be simply taken from fget the same way this is done for __isabstractmethod__.

Linked PRs

@eltoder eltoder added the type-feature A feature request or enhancement label Feb 13, 2023
@arhadthedev arhadthedev added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Feb 13, 2023
@sobolevn
Copy link
Member

What about this case?

>>> def getter(o):
...     return 42
... 
>>> class Some:
...    number = property(getter)
... 
>>> Some().number
42

Should it be 'getter'?

@TeamSpen210
Copy link

Actually, property already defines __set_name__(), storing the name internally for use in formatting AttributeErrors. That's called during class creation, so it'll be accurate regardless where the callable comes from. All that's needed is for that to be exposed. This was added in #71981, but it was kept private to avoid expanding the API since a need wasn't specified.

@eltoder
Copy link
Contributor Author

eltoder commented Feb 13, 2023

@TeamSpen210 Nice find! I think the API expansion comments were about adding name as an argument to property constructor. Adding __name__ as a read-only attribute for introspection seems very much in the spirit of python.

eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 13, 2023
Useful for introspection and consistent with functions and other
descriptors.
eltoder added a commit to eltoder/cpython that referenced this issue Feb 17, 2024
Useful for introspection and consistent with functions and other
descriptors.
serhiy-storchaka pushed a commit that referenced this issue Feb 20, 2024
Useful for introspection and consistent with functions and other
descriptors.
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
Useful for introspection and consistent with functions and other
descriptors.
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
Useful for introspection and consistent with functions and other
descriptors.
@JelleZijlstra
Copy link
Member

This is a change to a builtin and likely has an impact on some introspection tools, so it should be mentioned in the What's New for 3.13.

@JelleZijlstra JelleZijlstra reopened this May 24, 2024
@JelleZijlstra JelleZijlstra added deferred-blocker docs Documentation in the Doc dir 3.13 bugs and security fixes labels May 24, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Aug 28, 2024
(cherry picked from commit 40fff90)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
JelleZijlstra pushed a commit that referenced this issue Aug 29, 2024
(cherry picked from commit 40fff90)

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@picnixz
Copy link
Contributor

picnixz commented Aug 30, 2024

Closing since the feature is now documented in 3.13 and 3.14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes docs Documentation in the Doc dir interpreter-core (Objects, Python, Grammar, and Parser dirs) type-feature A feature request or enhancement
Projects
Development

No branches or pull requests

7 participants