Skip to content

attempt to improve sched.scheduler.enterabs/sched.scheduler.enter #14273

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

Closed
wants to merge 1 commit into from

Conversation

cj81499
Copy link
Contributor

@cj81499 cj81499 commented Jun 12, 2025

These changes are wrong and should not be merged. I'm hoping there might be a way to improve the type hints here (because I often get this wrong). Unfortunately, I'm not aware of one :(

stdlib/sched.pyi Outdated
Comment on lines 39 to 40
def enterabs(self, time: float, priority: object, action: Callable[PS, R], *args: PS.args, **kwargs: PS.kwargs) -> Event: ...
def enter(self, delay: float, priority: object, action: Callable[PS, R], *args: PS.args, **kwargs: PS.kwargs) -> Event: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These changes are wrong. I'd like to be able to say something like the following, but (afaik) Python's type system doesn't support it1 :(

Suggested change
def enterabs(self, time: float, priority: object, action: Callable[PS, R], *args: PS.args, **kwargs: PS.kwargs) -> Event: ...
def enter(self, delay: float, priority: object, action: Callable[PS, R], *args: PS.args, **kwargs: PS.kwargs) -> Event: ...
def enterabs(self, time: float, priority: Any, action: Callable[PS, R], argument: PS.args, kwargs: PS.kwargs) -> Event: ...
def enter(self, delay: float, priority: Any, action: Callable[PS, R], argument: PS.args, kwargs: PS.kwargs) -> Event: ...

Footnotes

  1. or at least, type checkers do not support it

@cj81499 cj81499 changed the title improve sched.scheduler.enterabs/improve sched.scheduler.enter types (by lying a little bit) attempt to improve sched.scheduler.enterabs/sched.scheduler.enter Jun 12, 2025

This comment has been minimized.

stdlib/sched.pyi Outdated
Comment on lines 14 to 15
class _MyCallable(Generic[Unpack[_Args], V, R]):
def __call__(self, args: tuple[Unpack[_Args]], kwargs: Mapping[str, V]) -> R: ...
Copy link
Contributor Author

@cj81499 cj81499 Jun 12, 2025

Choose a reason for hiding this comment

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

my idea here is essentially to reinvent the wheel of Callable so that we can preserve some info about both positional and keyword argument types. The type system isn't perfect here, since there (afiak) no info to preserve perfect info about positional only args, positional/kw args, and kw-only args other than ParamSpec, which isn't usable in this context (b/c enter takes argument/kwargs, not *args/**kwargs).

I expect that V will be pretty poor at preserving this info for Callables with many different kw arg types, but... maybe this is something?

@cj81499 cj81499 force-pushed the improve-sched-enter branch from a4fc670 to 52e4d8b Compare June 12, 2025 22:07

This comment has been minimized.

@cj81499 cj81499 force-pushed the improve-sched-enter branch from 52e4d8b to 8583f7a Compare June 12, 2025 23:10
@cj81499
Copy link
Contributor Author

cj81499 commented Jun 12, 2025

Nevermind.

I tried this out using mypy with --custom-typeshed-dir and it doesn't work :(

Even after fiddling with it a bit, the best I could do was:

demo.py:17: error: Argument 3 to "enter" of "scheduler" has incompatible type "Callable[[], None]"; expected "_MyCallable[*tuple[Never, ...], Never, object]"  [arg-type]

@cj81499 cj81499 closed this Jun 12, 2025
Copy link
Contributor

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

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.

1 participant