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

Add the default attributes to functools.partial #102323

Closed
JensHeinrich opened this issue Feb 28, 2023 · 4 comments
Closed

Add the default attributes to functools.partial #102323

JensHeinrich opened this issue Feb 28, 2023 · 4 comments
Labels
type-feature A feature request or enhancement

Comments

@JensHeinrich
Copy link

JensHeinrich commented Feb 28, 2023

Feature or enhancement

Add __qualname__ and __name__ to functools.partial

Pitch

Even an anonymous lambda function gets a __qualname__ or rather __name__ of <lambda> and partial should behave in the same way.

Previous discussion

This has been previously discussed in #78656, but as lambda-functions are named since before ccd1921 363ac7d a similiar behavior of using <partial> as the name should have been suggested in the last discussion already.

@JensHeinrich JensHeinrich added the type-feature A feature request or enhancement label Feb 28, 2023
@sobolevn
Copy link
Member

sobolevn commented Feb 28, 2023

Why do you need this? There are other callable objects that can miss __qualname__:

>>> class MyCallable:
...     def __call__(self, *args, **kwargs):
...         print('whatever')
... 
>>> MyCallable()()
whatever
>>> MyCallable().__qualname__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'MyCallable' object has no attribute '__qualname__'

What is so special about partial?

Isn't getattr(obj, '__qualname__', '<default name>') enough?

@JensHeinrich
Copy link
Author

That's a valid workaround for the usecase that prompted this ticket, but I still feel it wouldn't hurt to just use <partial> as a __name__ attribute

JensHeinrich pushed a commit to JensHeinrich/pydantic that referenced this issue Mar 1, 2023
@sobolevn
Copy link
Member

sobolevn commented Mar 1, 2023

@JensHeinrich the problem with adding __name__ and __qualname__ attributes to partial is that it is not enough. There are also __annotations__ and __code__ attributes (and others) that people might assume all callables have. So, it is either full compatibility with the callable API or just __call__ as it is now.

I prefer the status quo :)
And based on previous descussion I will close this ticket.

I hope my explanation helped! Thanks for the report.

@sobolevn sobolevn closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2023
@JensHeinrich
Copy link
Author

I do get the reasoning, but I will mention the discussion in #91002 as I see those points are related. This hopefully helps future me or other people researching it.

dmontagu added a commit to pydantic/pydantic that referenced this issue Mar 8, 2023
* Use getattr and default for qualname

Was suggested in python/cpython#102323 by @sobolevn.
Fixes #5101
Superceedes #5102

* Import tests from #5102

* Fix signature generation for functools

* Use getattr and default for name

* Remove expectation of error from tests

* Reset tracked validators

* Describe changes

* Use getattr and default for module

* Use id as fallback

* Tweak name formatting

---------

Co-authored-by: JensHeinrich <github.com/JensHeinrich>
Co-authored-by: David Montague <35119617+dmontagu@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants