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

Is it ok that input args are printed twice? #100

Open
a1tus opened this issue Feb 27, 2022 · 10 comments
Open

Is it ok that input args are printed twice? #100

a1tus opened this issue Feb 27, 2022 · 10 comments

Comments

@a1tus
Copy link

a1tus commented Feb 27, 2022

I get my "debugged" vars printed both by debug utils and as a return val, so in case of dataclasses or big dicts (for which it fits the most) it eats all screen space.

Am I missing something or it works as expected?

Python 3.10.2 (main, Jan 29 2022, 02:55:36) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from devtools import debug
>>> debug({'a': 1, 'b': 2})
<stdin>:1 <module> (no code context for debug call, code inspection impossible)
    {
        'a': 1,
        'b': 2,
    } (dict) len=2
{'a': 1, 'b': 2}
@alexmojaki
Copy link
Contributor

This is normal, and is just what happens if you use debug by itself in the shell. There's not much reason to do that, but if you really want to, you can supress the extra printing from the shell by writing something like _=debug(...) instead so that it doesn't simply evaluate the expression.

@a1tus
Copy link
Author

a1tus commented Feb 27, 2022

I was using it exactly like this :)

Thank you, so I'm closing it then.

@a1tus a1tus closed this as completed Feb 27, 2022
@samuelcolvin
Copy link
Owner

I think returning the arguments should be configurable via an env variable.

I don't use the return value much, and I can imagine it's annoying in ipython.

@samuelcolvin samuelcolvin reopened this Feb 27, 2022
@a1tus
Copy link
Author

a1tus commented Feb 27, 2022

Oh, that would be nice. In our case usage is primarily limited to python/ipython/django shell plus.

Writing _ = debug(x) of course wastes time.

I can also suggest to consider os.isatty but not sure if it will fit all cases.

@samuelcolvin
Copy link
Owner

Ye I thought about os.isatty or similar, but I think it would be more confusing "black magic".

@alexmojaki
Copy link
Contributor

os.isatty(sys.stdout.fileno()) gives the same thing in the shell and in regular scripts, I don't see how it would help.

Checking if the next bytecode instruction is a PRINT_EXPR might work.

@a1tus
Copy link
Author

a1tus commented Feb 28, 2022

There is always an option to add separate func that just calls current debug with param to disable return like dbg, but again it depends whether you want to extend public contract.

@samuelcolvin
Copy link
Owner

You can check if you're in ipython like https://github.com/samuelcolvin/pydantic/blob/9d631a3429a66f30742c1a52c94ac18ec6ba848d/pydantic/utils.py#L183

But overall I agree, I think a new method might be best.

@sshishov
Copy link

Sorry, I just start looking into this tool recently. But reading this issue I have a question... Why the debug should return the value? When you do print, it does not return anything, it just prints...

What is the root cause of the debug returning the value it is debugging? some chaining or what?

@alexmojaki
Copy link
Contributor

So that if you have some code foo(bar()) you can change it to foo(debug(bar())) which is easier than b = bar(); debug(b); foo(b)

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

No branches or pull requests

4 participants