Unable to use `logging.NOTSET` with `assertLogs` or `assertNoLogs`
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- Python
- Estrelas
- 77.2k
- Forks
- 35.9k
- Métricas de merge de PRs
- Métricas de PR pendentes
Descrição
Bug report
Bug description:
Since logging.NOTSET equals 0 (a falsey value), the naive conditional check in _AssertLogsContext prevents it from being used as a valid level parameter with unittest.TestCase.assertLogs() or unittest.TestCase.assertNoLogs().
Example:
import logging
import unittest
class LoggerTestCase(unittest.TestCase):
def test_logging(self):
root_logger = logging.getLogger()
root_logger.setLevel(logging.ERROR)
logger = logging.getLogger("foo")
with self.assertLogs(logger=logger, level=logging.NOTSET) as cm:
logger.info("first message")
logger.error("second message")
# This test currently fails because `assertLogs(...)` sees the falsey
# `logging.NOTSET` and sets the level to `logging.INFO` by default; so we
# end up seeing the "INFO:foo:first message" in the output.
self.assertEqual(cm.output, ["ERROR:foo:second message"])
In my real life use case, the logging.NOTSET interaction is useful in context of creating a custom class ExplicitlyConfiguredLogger(logging.Logger) that only logs when the logger is configured with something other than logging.NOTSET (used in https://github.com/element-hq/synapse/pull/18474) and wanting to write some tests for it.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
Linked PRs
- gh-137555
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece em Lib/unittest/_log.py, na condição de _AssertLogsContext referenciada na issue. Reproduza o exemplo com logging.NOTSET por meio de assertLogs e assertNoLogs e, em seguida, verifique se ambos os contextos aceitam o nível sem usar INFO como padrão e se a cobertura de regressão exercita esse comportamento.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- python
- Domínio
- testing
- Tipo de issue
- Bug
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 25/100