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

functools.partial objects have no __qualname__ attribute #78656

Closed
cjerdonek opened this issue Aug 23, 2018 · 12 comments
Closed

functools.partial objects have no __qualname__ attribute #78656

cjerdonek opened this issue Aug 23, 2018 · 12 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@cjerdonek
Copy link
Member

BPO 34475
Nosy @vstinner, @cjerdonek, @serhiy-storchaka, @hongweipeng, @tirkarthi

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-10-18.21:24:00.758>
created_at = <Date 2018-08-23.14:25:59.091>
labels = ['type-feature', 'library']
title = 'functools.partial objects have no __qualname__ attribute'
updated_at = <Date 2018-10-18.21:29:12.799>
user = 'https://github.com/cjerdonek'

bugs.python.org fields:

activity = <Date 2018-10-18.21:29:12.799>
actor = 'chris.jerdonek'
assignee = 'none'
closed = True
closed_date = <Date 2018-10-18.21:24:00.758>
closer = 'serhiy.storchaka'
components = ['Library (Lib)']
creation = <Date 2018-08-23.14:25:59.091>
creator = 'chris.jerdonek'
dependencies = []
files = []
hgrepos = []
issue_num = 34475
keywords = []
message_count = 11.0
messages = ['323947', '324912', '324913', '324914', '327858', '327862', '327868', '327957', '328002', '328006', '328008']
nosy_count = 5.0
nosy_names = ['vstinner', 'chris.jerdonek', 'serhiy.storchaka', 'hongweipeng', 'xtreak']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue34475'
versions = ['Python 3.6']

@cjerdonek
Copy link
Member Author

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__'

@cjerdonek cjerdonek added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Aug 23, 2018
@hongweipeng
Copy link
Mannequin

hongweipeng mannequin commented Sep 10, 2018

the functools.partial returns an instance not fun or cls.using p.func.__qualname__ may be you want.

@cjerdonek
Copy link
Member Author

Using p.func would name the function passed to functools.partial() rather than the partial object itself.

@tirkarthi
Copy link
Member

It seems __repr__ call to partial object has qualname but I think it always returns "partial". Ref :

qualname = type(self).__qualname__

>>> 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

@hongweipeng
Copy link
Mannequin

hongweipeng mannequin commented Oct 17, 2018

partial() return an instance not class or function. Why it need __qualname__ attribute?

Ref: https://www.python.org/dev/peps/pep-3155/

@serhiy-storchaka
Copy link
Member

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.

@cjerdonek
Copy link
Member Author

Okay, I thought a partial object was supposed to "look" like a function.

I'm okay with closing this.

@serhiy-storchaka
Copy link
Member

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.

@cjerdonek
Copy link
Member Author

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.

@vstinner
Copy link
Member

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.

@cjerdonek
Copy link
Member Author

Sorry, I'm out of practice. I thought I closed this when I marked it rejected.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@JensHeinrich
Copy link

Is this still the right approach?
after all even lambda expressions get a __qualname__ now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants