-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
#8711: Handle disabled logging in 'caplog.set_level' and 'caplog.at_level' #8758
#8711: Handle disabled logging in 'caplog.set_level' and 'caplog.at_level' #8758
Conversation
|
||
:return int: The original disabled logging level. | ||
""" | ||
original_disable_level: int = logger_obj.manager.disable # type: ignore[attr-defined] |
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.
Linter doesn't like runtime attributes, so the ignore is needed to the best of my knowledge.
@@ -448,6 +488,9 @@ def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> Non | |||
if self._initial_handler_level is None: | |||
self._initial_handler_level = self.handler.level | |||
self.handler.setLevel(level) | |||
initial_disabled_logging_level = self.force_enable_logging(level, logger_obj) | |||
if self._initial_disabled_logging_level is None: | |||
self._initial_disabled_logging_level = initial_disabled_logging_level |
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.
We could remove this magic for a parameter if magic is frowned upon.
Just bumping / following up on this because it's popped up for me again in a project. |
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.
Thanks for sending this in! I’m new here, so I hope my comments are helpful.
src/_pytest/logging.py
Outdated
def set_level(self, level: Union[int, str], logger: Optional[str] = None) -> None: | ||
"""Set the level of a logger for the duration of a test. | ||
|
||
.. versionchanged:: 3.4 | ||
The levels of the loggers changed by this function will be | ||
restored to their initial values at the end of the test. | ||
|
||
Will enable the requesed logging level if it was disabled via :meth:`logging.disable`. |
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.
Will enable the requesed logging level if it was disabled via :meth:`logging.disable`. | |
Will enable the requested logging level if it was disabled via :meth:`logging.disable`. |
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 guess my "t" finger was tired that day.
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.
fixed
Thanks for the review. I'll fix these in the morning :) |
ec6f9d0
to
3537eaf
Compare
- Resolves issues from review provided by https://github.com/andrewdotn - Fixed spelling errors - Improved logic in `force_enable_logging` - Make a fixture to un-disable logging after a test so that logging will be un-disabled even if the test fails due to an assertion error. The fixture is in `test_fixture.py` because we can't `import logging` in `conftest.py` because there is a module called `logging` in the same folder. - Mypy implicit import error. I can't commit without this. Issue: pytest-dev#8711 PR: pytest-dev#8758
The code coverage seems to not look into those string-based tests. Most of the untested lines are actually tested. Should I just write direct function tests? |
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877) Issue: pytest-dev#8711 PR: pytest-dev#8758
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877) Issue: pytest-dev#8711 PR: pytest-dev#8758
377d826
to
e52ee7c
Compare
Fixed test coverage |
What happened to this? Would be a super handy feature to have. |
Mainly just waiting for approval |
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.
Hi @alexlambson,
Really sorry this fell through the tracks and got forgotten for so long, I appreciate the recent pings.
I left a few minor comments, the PR is really well written, including the tests.
If you could just rebase it and fix the comments I left, I will be glad to get this merged.
👍
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.
Hi @alexlambson,
Really sorry this fell through the tracks and got forgotten for so long, I appreciate the recent pings.
I left a few minor comments, the PR is really well written, including the tests.
If you could just rebase it and fix the comments I left, I will be glad to get this merged.
👍
…ytest-dev#8711 Forces requested `caplog` logging levels to be enabled if they were disabled via `logging.disable()` `[attr-defined]` mypy error ignored in `logging.py` because there were existing errors with the imports and `loggin.Logger.manager` is an attr set at runtime. Since it's in the standard lib I can't really fix that. Ignored an attr-defined error in `src/_pytest/config/__init__.py` because the re-export is necessary. Issue: pytest-dev#8711
- Resolves issues from review provided by https://github.com/andrewdotn - Fixed spelling errors - Improved logic in `force_enable_logging` - Make a fixture to un-disable logging after a test so that logging will be un-disabled even if the test fails due to an assertion error. The fixture is in `test_fixture.py` because we can't `import logging` in `conftest.py` because there is a module called `logging` in the same folder. - Mypy implicit import error. I can't commit without this. Issue: pytest-dev#8711 PR: pytest-dev#8758
- Adds test coverage for `force_enable_logging` with a string `level`. Fixes [code coverage check](https://github.com/pytest-dev/pytest/pull/8758/checks?check_run_id=4123920877) Issue: pytest-dev#8711 PR: pytest-dev#8758
- Address review ad rebase to latest from main - Make `force_enable_logging` private. Issue: pytest-dev#8711 PR: pytest-dev#8758
e52ee7c
to
7f89996
Compare
No worries @nicoddemus, pytest is a busy project.
|
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.
Thanks a lot @alexlambson!
Forces requested
caplog
logging levels to be enabled if they were disabled vialogging.disable()
[attr-defined]
mypy error ignored inlogging.py
because there were existing errors with the importsand
loggin.Logger.manager
is an attr set at runtime. Since it's in the standard lib I can't really fix that.Ignored an attr-defined error in
src/_pytest/config/__init__.py
because the re-export is necessary.Closes #8711