samuelcolvin / python-devtools

Dev tools for python
https://python-devtools.helpmanual.io/
MIT License
985 stars 47 forks source link

Add option to include stack trace in debug() output #143

Open kalekundert opened 1 year ago

kalekundert commented 1 year ago

As discussed in #105, this PR adds some options to have debug() output stack traces.

While I was working on this, I realized that there are two reasonable way to format stack traces. The first is to match the existing style that debug() uses:

<file>:<line> <function>

The second is to match the style that python itself uses:

File "<file>", line <line>, in <function>
    <open(file).readlines()[line-1]>

Both styles have the same information, but the python style is more verbose and includes the actual text of the line in question. I decided to use the devtools style, because it "fits" better and I didn't think the extra verbosity would be that helpful for the kinds of things I envision debugging with this feature, but I'm not confident in this decision. I might change my mind after living with it for a while. If anyone else has an opinion on which style to use, I'd be happy to hear it.

Another aspect of this PR that I should draw attention to is the new unit tests. I think that all the stack trace tests need to happen in subprocesses, because otherwise the stack traces would include ≈20 pytest frames (which would be likely to change between different versions of pytest). To avoid duplicating the boilerplate required to run a test in a subprocess, I decided to write these tests using two of my own libraries: parametrize_from_file and pytest_tmp_files. The specific code I wrote here is very similar to this example from the documentation. I think that I wrote these tests in the most understandable, maintainable way, but I'd understand if you were skeptical about adding two relatively unknown dependencies to your project. If so, let me know and I'd be happy to rewrite the tests using vanilla pytest. (Also, I had to regenerate the requirements/testing.txt file, and I'm not sure I did that in the right way.)

codecov[bot] commented 1 year ago

Codecov Report

Merging #143 (3edf2e9) into main (ec406ff) will decrease coverage by 0.42%. The diff coverage is 92.00%.

:exclamation: Current head 3edf2e9 differs from pull request most recent head 128a178. Consider uploading reports for the commit 128a178 to get more accurate results

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #143 +/- ## ========================================== - Coverage 96.29% 95.88% -0.42% ========================================== Files 8 8 Lines 729 753 +24 Branches 111 115 +4 ========================================== + Hits 702 722 +20 - Misses 21 23 +2 - Partials 6 8 +2 ``` | [Files](https://app.codecov.io/gh/samuelcolvin/python-devtools/pull/143?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin) | Coverage Δ | | |---|---|---| | [devtools/debug.py](https://app.codecov.io/gh/samuelcolvin/python-devtools/pull/143?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin#diff-ZGV2dG9vbHMvZGVidWcucHk=) | `97.38% <92.00%> (-2.62%)` | :arrow_down: | ------ [Continue to review full report in Codecov by Sentry](https://app.codecov.io/gh/samuelcolvin/python-devtools/pull/143?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin). > **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin) > `Δ = absolute (impact)`, `ø = not affected`, `? = missing data` > Powered by [Codecov](https://app.codecov.io/gh/samuelcolvin/python-devtools/pull/143?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin). Last update [ec406ff...128a178](https://app.codecov.io/gh/samuelcolvin/python-devtools/pull/143?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Samuel+Colvin).
kalekundert commented 1 year ago

Here's a screenshot: 2023-10-09-155513_2560x1440_scrot