[issue44618] inspect.signature does not work for datetime classes
Mauricio Villegas added the comment: I am not sure if this affects all built-in classes, assuming that by built-in it means that `SOMEOBJECT.__class__.__module__ == 'builtins'`. For example I have C++ library that is compiled into a python module using swig. It is available as a docker image `docker pull mauvilsa/pagexml:runtime-ubuntu20.04-py38`. For a class in that module it can be observed: >>> import inspect >>> import pagexml >>> pagexml.swigPageXML.PageXML.__class__.__module__ 'builtins' >>> print(inspect.signature(pagexml.swigPageXML.PageXML.__init__)) (self, pagexml_path=None, schema_path=None) -- ___ Python tracker <https://bugs.python.org/issue44618> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44618] inspect.signature does not work for datetime classes
Mauricio Villegas added the comment: Also happens in python 3.10. === Python 3.10.0b4 === (*args, **kwargs) (self, /, *args, **kwargs) Traceback (most recent call last): ... ValueError: no signature found for builtin type -- versions: +Python 3.10 ___ Python tracker <https://bugs.python.org/issue44618> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44618] inspect.signature does not work for datetime classes
Mauricio Villegas added the comment: > Doesn’t it do that with any built in function? Sorry. I did not include what is the behavior for other classes. An example could be calendar.Calendar. In this case the signature gives: >>> from calendar import Calendar >>> import inspect >>> print(inspect.signature(Calendar.__init__)) (self, firstweekday=0) >>> print(inspect.signature(Calendar)) (firstweekday=0) Note that the signature gives firstweekday which is the only init parameter. Calendar is not implemented with __new__ so getting the signature would be for object, that does not include named parameters. It also works fine when you implement a class with __new__. For example: >>> class MyClass: def __new__(cls, paramA, paramB=1): obj = object.__new__(cls) obj.paramA = paramA obj.paramB = paramB return obj >>> print(inspect.signature(MyClass.__new__)) (cls, paramA, paramB=1) >>> print(inspect.signature(MyClass)) (paramA, paramB=1) I do think it is a bug specific to the datetime classes. -- ___ Python tracker <https://bugs.python.org/issue44618> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor
Mauricio Villegas added the comment: I created another issue since the problem appears to be a bit different: https://bugs.python.org/issue44618 -- ___ Python tracker <https://bugs.python.org/issue40897> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44618] inspect.signature does not work for datetime classes
Change by Mauricio Villegas : -- components: +Library (Lib) type: -> behavior ___ Python tracker <https://bugs.python.org/issue44618> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue44618] inspect.signature does not work for datetime classes
New submission from Mauricio Villegas : Classes in the datetime module are implemented using __new__ with some named parameters. I want to be able to inspect their signature to know which are the names of the parameters it accepts like it works for most classes. However, this does not work for classes in the datetime module. I already mentioned this in https://bugs.python.org/issue40897 but now I am thinking this should be a separate issue so I am creating this one. An example is the class timedelta. It has as parameters days, seconds, microseconds, milliseconds, minutes, hours and weeks. If I run the following script trying different python versions for py in 36 37 38 39; do source py${py}/bin/activate echo "=== $(python3 --version) ===" python3 -c " from datetime import timedelta import inspect print(inspect.signature(timedelta.__new__)) print(inspect.signature(timedelta.__init__)) inspect.signature(timedelta) " deactivate done What I get is === Python 3.6.9 === (*args, **kwargs) (self, /, *args, **kwargs) Traceback (most recent call last): ... ValueError: no signature found for builtin type === Python 3.7.11 === (*args, **kwargs) (self, /, *args, **kwargs) Traceback (most recent call last): ... ValueError: no signature found for builtin type === Python 3.8.11 === (*args, **kwargs) (self, /, *args, **kwargs) Traceback (most recent call last): ... ValueError: no signature found for builtin type === Python 3.9.6 === (*args, **kwargs) (self, /, *args, **kwargs) Traceback (most recent call last): ... ValueError: no signature found for builtin type -- messages: 397387 nosy: mauvilsa priority: normal severity: normal status: open title: inspect.signature does not work for datetime classes versions: Python 3.6, Python 3.7, Python 3.8, Python 3.9 ___ Python tracker <https://bugs.python.org/issue44618> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue40897] Inheriting from class that defines __new__ causes inspect.signature to always return (*args, **kwargs) for constructor
Mauricio Villegas added the comment: I think this is affecting me or it is a new similar issue. And it is not only for python 3.9, but also from 3.6 up. I am working on making code configurable based on signatures (see https://jsonargparse.readthedocs.io/en/stable/#classes-methods-and-functions). Now we need this to work for datetime.timedelta which defines parameters in __new__ instead of __init__. The following happens: >>> from datetime import timedelta >>> import inspect >>> inspect.signature(timedelta.__new__) >>> inspect.signature(timedelta.__init__) >>> inspect.signature(timedelta) ... ValueError: no signature found for builtin type I am expecting to get parameters for days, seconds, microseconds, milliseconds, minutes, hours and weeks, see https://github.com/python/cpython/blob/bfe544d2f2c2e7a7c03a764bed3276a1e27a0f5c/Lib/datetime.py#L461-L462. Hopefully this gives some insight into what should be done. Independent from the fix, I would like to know if currently there is any way I can get the signature for __new__ that I could use until there is a proper fix for it. -- nosy: +mauvilsa ___ Python tracker <https://bugs.python.org/issue40897> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue35005] argparse should accept json and yaml argument types
Mauricio Villegas added the comment: FYI there is a new python package that extends argparse with the enhancements proposed here and more. https://pypi.org/project/jsonargparse/ -- nosy: +mauvilsa ___ Python tracker <https://bugs.python.org/issue35005> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com