classification
Title: __qualname__ and __slots__
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: 23722 Superseder:
Assigned To: xiang.zhang Nosy List: Arfrever, ncoghlan, serhiy.storchaka, xiang.zhang, yselivanov
Priority: normal Keywords: patch

Created on 2015-05-29 20:09 by yselivanov, last changed 2017-03-24 22:42 by xiang.zhang. This issue is now closed.

Files
File name Uploaded Description Edit
slots_qualname.patch xiang.zhang, 2016-11-10 07:03 review
slots_qualname_v2.patch xiang.zhang, 2016-11-10 08:34 review
slots_special.patch xiang.zhang, 2016-11-10 13:48 review
slots_special_v2.patch xiang.zhang, 2016-11-10 14:24 review
slots_special_v3.patch xiang.zhang, 2016-11-11 13:18 review
slots_special-v4.patch xiang.zhang, 2016-12-05 09:08 review
Pull Requests
URL Status Linked Edit
PR 495 merged xiang.zhang, 2017-03-06 02:42
Messages (16)
msg244410 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2015-05-29 20:09
The following code doesn't work.  Would be great if we can fix it in 3.5

class Foo:
    __slots__ = ('__qualname__',)
msg280416 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-11-09 17:03
Serhiy, maybe you know how to fix this?
msg280418 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-09 17:19
Why should it work Yury?

__qualname__ and __doc__(if exists) are inserted into the dict when creating a class.

>>> class Foo:
...     """bar"""
...     __slots__ = ('__doc__',)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: '__doc__' in __slots__ conflicts with class variable
msg280419 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2016-11-09 17:21
Because the following works without a problem:

  class F:
    __slots__ = ('__name__', )

  f = F()
  f.__name__ = 'aaaa'

This bug with __qualname__ is why _GeneratorWrapper in types.py doesn't have __slots__.
msg280491 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-10 08:05
What about other names set when creating a class? __module__, __class__, __classcell__?
msg280492 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-10 08:11
I think of them but currently they don't pose a problem for practical codes like __qualname__. Maybe leaving them until there comes a real need?
msg280505 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-10 13:48
> What about other names set when creating a class? __module__, __class__, __classcell__?

__module__ remains in the class dict so I think it's a class variable.
Will __class__ be set? It's inserted into the function scope.
__classcell__ I think has the same situation with __qualname__.

New patch also adds __classcell__.
msg280507 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-10 14:24
slots_special_v2 fixes a bug in the previous patch.
msg280510 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-11-10 14:43
__qualname__ doesn't makes much sense if it doesn't match __module__. If _GeneratorWrapper patches __qualname__, I think it should patch __module__ too.
msg280580 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-11-11 13:18
v3 updates the test cases.
msg282327 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-04 10:25
Nick, could you please look at the patch? Especially at the part about __classcell__.
msg282332 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2016-12-04 12:00
"__classcell__" in __slots__ doesn't really make sense, as unlike __qualname__ (which turns into a real class level attribute and can be useful to override on instances), it's only a transient entry in a class definition namespace - types.__new__ pops it automatically so it doesn't get turned into an attribute.

However, consistency is a good thing, so allowing it seems reasonable.

Regarding the specifics, the pending patch on issue 23722 makes it a TypeError to ever set __classcell__ to anything other than a real cell object.
msg282401 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2016-12-05 09:08
> However, consistency is a good thing, so allowing it seems reasonable.

The inconsistency also appears when "__classcell__" in slots with or without super().

v4 catches up with HEAD.
msg288944 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2017-03-03 23:11
Xiang, can you make a PR?
msg289062 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-03-06 02:43
PR made. :-)
msg290274 - (view) Author: Xiang Zhang (xiang.zhang) * (Python committer) Date: 2017-03-24 22:42
New changeset c393ee858932f79bd6dabf31550f9a53ea90bc68 by Xiang Zhang in branch 'master':
bpo-24329: allow __qualname__ and __classcell__ in __slots__ (GH-495)
https://github.com/python/cpython/commit/c393ee858932f79bd6dabf31550f9a53ea90bc68
History
Date User Action Args
2017-03-24 22:42:54xiang.zhangsetmessages: + msg290274
2017-03-08 03:19:59xiang.zhangsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2017-03-06 06:01:51serhiy.storchakasetassignee: serhiy.storchaka -> xiang.zhang
2017-03-06 02:43:42xiang.zhangsetmessages: + msg289062
2017-03-06 02:42:57xiang.zhangsetpull_requests: + pull_request405
2017-03-03 23:12:12yselivanovsetversions: - Python 3.5, Python 3.6
2017-03-03 23:11:51yselivanovsetmessages: + msg288944
2016-12-05 09:08:41xiang.zhangsetfiles: + slots_special-v4.patch

messages: + msg282401
2016-12-04 12:04:05serhiy.storchakasetdependencies: + During metaclass.__init__, super() of the constructed class does not work
2016-12-04 12:00:57ncoghlansetmessages: + msg282332
2016-12-04 10:25:25serhiy.storchakasetnosy: + ncoghlan
messages: + msg282327
2016-11-15 14:08:08serhiy.storchakasetassignee: serhiy.storchaka
versions: + Python 3.5
2016-11-11 13:18:42xiang.zhangsetfiles: + slots_special_v3.patch

messages: + msg280580
2016-11-10 14:43:18serhiy.storchakasetmessages: + msg280510
2016-11-10 14:32:06pitrousetnosy: - pitrou
2016-11-10 14:24:07xiang.zhangsetfiles: + slots_special_v2.patch

messages: + msg280507
2016-11-10 13:48:03xiang.zhangsetfiles: + slots_special.patch

messages: + msg280505
2016-11-10 08:34:33xiang.zhangsetfiles: + slots_qualname_v2.patch
2016-11-10 08:11:35xiang.zhangsetmessages: + msg280492
2016-11-10 08:05:38serhiy.storchakasetmessages: + msg280491
2016-11-10 07:03:50xiang.zhangsetfiles: + slots_qualname.patch
keywords: + patch
type: behavior
stage: patch review
2016-11-09 17:21:29yselivanovsetmessages: + msg280419
2016-11-09 17:19:06xiang.zhangsetnosy: + xiang.zhang
messages: + msg280418
2016-11-09 17:03:04yselivanovsetnosy: + serhiy.storchaka

messages: + msg280416
versions: + Python 3.7, - Python 3.5
2015-07-21 07:10:25ethan.furmansetnosy: - ethan.furman
2015-06-01 18:41:32Arfreversetnosy: + Arfrever
2015-05-29 20:50:30ethan.furmansetnosy: + ethan.furman
2015-05-29 20:09:17yselivanovcreate