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: Globals declared in function scope have wrong __qualname__
Type: behavior Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: alexandre.vassalotti Nosy List: Arfrever, alexandre.vassalotti, benjamin.peterson, pitrou, python-dev
Priority: high Keywords:

Created on 2013-10-19 17:56 by alexandre.vassalotti, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (9)
msg200453 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2013-10-19 17:56
The value of __qualname__ for globals declared in function scope doesn't seems right to me:

>>> def f():
...    global C
...    class C: pass
...    return C.__qualname__
...
>>> f()
'f.<locals>.C'

It would be much more useful to return 'C' in this case. In my case, fixing this would help me write cleaner unit tests for pickle, since it would allow me to keep test classes with their test code.
msg200460 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-10-19 18:09
That's interesting. I suppose it requires some tweaking in the compiler to get right.
msg200464 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-10-19 18:15
I suppose it would also be desirable to have

def f():
    C = None
    def g():
        nonlocal C
        class C: pass
    return g()

have a different qualname, too?
msg200468 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2013-10-19 18:19
Supporting nonlocal variables would be nice. However, unless it is easy to implement, I don't think it is worth the effort since these variables can't be accessed outside the enclosing function's scope.
msg200489 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-19 20:06
New changeset 35b384ed594b by Benjamin Peterson in branch 'default':
give explicitly global functions and classes a global __qualname__ (closes #19301)
http://hg.python.org/cpython/rev/35b384ed594b
msg200490 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-10-19 20:07
There you go. I could do nonlocal, but that's a more work for no real benefit besides consistency.
msg200631 - (view) Author: Alexandre Vassalotti (alexandre.vassalotti) * (Python committer) Date: 2013-10-20 20:51
It seems that we are missing support for nested globals:

>>> def f():
...   global C
...   class C:
...     class D:
...       pass
...
>>> f()
>>> C.D.__qualname__
'f.<locals>.C.D'
msg200632 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2013-10-20 20:54
Oh, phoey.
msg200644 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2013-10-20 21:51
New changeset bb2affc1e317 by Benjamin Peterson in branch 'default':
cleanup the construction of __qualname__ (closes #19301 again)
http://hg.python.org/cpython/rev/bb2affc1e317
History
Date User Action Args
2022-04-11 14:57:52adminsetgithub: 63500
2013-10-20 21:51:00python-devsetstatus: open -> closed

messages: + msg200644
2013-10-20 20:54:38benjamin.petersonsetmessages: + msg200632
2013-10-20 20:51:07alexandre.vassalottisetstatus: closed -> open

messages: + msg200631
2013-10-19 20:07:24benjamin.petersonsetmessages: + msg200490
2013-10-19 20:06:17python-devsetstatus: open -> closed

nosy: + python-dev
messages: + msg200489

resolution: fixed
stage: needs patch -> resolved
2013-10-19 18:19:58alexandre.vassalottisetmessages: + msg200468
2013-10-19 18:15:39benjamin.petersonsetmessages: + msg200464
2013-10-19 18:09:29pitrousetnosy: + benjamin.peterson
messages: + msg200460
2013-10-19 18:05:37Arfreversetnosy: + Arfrever
2013-10-19 17:56:32alexandre.vassalotticreate