-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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 test coverage #56637
Comments
The test coverage for functools was down around ~60%, this is a patch to bring that up to ~98%. Made two changes to the Lib/functools.py file itself:
In the test_functools.py file:
|
Raymond, do we care whether or not the pure Python version of functools.partial supports inheritance and instance testing? The constructor is technically documented as returning a "partial object" rather than a simple staticmethod instance with additional attributes. My own preference leans towards keeping the closure based implementation due to its simplicity, which is what makes it particularly useful as a cross-check on the C implementation. |
We don't care. The docs make very few |
Cheers for the comments Eric. I've modified the patch accordingly. |
Brian's patch looks ok to me. There's a missing newline (or two) just before test_main() but that can easily be fixed on commit. |
Ezio and I made further minor comments that can be handled by the person doing the commit; I’d like to do it. |
Just noticed one minor nit with the patch: the pure Python version of functools.partial should support "func" as a keyword argument that is passed to the underlying object. The trick is to declare a positional only argument like this: def f(*args, **kwds):
first, *args = args
# etc... |
Also, the closure based implementation should be decorated with @staticmethod (see http://bugs.python.org/issue11704) and the tests updated accordingly. |
I've updated the patch to address the comments here and in the code review. I added more cross testing of the pure Python implementation of partial - as you pointed out inheritance wasn't supported so I changed from the simple closure to a class implementation. Instead of skipping repr tests for the pure Python implementation could we not just implement it? I did skip the pickle test for the Python implementation though. Nick, I wasn't sure how to decorate the partial object as a staticmethod so I don't think this patch addresses bpo-11704. Also I didn't understand why Lock was being imported from _thread instead of thread. Since coverage went to 100% and the tests continue to all pass when I changed this. |
Why does the pure Python version of partial have to be so complicated? Something else:
The module is named _thread in 3.x, so this shouldn't have been changed (but admittedly it's thread in 2.x). |
Thanks Antoine, the __class__ attribute wasn't useful, I've removed that. Overriding the __setattr__ and __delattr__ gives consistent behaviour with the both versions - allowing the unittest reuse. Also I've changed thread back to _thread. Isn't more compatibility between the Python and C implementations desired? Is it an aim to document more of the functionality? An earlier version of this patch had a closure implementation of partial; I'm happy to revert to that if simplicity is preferred to compatibility? Should the caching decorators be tested from multiple threads? |
Le mardi 24 juillet 2012 à 06:00 +0000, Brian Thorne a écrit :
IMHO, not when compatibility regards obscure details such as whether
Why not, if there's an easy way to do so. |
Back to a simpler closure implementation of partial and skip the repr test for python implementation. |
New changeset fcfaca024160 by Antoine Pitrou in branch 'default': |
Sorry for the delay. I have now committed the patch to 3.4 (default). Thank you! |
The following from the changeset left me with questions: -from _functools import partial, reduce
+try:
+ from _functools import reduce
+except ImportError:
+ pass
|
My mistake, the try block should have just covered the import of partial - that is after all the exceptional circumstance we can deal with by using the pure python implementation. Possibly reduce could be handled in a similar way with a fallback python implementation? Otherwise your suggestion of conditionally adding it to __all__ makes sense to me.
What are the main considerations to properly answer these last questions? Performance comparison between the implementations, maintainability? |
In the meantime I'd expect the import of _functools.reduce to not be wrapped in a try block. Does that have an impact on coverage?
Sorry, the first time through I missed the part of the patch that tries to import _functools.partial _after_ the pure Python version is defined. |
I tried to remove the try block, but when making the import |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: