Skip to content

Commit

Permalink
Improve deprecation reporting (#85)
Browse files Browse the repository at this point in the history
* Correct stacklevel on parser context warning
* Correct advice on how to get the error message
* Do not used deprecated attributes internally
  • Loading branch information
drhagen authored Jun 15, 2023
1 parent 8b18141 commit 050c822
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/parsita/metaclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class GeneralParsers(metaclass=GeneralParsersMeta):
"""

def __init_subclass__(cls, **kwargs) -> None:
warnings.warn(DeprecationWarning(deprecation_text.format("GeneralParsers")), stacklevel=1)
warnings.warn(DeprecationWarning(deprecation_text.format("GeneralParsers")), stacklevel=2)
super().__init_subclass__(**kwargs)


Expand Down Expand Up @@ -165,7 +165,7 @@ class TextParsers(metaclass=TextParsersMeta):
"""

def __init_subclass__(cls, **kwargs) -> None:
warnings.warn(DeprecationWarning(deprecation_text.format("TextParsers")), stacklevel=1)
warnings.warn(DeprecationWarning(deprecation_text.format("TextParsers")), stacklevel=3)
super().__init_subclass__(**kwargs)


Expand Down
30 changes: 13 additions & 17 deletions src/parsita/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,23 +303,21 @@ class Success(Generic[Output], Result[Output], result.Success[Output]):
"""

@property
@deprecated("Use unwrap() instead.", version="1.8.0")
@deprecated("Use self.unwrap() instead.", version="1.8.0")
def value(self) -> Output:
"""The value returned from the parser.
Deprecated: Use self.unwrap() instead.
"""
return self._inner_value
return self.unwrap()

@deprecated("Use unwrap() instead.", version="1.8.0")
@deprecated("Use self.unwrap() instead.", version="1.8.0")
def or_die(self) -> Output:
return self.value

def __eq__(self, other):
if isinstance(other, Success):
return self.value == other.value
elif isinstance(other, result.Success):
return self._inner_value == other._inner_value
if isinstance(other, result.Success):
return self.unwrap() == other.unwrap()
else:
return NotImplemented

Expand All @@ -344,30 +342,28 @@ def __init__(self, error: Union[ParseError, str]):
super().__init__(error)

@property
@deprecated("Use failure().message instead.", version="1.8.0")
@deprecated("Use str(self.failure()) instead.", version="1.8.0")
def message(self) -> str:
"""A human-readable error message.
Deprecated: Use self.failure().message instead.
Deprecated: Use str(self.failure()) instead.
From the farthest point reached during parsing.
"""
return str(self._inner_value)
return self.failure().message

@deprecated("Use unwrap() instead.", version="1.8.0")
@deprecated("Use self.unwrap() instead.", version="1.8.0")
def or_die(self) -> Output:
raise ParseError(self.message)
raise self.failure()

def __eq__(self, other):
if isinstance(other, Failure):
return self.message == other.message
elif isinstance(other, result.Failure):
return self._inner_value == other._inner_value
if isinstance(other, result.Failure):
return self.failure() == other.failure()
else:
return NotImplemented

def __repr__(self):
return f"Failure({self.message!r})"
return f"Failure({str(self.failure())!r})"


class Status(Generic[Input, Output]):
Expand Down

0 comments on commit 050c822

Please sign in to comment.