You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
inspect.signature does not work for datetime classes #88784
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=Noneclosed_at=Nonecreated_at=<Date2021-07-13.07:41:18.210>labels= ['type-bug', '3.8', '3.9', '3.10', '3.7', 'library']
title='inspect.signature does not work for datetime classes'updated_at=<Date2021-07-17.09:51:41.720>user='https://github.com/mauvilsa'
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
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:
You're right, it seems specific to built-in *classes* like those in datetime. We get the same with inspect.signature(int), since int is also a built-in class.
I don't know who maintains inspect, but it's not me. :-(
mauvilsa commented on Jul 17, 2021
mauvilsamannequin
MannequinAuthor
More actions
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 <class 'datetime.timedelta'>
Since this is a problem with how the C extension is configured, maybe we could make some workaround where it calls the python implementation signature? Or encode it by hand?
#136573 fixes this issue, most cases. The only cases without signature/with unsupported signature are:
The tzinfo constructor. This is an abstract class, you should not create instances of it. There is a difference between Python and C implementations. Python implementation does not accept any argument, C implementation accepts any arguments.
The timezone constructor. Python implementation takes optional name argument with default value None. In C implementation, passing None and not passing anything give different results, so None cannot be used as the default value. help() shows it as timezone(offset, name=<unrepresentable>).
All replace() methods. They are the same as __replace__() methods, so we can give them the same signature, __replace__(self, /, **changes), but it does not show which keywords are accepted. So, help() shows the datetime.replace() signature as replace(self, /, year=unchanged, month=unchanged, day=unchanged, unchanged, minute=unchanged, second=unchanged, microsecond=unchanged, tzinfo=unchanged, *, fold=unchanged), which is not supported in inspect.signature().
To support this cases, we need a general solution in inspect.signature() (which would fix many other cases) and somehow resolve the differences between Python and C implementations. These are different issues.
Activity
mauvilsa commentedon Jul 13, 2021
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
What I get is
gvanrossum commentedon Jul 14, 2021
Doesn’t it do that with any built in function? Try Len or open, for example. I think that’s a feature, at least not a bug specific to date time.
mauvilsa commentedon Jul 14, 2021
Sorry. I did not include what is the behavior for other classes. An example could be calendar.Calendar. In this case the signature gives:
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:
I do think it is a bug specific to the datetime classes.
gvanrossum commentedon Jul 14, 2021
You're right, it seems specific to built-in *classes* like those in datetime. We get the same with inspect.signature(int), since int is also a built-in class.
I don't know who maintains inspect, but it's not me. :-(
mauvilsa commentedon Jul 17, 2021
Also happens in python 3.10.
=== Python 3.10.0b4 ===
6 remaining items
StanFromIreland commentedon Mar 12, 2025
Since this is a problem with how the C extension is configured, maybe we could make some workaround where it calls the python implementation signature? Or encode it by hand?
serhiy-storchaka commentedon Aug 9, 2025
#136573 fixes this issue, most cases. The only cases without signature/with unsupported signature are:
tzinfoconstructor. This is an abstract class, you should not create instances of it. There is a difference between Python and C implementations. Python implementation does not accept any argument, C implementation accepts any arguments.timezoneconstructor. Python implementation takes optionalnameargument with default value None. In C implementation, passing None and not passing anything give different results, so None cannot be used as the default value.help()shows it astimezone(offset, name=<unrepresentable>).replace()methods. They are the same as__replace__()methods, so we can give them the same signature,__replace__(self, /, **changes), but it does not show which keywords are accepted. So,help()shows thedatetime.replace()signature asreplace(self, /, year=unchanged, month=unchanged, day=unchanged, unchanged, minute=unchanged, second=unchanged, microsecond=unchanged, tzinfo=unchanged, *, fold=unchanged), which is not supported ininspect.signature().To support this cases, we need a general solution in
inspect.signature()(which would fix many other cases) and somehow resolve the differences between Python and C implementations. These are different issues.