8000 bpo-46752: Uniform TaskGroup.__repr__ by asvetlov · Pull Request #31409 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-46752: Uniform TaskGroup.__repr__ #31409

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

Merged
merged 3 commits into from
Feb 20, 2022
Merged
Changes from 1 commit
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
18 changes: 10 additions & 8 deletions Lib/asyncio/taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from . import exceptions
from . import tasks


class TaskGroup:

def __init__(self):
Expand All @@ -25,19 +26,20 @@ def __init__(self):
self._on_completed_fut = None

def __repr__(self):
msg = f'<TaskGroup'
info = []
if self._tasks:
msg += f' tasks:{len(self._tasks)}'
info.append(f'tasks:{len(self._tasks)}')
if self._unfinished_tasks:
msg += f' unfinished:{self._unfinished_tasks}'
info.append(f'unfinished:{self._unfinished_tasks}')
if self._errors:
msg += f' errors:{len(self._errors)}'
info.append(f'errors:{len(self._errors)}')
if self._aborting:
msg += ' cancelling'
info.append('cancelling')
elif self._entered:
msg += ' entered'
msg += '>'
return msg
info.append('entered')

info_str = ' ' + ' '.join(info)
return f'<TaskGroup{info_str}>'
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
info_str = ' ' + ' '.join(info)
return f'<TaskGroup{info_str}>'
info_str = ' '.join(info)
return f'<TaskGroup {info_str}>'

Copy link
Contributor Author
@asvetlov asvetlov Feb 18, 2022

Choose a reason for hiding this comment

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

After applying the suggestion, a repr for created TaskGroup before __enter__() is called will be "<TaskGroup >" (note the space after class name).
Is it ok? Should we care about this rare case?

Copy link
Member

Choose a reason for hiding this comment

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

IMO accumulating strings in [] isn't a great pattern unless you have to use it for performance reasons. /my2c.

Copy link
Member

Choose a reason for hiding this comment

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

After applying the suggestion, a repr for created TaskGroup before __enter__() is called will be "<TaskGroup >" (note the space after class name). Is it ok? Should we care about this rare case?

The original code has the same issue AFAICT. If you don't want that you could initialize info = [''] perhaps?

Copy link
Member

Choose a reason for hiding this comment

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

IMO accumulating strings in [] isn't a great pattern unless you have to use it for performance reasons. /my2c.

Meh. IIRC the info pattern is used all over asyncio (e.g. _task_repr_info in base_tasks.py).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

info = [''] is clever, applied.

Yes, info list is used by other asyncio repr's. I'd like to have the similar approach across the library.


async def __aenter__(self):
if self._entered:
Expand Down
0