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

__str__ and __repr__ for asyncio.Task still omit arg values #83804

Closed
stuball123 mannequin opened this issue Feb 13, 2020 · 8 comments
Closed

__str__ and __repr__ for asyncio.Task still omit arg values #83804

stuball123 mannequin opened this issue Feb 13, 2020 · 8 comments
Labels
3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@stuball123
Copy link
Mannequin

stuball123 mannequin commented Feb 13, 2020

BPO 39623
Nosy @asvetlov, @1st1, @tirkarthi, @kmaork

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-02-20.23:52:20.937>
created_at = <Date 2020-02-13.08:19:35.113>
labels = ['type-bug', '3.9', 'expert-asyncio']
title = '__str__ and __repr__ for asyncio.Task still omit arg values'
updated_at = <Date 2020-02-20.23:52:20.936>
user = 'https://bugs.python.org/stuball123'

bugs.python.org fields:

activity = <Date 2020-02-20.23:52:20.936>
actor = 'yselivanov'
assignee = 'none'
closed = True
closed_date = <Date 2020-02-20.23:52:20.937>
closer = 'yselivanov'
components = ['asyncio']
creation = <Date 2020-02-13.08:19:35.113>
creator = 'stuball123'
dependencies = []
files = []
hgrepos = []
issue_num = 39623
keywords = []
message_count = 8.0
messages = ['361944', '361950', '362266', '362348', '362363', '362366', '362369', '362371']
nosy_count = 5.0
nosy_names = ['asvetlov', 'yselivanov', 'xtreak', 'kmaork', 'stuball123']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue39623'
versions = ['Python 3.9']

@stuball123
Copy link
Mannequin Author

stuball123 mannequin commented Feb 13, 2020

This is not very helpful if your gather or wait contains multiple versions of foo with different argument values:

<Task pending coro=<foo() running at c.py:6>

Should just be:

<Task pending coro=<foo(42, "hello") running at c.py:6>

Would probably take all of 5 minutes to implement and make a lot of people's lives easier.

@tirkarthi
Copy link
Member

This could sometimes make the output verbose when the arguments themselves are tasks while including arguments in the signature in _format_coroutine.

$ cat /tmp/foo.py
import asyncio
async def foo(a, b): pass

async def main():
    task = asyncio.create_task(foo(1, b=1))
    task1 = asyncio.create_task(foo(1, b=task))
    print(repr(task))
    print(repr(task1))

asyncio.run(main())
$ python3.8 /tmp/foo.py
<Task pending name='Task-2' coro=<foo() running at /tmp/foo.py:3>>
<Task pending name='Task-3' coro=<foo() running at /tmp/foo.py:3>>

$ ./python.exe /tmp/foo.py
<Task pending name='Task-2' coro=<foo(a=1, b=1) running at /tmp/foo.py:3>>
<Task pending name='Task-3' coro=<foo(a=1, b=<Task pending name='Task-2' coro=<foo(a=1, b=1) running at /tmp/foo.py:3>>) running at /tmp/foo.py:3>>

@tirkarthi tirkarthi added 3.9 only security fixes type-bug An unexpected behavior, bug, or error labels Feb 13, 2020
@kmaork
Copy link
Mannequin

kmaork mannequin commented Feb 19, 2020

This could sometimes make the output verbose

We could limit the length of the recursive __repr__ functions, and display partial reprs suffixed with ..., like in numpy for example.

We can define that the maximum wanted length of a task repr would be, say, 80 characters, get the reprs of all task arguments, and if the sum length is more than the maximum we can trim the longest ones and suffix with a '...'.

Maybe this restriction shouldn't be applied if python is built in debug mode.

@asvetlov
Copy link
Contributor

Not so easy to find a satisfactory generic approach.
An argument can also be 10 MiB length bytes array, a dictionary with 10,000 elements, HTML page, name it.
All these objects are printable but their representation is too verbose.
Task can have a dozen of arguments, only the latest may be meaningful for logically separating one task from others.

Task has a name exactly for the purpose of distinguishing similar but different tasks, please use it. Only the task creator knows how to name it better.

@kmaork
Copy link
Mannequin

kmaork mannequin commented Feb 20, 2020

Not so easy to find a satisfactory generic approach.
I agree, but wouldn't you agree that some information is better than no information?

Task has a name exactly for the purpose of distinguishing similar but different tasks
But in case the same task is run many times with different arguments, the task's name by itself doesn't provide very useful information...

@1st1
Copy link
Member

1st1 commented Feb 20, 2020

I agree, but wouldn't you agree that some information is better than no information?

We do agree with that. Making it work in the way that does not disturb people when a 10mb bytes string is passed is challenging. We could just cut everything after 100 characters, but it's not an ideal solution either.

But in case the same task is run many times with different arguments, the task's name by itself doesn't provide very useful information...

So make it useful. You can concatenate critical arguments reprs to task names or make them informative in other way. If you're working with a third-party library that doesn't use task names consider making a PR.

@kmaork
Copy link
Mannequin

kmaork mannequin commented Feb 20, 2020

Oh I just learned that since python3.8 you can name individual tasks. Sorry for the confusion :)
It does seem like a good solution.

@1st1
Copy link
Member

1st1 commented Feb 20, 2020

It does seem like a good solution.

Great. I'll close this issue then as the proposed solution is actually not as straightforward as it seems. Task names exist specifically to solve this case.

@1st1 1st1 closed this as completed Feb 20, 2020
@1st1 1st1 closed this as completed Feb 20, 2020
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants