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

linter complaint about using a worker as a timer callback #3434

Closed
guystreeter opened this issue Sep 30, 2023 · 3 comments · Fixed by #3499
Closed

linter complaint about using a worker as a timer callback #3434

guystreeter opened this issue Sep 30, 2023 · 3 comments · Fixed by #3499
Assignees
Labels
enhancement New feature or request Task

Comments

@guystreeter
Copy link

Have you checked closed issues? https://github.com/Textualize/textual/issues?q=is%3Aissue+is%3Aclosed

yes

Please give a brief but clear explanation of the issue. If you can, include a complete working example that demonstrates the bug. Check it can run without modifications.

from textual import work
from textual.app import App
class testy(App):
    @work
    async def worky(self) -> None:
        pass
    def compose(self):
        self.set_timer(1, self.worky)
$ pyright test.py
/home/guy/gits/ttuna/test.py
  /home/guy/gits/ttuna/test.py:8:27 - error: Argument of type "() -> Worker[None]" cannot be assigned to parameter "callback" of type "TimerCallback | None" in function "set_timer"
    Type "() -> Worker[None]" cannot be assigned to type "TimerCallback | None"
      Type "() -> Worker[None]" cannot be assigned to type "() -> Awaitable[None]"
        Function return type "Worker[None]" is incompatible with type "Awaitable[None]"
          "Worker[None]" is incompatible with protocol "Awaitable[None]"
            "__await__" is not present
      Type "() -> Worker[None]" cannot be assigned to type "() -> None"
        Function return type "Worker[None]" is incompatible with type "None"
          Type cannot be assigned to type "None"
    ... (reportGeneralTypeIssues)
1 error, 0 warnings, 0 informations 

The same error is reported for set_interval as well

@Textualize Textualize deleted a comment from github-actions bot Sep 30, 2023
@willmcgugan
Copy link
Collaborator

The callback in set_timer is typed as returning None, but your worky method returns a Worker instance.

That might be too strict. We could maybe have it typed as returning Any.

In the meantime, set_timer(1, lambda: self.worky()) may quell the typing error.

@guystreeter
Copy link
Author

Thanks, the linter is apparently too smart to be fooled by lambda. Passing it through an intermediate function works, though:

    @work
    async def worky(self) -> None:
        pass
    def do_worky(self) -> None:
        self.worky()
    def compose(self):
        self.set_timer(1, self.do_worky())

produces no complaint.

@github-actions
Copy link

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request Task
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants