Thanks for the engagement and contributions here!
(My apologies that I wasn’t able to respond as quickly as I would have liked.
)
Can you explain what’s wrong with writing […] out [`.total_seconds`]?
I wouldn’t call it “wrong”, but I would call it “less-nice-than” or “unergonomic”. The standard library affords a function to do something (well, in this case, nothing) for a duration. The standard library also affords a class that represents a duration. It makes perfects sense to want to pass an instance of the latter directly to the former. Looked at this way, the .total_seconds() remains not “wrong” but more of a “speed bump”, both in the writing and reading of code that has a timedelta and wants to use it with time.sleep.
What is wrong with [time.sleep(0.4)]?
I don’t want to have to find anything wrong with it, but if I really twist my mind around it, I could hand-wave in the direction of “what if some engineer works in Java for a few hours, where sleep takes milliseconds as a parameter, and then switches to Python for a task, and makes an off-by-a-multiplier-of-one-thousand error with zir argument to Python’s time.sleep?”. One remedy to this could be to use something like the argument comments that clang checks, so that the code always reads “this 0.4 is a measurement of seconds”. (As of the Python 3.13.7 that I have available to me, I do not seem able to pass time.sleep’s argument by the keyword “secs".)
… but I don’t find this possibility anywhere near strong enough to make the case for removing the existing use of scalar ints and floats; this proposal continues to make far more sense as a strictly-additive enhancement request leaving the present semantics also fully supported.
If this were accepted, I would even like to see a linter rule to
push making the call with the timedelta over a float.
This would make sense for the codebases in which I work, but I don’t see that it would work for all codebases everywhere, so I wouldn’t advocate for such a linter rule being on by default.
The time module is a step lower-level than datetime, it would not
be appropriate for time to depend on datetime as it would need to
in order to support passing a timedelta to sleep.
This is surprising for me to hear; as a long-time user of the standard library I don’t think I’ve ever come across any of its internal layers having these kinds of externally-visible consequences. This consideration could be kept internal to the standard library though, couldn’t it? This enhancement request could be implemented in such a way that the standard library would continue to show its “all at the same level of the abstraction hierarchy” that it does today, yes?
(Thanks again for the comments here!
)