Add __str__ and __repr__ to Sanic and Bluepring (#2043)

This commit is contained in:
Adam Hopkins
2021-03-03 16:58:18 +02:00
committed by GitHub
parent a733d32715
commit 93a697f6ea
3 changed files with 65 additions and 1 deletions

View File

@@ -33,4 +33,8 @@ class BaseSanic(
ExceptionMixin,
metaclass=Base,
):
...
def __str__(self) -> str:
return f"<{self.__class__.__name__} {self.name}>"
def __repr__(self) -> str:
return f'{self.__class__.__name__}(name="{self.name}")'

View File

@@ -49,6 +49,23 @@ class Blueprint(BaseSanic):
self.version = version
self.strict_slashes = strict_slashes
def __repr__(self) -> str:
args = ", ".join(
[
f'{attr}="{getattr(self, attr)}"'
if isinstance(getattr(self, attr), str)
else f"{attr}={getattr(self, attr)}"
for attr in (
"name",
"url_prefix",
"host",
"version",
"strict_slashes",
)
]
)
return f"Blueprint({args})"
def route(self, *args, **kwargs):
kwargs["apply"] = False
return super().route(*args, **kwargs)