Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dashscope/audio/asr/recognition.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def __receive_worker(self):
responses = self.__launch_request()
for part in responses:
if part.status_code == HTTPStatus.OK:
if len(part.output) == 0:
if len(part.output) == 0 or ('finished' in part.output and part.output['finished'] == True):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

The current logic uses or which might lead to unexpected behavior. If len(part.output) == 0 is false but the second condition 'finished' in part.output and part.output['finished'] == True is true, the code will still proceed as if the condition is met. This could potentially cause issues if the intention is to only complete when both conditions are true. It is recommended to use and to ensure both conditions are met before proceeding.

Consider changing or to and to ensure that the complete timestamp is only set when both the output is empty and the 'finished' flag is present and true.

Suggested change
if len(part.output) == 0 or ('finished' in part.output and part.output['finished'] == True):
if len(part.output) == 0 and ('finished' in part.output and part.output['finished'] == True):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The condition to check for completion can be simplified to be more concise and idiomatic Python.

  • Checking for an empty dictionary can be done with not part.output.
  • Checking for the presence and truthiness of the 'finished' key can be done with part.output.get('finished'). This avoids verbose key existence checks.
  • Comparing a boolean value with == True is generally discouraged.1

This makes the code more readable and maintainable.

Suggested change
if len(part.output) == 0 or ('finished' in part.output and part.output['finished'] == True):
if not part.output or part.output.get('finished'):

Style Guide References

Footnotes

  1. PEP 8 advises against comparing boolean values to True or False using ==.

self._on_complete_timestamp = time.time() * 1000
logger.debug('last package delay {}'.format(
self.get_last_package_delay()))
Expand Down