This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Inability to specific __qualname__ as a property on a class instance.
Type: enhancement Stage: needs patch
Components: Interpreter Core Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: LewisGaul, grahamd, iritkatriel, pitrou, vstinner
Priority: normal Keywords:

Created on 2013-09-22 13:37 by grahamd, last changed 2022-04-11 14:57 by admin.

Messages (5)
msg198275 - (view) Author: Graham Dumpleton (grahamd) Date: 2013-09-22 13:37
Python 3 introduced __qualname__. This attribute exists on class types and also instances of certain class types, such as functions. For example:

def f(): pass

print(f.__name__)
print(f.__qualname__)

class Class: pass

print(Class.__name__)
print(Class.__qualname__)

yields:

f
f
Class
Class

An instance of a class however does not have __name__ or __qualname__ attributes. With:

c = Class()

print(c.__name__)
print(c.__qualname__)

yielding:

Traceback (most recent call last):
  File "qualnametest.py", line 13, in <module>
    print(c.__name__)
AttributeError: 'Class' object has no attribute '__name__'

Traceback (most recent call last):
  File "qualnametest.py", line 14, in <module>
    print(c.__qualname__)
AttributeError: 'Class' object has no attribute '__qualname__'

For a class, it is possible to override the __name__ attribute using a property.

class Class:
    @property
    def __name__(self):
        return 'override'

c = Class()

print(c.__name__)

With the result being:

override

This is useful in writing object proxies or function wrappers for decorators as rather than having to copy the __name__ attribute into the wrapper, the lookup can be deferred until when it is required.

The same though cannot be done for __qualname__. With:

class Class:
    @property
    def __qualname__(self):
        return 'override'

yielding an error when the class definition is being processed:

Traceback (most recent call last):
  File "qualnametest.py", line 16, in <module>
    class Class:
TypeError: type __qualname__ must be a str, not property

This means the same trick cannot be used in object proxies and function wrappers and instead __qualname__ must be copied and assigned explicitly as a string attribute in the __init__() function of the object proxy or function wrapper.

I can sort of understand a prohibition on __qualname__ being a string attribute in certain cases, especially if overriding it on a type or instance where __qualname__ attribute already exists, but I don't understand why a limitation would be imposed to prevent using a property as a means of generating the value for a class instance which doesn't otherwise have a __qualname__ attribute. There is no similar restriction for __name__.

Unless there is a good specific reason for this behaviour, the ability to override it with a property in cases where the __qualname__ attribute didn't already exist, would be handy for proxies and wrappers.
msg198276 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-22 13:39
I guess this would be a harmless improvement in any case.
msg387431 - (view) Author: Lewis Gaul (LewisGaul) * Date: 2021-02-21 00:04
This would also be useful for me - I just hit this same problem. If someone could give some guidance on how to make this change I'd be happy to put up a PR.
msg409960 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-07 12:04
This is the check that is preventing qualname being a property:

https://github.com/python/cpython/blob/994f90c0772612780361e1dc5fa5223dce22f70a/Objects/typeobject.c#L2853

Note that it is not the same check used for assignment, which is here:

https://github.com/python/cpython/blob/994f90c0772612780361e1dc5fa5223dce22f70a/Objects/typeobject.c#L559


I think some unit tests are missing for properties, they would need to be added.
msg411687 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-25 23:29
See also Issue41294.
History
Date User Action Args
2022-04-11 14:57:51adminsetgithub: 63273
2022-01-25 23:29:35iritkatrielsetmessages: + msg411687
2022-01-07 12:04:01iritkatrielsetnosy: + iritkatriel, vstinner

messages: + msg409960
versions: + Python 3.11, - Python 3.10
2021-02-21 00:04:59LewisGaulsetnosy: + LewisGaul
messages: + msg387431
2021-02-15 20:09:43iritkatrielsetversions: + Python 3.10, - Python 3.4
2013-09-22 13:39:20pitrousetversions: + Python 3.4, - Python 3.3
nosy: + pitrou

messages: + msg198276

type: behavior -> enhancement
stage: needs patch
2013-09-22 13:37:01grahamdcreate