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: functools.partial objects have no __qualname__ attribute
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.6
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: chris.jerdonek, hongweipeng, serhiy.storchaka, vstinner, xtreak
Priority: normal Keywords:

Created on 2018-08-23 14:25 by chris.jerdonek, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (11)
msg323947 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2018-08-23 14:25
functools.partial objects have no __qualname__ attribute. This means, for example, that code expecting a callable that logs the __qualname__ attribute can break when passed a functools.partial object.

Example:

>>> import functools
>>> int.__qualname__
'int'
>>> p = functools.partial(int)
>>> p.__qualname__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'functools.partial' object has no attribute '__qualname__'
msg324912 - (view) Author: hongweipeng (hongweipeng) * Date: 2018-09-10 04:06
the functools.partial returns an instance not fun or cls.using `p.func.__qualname__` may be you want.
msg324913 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2018-09-10 04:48
Using p.func would name the function passed to functools.partial() rather than the partial object itself.
msg324914 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-09-10 06:17
It seems __repr__ call to partial object has qualname but I think it always returns "partial". Ref : https://github.com/python/cpython/blob/0afada163c7ef25c3a9d46ed445481fb69f2ecaf/Lib/functools.py#L276

>>> import functools
>>> int.__qualname__
'int'
>>> p = functools.partial(int)
>>> p.__qualname__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'functools.partial' object has no attribute '__qualname__'
>>> p
functools.partial(<class 'int'>)


Thanks
msg327858 - (view) Author: hongweipeng (hongweipeng) * Date: 2018-10-17 04:45
partial() return an instance not class or function. Why it need __qualname__ attribute?

Ref: https://www.python.org/dev/peps/pep-3155/
msg327862 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-17 05:40
I don't see a problem. Not all callables have the __qualname__ attribute. It is not the part of the protocol. The code that expects the __qualname__ attribute should be fixed.
msg327868 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2018-10-17 07:24
Okay, I thought a partial object was supposed to "look" like a function.

I'm okay with closing this.
msg327957 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2018-10-18 10:10
partial objects lack many other function attributes: __name__, __module__ (and __qualname__ doesn't make sense without __module__),  __annotations__, __get__(), etc. It would be nice to make these types more similar, but attributes shouldn't lie. And I am not sure what partial.__qualname__ can be. It shouldn't be the __qualname__ of the wrapped function, since the partial object differs from it, and is not accessible by same name.
msg328002 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2018-10-18 20:04
> It shouldn't be the __qualname__ of the wrapped function

Yes, I agree with you. I was thinking it should be similar to what it would be for a function defined at the same location.
msg328006 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2018-10-18 21:21
functools.partial objects have no __qualname__ attribute, but they don't have a __name__ attribute neither.

$ python3
Python 3.6.6 (default, Jul 19 2018, 14:25:17) 
>>> import functools
>>> func=int
>>> p=functools.partial(func)
>>> p.__name__
AttributeError: 'functools.partial' object has no attribute '__name__'
>>> p.__qualname__
AttributeError: 'functools.partial' object has no attribute '__qualname__'
>>> repr(p)
"functools.partial(<class 'int'>)"

If you want to "inherit" the name of the "wrapped function", you may use: functools.update_wrapper().

I'm not sure that it's correct to inherit the name by default. functools.partial() creates a new function, so if it has a name, for me, it should be different.

I agree to close the issue, it's not a bug.
msg328008 - (view) Author: Chris Jerdonek (chris.jerdonek) * (Python committer) Date: 2018-10-18 21:29
Sorry, I'm out of practice. I thought I closed this when I marked it rejected.
History
Date User Action Args
2022-04-11 14:59:05adminsetgithub: 78656
2018-10-18 21:29:12chris.jerdoneksetmessages: + msg328008
2018-10-18 21:24:00serhiy.storchakasetstatus: open -> closed
stage: resolved
2018-10-18 21:21:51vstinnersetnosy: + vstinner
messages: + msg328006
2018-10-18 20:04:02chris.jerdoneksetmessages: + msg328002
2018-10-18 10:10:27serhiy.storchakasetmessages: + msg327957
2018-10-17 07:24:07chris.jerdoneksetresolution: rejected
messages: + msg327868
2018-10-17 05:40:01serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg327862
2018-10-17 04:45:47hongweipengsetmessages: + msg327858
2018-09-10 06:17:01xtreaksetmessages: + msg324914
2018-09-10 06:09:56xtreaksetnosy: + xtreak
2018-09-10 04:48:47chris.jerdoneksetmessages: + msg324913
2018-09-10 04:06:37hongweipengsetnosy: + hongweipeng
messages: + msg324912
2018-08-23 14:25:59chris.jerdonekcreate