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

Add configuration to report any usage of an unawaited coroutine #3563

Closed
maddalax opened this issue Jun 9, 2022 · 3 comments
Closed

Add configuration to report any usage of an unawaited coroutine #3563

maddalax opened this issue Jun 9, 2022 · 3 comments
Labels
enhancement request New feature or request

Comments

@maddalax
Copy link

maddalax commented Jun 9, 2022

Is your feature request related to a problem? Please describe.
I've been following #1317, but I'm hoping it would be possible to extend this to support warning/error if you have any unawaited coroutines.

Screen Shot 2022-06-09 at 10 50 56 AM

The issue being:

  • the first response.json() call is being flagged as expected
  • the _logger.debug(response.json()) is not flagged.

I understand we cannot make the assumption that everyone's workflow requires coroutines to be awaited / it's entirely fine to pass coroutines as a parameter, I'm more just asking if this could be something that's configurable.

@maddalax maddalax added the enhancement request New feature or request label Jun 9, 2022
@erictraut
Copy link
Collaborator

This would require introducing a new diagnostic rule because reportUnusedCoroutine would generate false positives if it included this. We do occasionally introduce new diagnostic rules, but we do so only if we have strong signal that it is generally useful. The rule you're proposing would need to be off by default, even in strict mode. That means that it wouldn't be discoverable and very few people (probably just yourself and a handful of others who happened to stumble upon it) would ever use it. So I don't think this meets our bar for adding a new diagnostic rule, at least not at this time.

I'm going to close this for now, but we might reconsider in the future if we get signal from enough pyright users that they want this functionality.

@maddalax
Copy link
Author

maddalax commented Jun 9, 2022

Understandable. We are converting a sync codebase to an async one, which I would imagine is somewhat of a fairly common use case as async/await becomes more mainstream in Python. But i'll wait and see if anyone else has this concern. Thanks

@nrbnlulu
Copy link

nrbnlulu commented Sep 26, 2024

@erictraut said in: #9086 (comment)

because it would lead to false positive errors in legitimate Python code

for a reference here are some linters that support this:

If you can think of a way to make this check safer — such that it's very unlikely to produce false positive errors, then it would be easier to justify adding such a feature.

  1. if you pass a coro to a function that accepts a coro thats prob not a bug
async with asyncio.TaskGroup() as tg:
	sleep_for = asyncio.sleep(1)
	tg.create_task(sleep_for)

I'm not sure if pyright can deduce that though...

  1. if you use it in an if statement
    function returns Coro | T thats not a bug
class Scrapper:
	def get_current_task(self) -> Coroutine | None:
		...
	
	async def ensure_running(self) -> None:
		if not self.get_current_task():  #  ✅
			await self.start()

function returns Coro[T] thats prob a bug

class Scrapper:
	async def get_current_task(self) -> Coroutine | None:
		...
	
	async def ensure_running(self) -> None:
		if not self.get_current_task():  #  🚨 you should have awaited it
			await self.start()

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

No branches or pull requests

3 participants