-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Increase truncation threshold with -v, disable with -vv #8391
Conversation
5b87a59
to
3d4c3d8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The feature request and proposed solution LGTM. I tried briefly to think of alternatives but since we already started with -v
, -vv
, pushing it more seems best in terms of user expectation, although I hope 4 would be the end of it 😀
Is there any documentation/help text that needs to be updated? Also I wonder if the Full output truncated, use '-vv' to show
wants to mention the higher levels as well (probably not). Basically, offer some way for a user to figure out that disabling the ellipsis is possible.
src/_pytest/_io/saferepr.py
Outdated
@@ -75,15 +90,15 @@ def safeformat(obj: object) -> str: | |||
return _format_repr_exception(exc, obj) | |||
|
|||
|
|||
def saferepr(obj: object, maxsize: int = 240) -> str: | |||
def saferepr(obj: object, maxsize: Optional[int] = 240) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to synergize this 240 with _DEFAULT_REPR_MAX_SIZE
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, will do!
@@ -427,7 +427,22 @@ def _saferepr(obj: object) -> str: | |||
sequences, especially '\n{' and '\n}' are likely to be present in | |||
JSON reprs. | |||
""" | |||
return saferepr(obj).replace("\n", "\\n") | |||
maxsize = _get_maxsize_for_saferepr(util._config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This consults the Config at the time the test is run. Alternative is to consult the config at the time the test is rewritten, that is, make the AssertionRewriter
get the maxsize, add a maxsize
argument to _saferepr
and make AssertionRewriter
pass it as a constant in the AST. Do you have any preference? The reason I'm talking about that is trying to avoid the util._config
mutable global. These tend to be not great...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative is to consult the config at the time the test is rewritten, that is, make the AssertionRewriter get the maxsize
I thought that too, however I realized assertion rewritten is cached, so if you invoke pytest
the first time, the .pyc
file is rewritten and cached; if you invoke pytest -v
next, the .pyc
file is cached so assertion rewriting is not triggered. Unfortunately this information needs to be available at runtime.
I injected the Config
object in the same way we inject the comparison functions today, so I think this at least aligns with how things work today, however I'm definitely open for alternative ideas. 👍
I hope too! As for alternatives, we have discussed separating verbosity of terminal output (how tests are printed as they are executed) and verbosity of assertions (how assertion messages are printed), however that change is analogous to this one.
Good point. I didn't find a place in the docs that discussed verbosity in particular, so I think we will need to add one (will do so in this PR later). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this manually on an I am happy to say that I now see an unabridged AssertionError that is ~1500 characters long, regardless the verbosity level (tried 0, 1, 2). IMHO, ~2-4KB default should be considered safe for any consumer, we should remember that these are errors, so is quite likely that the user would want to see them.
The outcome allow user to identify the what went wrong, before that it was a more like a single line which almost never included enough info to identify the issue.
Thanks for testing! Hmm something went wrong however, because my patch should only have an effect on verbosity >= 3... I will take another look at the code later, but is there anything in particular to that output that might help finding the cause? 🤔 |
In case you do not want to increase the length for lower verbosity levels, how are we going to be able to configure a custom limit? If that is again hardcoded, it may not be of much use for me. |
The limit will be 1mb for utterly verbose, 2.4k for very verbose |
Not sure I follow, but I will write docs to make this explicit later, but here is a quick verbosity/limit table:
@ssbarnea however it is not clear to me yet if you can see full output with low verbosity levels; as I commented, my patch should produce the same results up to verbosity<3, but you commented:
|
For me 240ch is not enough and v0-v3 is exactly what most users are using and I do not want to force people to tune verbosity level in order to be able to read a failure... not to mention that if this comes from a flaky test, they may not even be able to reproduce it once they increase the verbosity. Tuning verbosity is tricy for CI/CD, especially when the user encountering the error may not be the maintainer of the project, so he might not even know how to increase verbosity. Ideally we should have an ini option that defines the censoring threshold. I would personally set it to ~4000 for projects where I need it while pytest by default can use the default proposed default of 240. Did I explain the use-case well? -- Is anyone is curious about how a random test failure displays, take a look at https://github.com/ansible-community/ansible-lint/pull/1422/checks?check_run_id=2024534247#step:8:652 -- basically the "meaty" bit was censored. |
You did, thanks! I agree the current defaults can be tweaked, I also think 240 is a tad limiting. We also discussed previously about separating terminal output from assertion output in the configuration file. Regardless I think this would be a step in the right direction, because at least now the config object can be accessed by the assertion rewriting functions, which paves the way for further improvements. However can you clarify this comment:
This should not happen with my patch, that's why I want to understand how you got that result. |
3d4c3d8
to
d8965da
Compare
As discussed in #8403, changed the verbosity to 2400 for |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, great doc.
I suspect we will need to bring back some safety limit, but let's wait and see :)
74201e6
to
be8d63e
Compare
This was changed during the current docs restructing and we missed that it changed the label.
This PR also fixes the current docs (c1e0570). |
Thanks everyone! This fix will clearly make it easier to debug test failures. |
This helps with the problem described in #6682.
Decided to go with-vvv
and-vvvv
because I believe they are rarely used, so this should really affect users that want to disable truncation, but would like to hear opinions.As discussed in #8403, changed the verbosity to 2400 for
-v
, and unlimited for-vv
and up.Fix #6682
Fix #8403