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 467e171 commit 5e79923
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions jsonschema/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,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 @@ -107,11 +107,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 @@ -121,7 +121,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 @@ -131,7 +131,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 @@ -140,7 +140,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 @@ -202,7 +204,7 @@ class RefResolutionError(Exception):

_cause = attr.ib()

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


Expand All @@ -211,10 +213,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 5e79923

Please sign in to comment.