wrong/ambiguous type descriptions for `LogRecord.exc_info`
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Anfängerfreundlichkeit
- 52/100
- Issue-Typ
- Dokumentation
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Tech-Stack
- python
- Bereich
- documentation
Rechercherichtung
Start with the LogRecord.exc_info and Formatter.formatException() entries in the logging documentation, then compare their descriptions with the behavior and typeshed references linked in the issue. Clarify which values these APIs allow and how the documented exception tuple handles missing exception or traceback values. Done means the documentation accurately describes the relevant cases; the issue leaves open whether code or typeshed changes are also needed.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Documentation
Hey.
The documentation of LogRecord describes the exc_info attribute as:
exc_info (tuple[type[BaseException], BaseException, types.TracebackType] | None) – An exception tuple with the current exception information, as returned by sys.exc_info(), or None if no exception information is available.
However, records created through the normal logger methods can contain values that don’t match this type:
import logging
class H(logging.Handler):
def emit(self, record):
print(repr(record.exc_info))
logger = logging.getLogger()
logger.addHandler(H())
logger.propagate = False
logger.error("b", exc_info=True) # outside of an except-block
logger.error("c", exc_info=ValueError()) # exception that was never raised
which gives as of CPython 3.14.10:
(None, None, None)
(<class 'ValueError'>, ValueError(), None)
A very recent commit (938144efad4659f7aa66bf9ef362fd3c602c4bbc) already fixed the case of a false truth value, which as per “If exc_info does not evaluate as false, …” is IMO allowed.
Now:
- Outside an
except-block it would be(None, None, None). - If an exception instance is passed, the tuple is built from its traceback, which is None for exceptions that were never raised, while the documented type requires types.TracebackType.
I think typeshed already documents it properly:
https://github.com/python/typeshed/blob/80f7beabfbb71a634cc3dbca8e418a5da790142c/stdlib/logging/__init__.pyi#L63
So far I'd say this is a documentation issue.
Now I can create valid LogRecord-objects that don’t go through _log() and its normalisation:
>>> import logging
>>> l = logging.getLogger()
>>> r = logging.makeLogRecord({"exc_info": True})
>>> r.exc_info
True
>>> r = l.makeRecord("z", 40, "f", 1, "m", None, exc_info=False)
>>> r.exc_info
False
>>>
For makeLogRecord and makeRecord… it’s IMO not really clear what values these allow (de jure) for exc_info.
If they’d follow the spirit of “If exc_info does not evaluate as false, …” then the the above type description would be even more wrong, as then exc_info could be any object.
This would then IMO also make the types on typeshed wrong, cause while logging.Formatter.format() itself would call formatException() only if exc_info has a true truth value:
https://github.com/python/cpython/blob/ecefd40671f340d543c214b80744d4301d249f37/Lib/logging/__init__.py#L716-L720
that may be still any object, there's nothing for the false case and both format() and formatException() are public, so any subclass may call formatException() without the if record.exc_info:.
Last but not least:
formatException() is more or less documented to an even more restrictive type:
Formats the specified exception information (a standard exception tuple as returned by sys.exc_info()) as a string.
So not sure,... at least some documentation seems to be not exact... but maybe there also needs to be code changes if the normalisation of exc_info should happen not only via LogRecord objects created through _log()... and if not, typeshed might need some updates to account for values that may be given.
Cheers,
Chris
- Vorherrschende Sprache
- Python
- Sterne
- 78.2k
- Forks
- 36.6k
- Ø Merge
- 19 Std. 57 Min.
- Gemergte PRs (30 T.)
- 630
Entwicklungsumgebung
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/cpython
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
Maintainer antworten meist innerhalb von 1 Tag
-
stdlib type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
extension-modules stdlib topic-XML type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
python/cpython#161807 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
stdlib type-feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
python/cpython#161788 · 7 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
extension-modules topic-IO type-bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
cpp-best-practices/project_validator#267 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Project-N-E-K-O/N.E.K.O#3725 ·
Maintainer antworten meist innerhalb von 1 Tag
-
[Bug] Sending a Telegram message chain twice duplicates mentionsEvtl. vergeben @123XieC hat das heute übernommen. Offen
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
AstrBotDevs/AstrBot#11125 ·
Maintainer antworten meist innerhalb von 1 Tag
-
backend bug priority: high
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
nooriegarg/ContribIQ#73 ·
Maintainer antworten meist innerhalb von 1 Tag
-
bug p3 security
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
amd/gaia#8197 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag