VirusTotal / vt-py

The official Python 3 client library for VirusTotal
https://virustotal.github.io/vt-py/
Apache License 2.0
531 stars 121 forks source link

Fix 'coroutine' object has no attribute 'stats' error in analysis results for upload_files.py #157

Closed shen-qin closed 1 year ago

shen-qin commented 1 year ago

I encountered AttributeError: 'coroutine' object has no attribute 'stats' when I was using upload_files.py. This is how I fixed the issue.

Original Code:

async def process_analysis_results(apikey, analysis, file_path):
  async with vt.Client(apikey) as client:
    completed_analysis = await client.wait_for_analysis_completion(analysis)
    print(f'{file_path}: {completed_analysis.stats}')

Modified Code:

async def process_analysis_results(apikey, analysis, file_path):
  async with vt.Client(apikey) as client:
    completed_analysis = await client.wait_for_analysis_completion(analysis)
    analysis_result = await completed_analysis
    print(f'{file_path}: {analysis_result.stats}')

Explanation:

In the original code, the stats attribute is directly accessed from the coroutine object completed_analysis. However, since the completed_analysis is a coroutine, it needs to be awaited to retrieve the result.

Therefore I made the above modifications and this is my rationale:

  1. By awaiting completed_analysis, we obtain the actual result of the analysis.
  2. We store the result in the variable analysis_result before accessing the stats attribute of analysis_result

This modification ensures that you correctly retrieve and access the analysis result before accessing its attributes. This change should resolve the AttributeError: 'coroutine' object has no attribute 'stats' and produce the desired output.

google-cla[bot] commented 1 year ago

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

mgmacias95 commented 1 year ago

Hello @shen-qin,

Thank you for your contribution, I think the problem here is in the wait_for_analysis_completion function that this script is using: https://github.com/VirusTotal/vt-py/blob/d44c39a4814154e7bb774486454467c8b9db69e7/vt/client.py#L752-L761

There should only be one wait_for_analysis_completion function, but even if we use the two, the second one should be something like this:

  async def wait_for_analysis_completion(self, analysis):
    return await self._wait_for_analysis_completion(analysis)

Can you modify your PR to fix this? Thank you!

Regards, Marta

shen-qin commented 1 year ago

Hello @mgmacias95,

Thank you for pointing that out!

I have made the relevant modifications :)

Regards, Shen Qin