From d97d0ed85a58853c4b5c8131b740232ce28af114 Mon Sep 17 00:00:00 2001 From: Pierre GIRAUD Date: Wed, 27 Nov 2024 11:20:22 +0100 Subject: [PATCH 1/2] Explicitly set and use a message field for ParseError class Instead of magical args[0] --- pgtoolkit/errors.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pgtoolkit/errors.py b/pgtoolkit/errors.py index 60afdd7..adc0a61 100644 --- a/pgtoolkit/errors.py +++ b/pgtoolkit/errors.py @@ -3,7 +3,8 @@ class ParseError(Exception): def __init__(self, lineno: int, line: str, message: str) -> None: - super().__init__(message) + self.message = message + super().__init__(self.message) self.lineno = lineno self.line = line @@ -11,12 +12,12 @@ def __repr__(self) -> str: return "<%s at line %d: %.32s>" % ( self.__class__.__name__, self.lineno, - self.args[0], + self.message, ) def __str__(self) -> str: return "Bad line #{} '{:.32}': {}".format( self.lineno, self.line.strip(), - self.args[0], + self.message, ) From 6da3a6557b2ed12e892db8c0f118c16b873c8602 Mon Sep 17 00:00:00 2001 From: Pierre GIRAUD Date: Wed, 27 Nov 2024 11:35:40 +0100 Subject: [PATCH 2/2] Define ParseError as a dataclass --- pgtoolkit/errors.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/pgtoolkit/errors.py b/pgtoolkit/errors.py index adc0a61..20f8d3f 100644 --- a/pgtoolkit/errors.py +++ b/pgtoolkit/errors.py @@ -1,19 +1,13 @@ from __future__ import annotations +from dataclasses import dataclass -class ParseError(Exception): - def __init__(self, lineno: int, line: str, message: str) -> None: - self.message = message - super().__init__(self.message) - self.lineno = lineno - self.line = line - def __repr__(self) -> str: - return "<%s at line %d: %.32s>" % ( - self.__class__.__name__, - self.lineno, - self.message, - ) +@dataclass +class ParseError(Exception): + lineno: int + line: str + message: str def __str__(self) -> str: return "Bad line #{} '{:.32}': {}".format(