Skip to content

Commit

Permalink
Add typing to some basic methods in the exceptions module
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielNoord committed Nov 26, 2022
1 parent 829430e commit b69b295
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions jsonschema/exceptions.py
Expand Up @@ -67,7 +67,7 @@ def __init__(
for error in context:
error.parent = self

def __repr__(self):
def __repr__(self) -> str:
return f"<{self.__class__.__name__}: {self.message!r}>"

def __str__(self) -> str:
Expand Down Expand Up @@ -104,11 +104,11 @@ def __str__(self) -> str:
)

@classmethod
def create_from(cls, other):
def create_from(cls, other: _Error):
return cls(**other._contents())

@property
def absolute_path(self):
def absolute_path(self) -> deque[str]:
parent = self.parent
if parent is None:
return self.relative_path
Expand All @@ -118,7 +118,7 @@ def absolute_path(self):
return path

@property
def absolute_schema_path(self):
def absolute_schema_path(self) -> deque[str]:
parent = self.parent
if parent is None:
return self.relative_schema_path
Expand All @@ -128,7 +128,7 @@ def absolute_schema_path(self):
return path

@property
def json_path(self):
def json_path(self) -> str:
path = "$"
for elem in self.absolute_path:
if isinstance(elem, int):
Expand All @@ -137,7 +137,9 @@ def json_path(self):
path += "." + elem
return path

def _set(self, type_checker=None, **kwargs):
def _set(
self, type_checker: _types.TypeChecker | None = None, **kwargs: Any
) -> None:
if type_checker is not None and self._type_checker is _unset:
self._type_checker = type_checker

Expand Down Expand Up @@ -199,7 +201,7 @@ class RefResolutionError(Exception):

_cause = attr.ib()

def __str__(self):
def __str__(self) -> str:
return str(self._cause)


Expand All @@ -208,10 +210,10 @@ class UndefinedTypeCheck(Exception):
A type checker was asked to check a type it did not have registered.
"""

def __init__(self, type):
def __init__(self, type: str) -> None:
self.type = type

def __str__(self):
def __str__(self) -> str:
return f"Type {self.type!r} is unknown to this type checker"


Expand Down

0 comments on commit b69b295

Please sign in to comment.