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

time: Make _ClockInfo concrete and simplify platform checks #13413

Closed
wants to merge 1 commit into from

Conversation

hamdanal
Copy link
Contributor

No description provided.

Comment on lines 78 to +87
def time() -> float: ...
def time_ns() -> int: ...
def monotonic() -> float: ...
def monotonic_ns() -> int: ...
def perf_counter() -> float: ...
def perf_counter_ns() -> int: ...
def process_time() -> float: ...
def process_time_ns() -> int: ...
def thread_time() -> float: ...
def thread_time_ns() -> int: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also put the seconds and ns clock functions together

@hamdanal hamdanal changed the title time: Make _ClockInfo concrete andsimplify platform checks time: Make _ClockInfo concrete and simplify platform checks Jan 19, 2025
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Comment on lines -83 to +89
class _ClockInfo(Protocol):
class _ClockInfo(SimpleNamespace):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this change isn't entirely accurate though, right? At runtime it actually is an instance of SimpleNamespace, not an instance of a subclass of SimpleNamespace:

>>> import time
>>> i = time.get_clock_info("monotonic")
>>> i
namespace(implementation='mach_absolute_time()', monotonic=True, adjustable=False, resolution=4.166666666666666e-08)
>>> type(i)
<class 'types.SimpleNamespace'>

so the fully accurate type hint for get_clock_info would be

def get_clock_info(name: Literal["monotonic", "perf_counter", "process_time", "time", "thread_time"], /) -> SimpleNamespace: ...

but that would suck for users, because any attribute access on a SimpleNamespace attribute is inferred as Any by type checkers.

Could you say a little bit about what problem this change solves? I think type checkers should be able to infer that the object returned by get_clock_info cannot have its __class__ set to _ClockInfo exactly -- since _ClockInfo is an abstract class, it must be an instance of a concrete class that satisfies the _ClockInfo interface that is returned, rather than an instance of _ClockInfo exactly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, this change has been sitting in my notes for a very long time I forgot what I was doing with this function at the time. Please feel free to close the PR if you think the change is not correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, no worries. I'm happy to consider a change along these lines if there's a concrete problem it's solving, but to me it looks like it makes the signature slightly less accurate, so I'm inclined to close it if not :-)

@hamdanal hamdanal deleted the time branch January 19, 2025 18:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants