From ccda0c09ffaec5dfed0e9401a497301ef66eba8b Mon Sep 17 00:00:00 2001 From: Adam Dobrawy Date: Sat, 7 Mar 2026 13:52:50 +0100 Subject: [PATCH] Add __repr__ to Job, AnticaptchaClient, and BaseTask Improves debugging and REPL experience by showing useful info instead of unhelpful output. Co-Authored-By: Claude Opus 4.6 --- python_anticaptcha/base.py | 11 +++++++++++ python_anticaptcha/tasks.py | 6 ++++++ 2 files changed, 17 insertions(+) diff --git a/python_anticaptcha/base.py b/python_anticaptcha/base.py index 4e0d346..7db0c38 100644 --- a/python_anticaptcha/base.py +++ b/python_anticaptcha/base.py @@ -61,6 +61,12 @@ def report_incorrect_image(self) -> bool: def report_incorrect_recaptcha(self) -> bool: return self.client.reportIncorrectRecaptcha(self.task_id) + def __repr__(self) -> str: + status = self._last_result.get("status") if self._last_result else None + if status: + return f"" + return f"" + def join(self, maximum_time: int | None = None) -> None: elapsed_time = 0 maximum_time = maximum_time or MAXIMUM_JOIN_TIME @@ -115,6 +121,11 @@ def __exit__(self, exc_type, exc_val, exc_tb): def close(self): self.session.close() + def __repr__(self) -> str: + from urllib.parse import urlparse + host = urlparse(self.base_url).hostname or self.base_url + return f"" + @property def client_ip(self) -> str: if not hasattr(self, "_client_ip"): diff --git a/python_anticaptcha/tasks.py b/python_anticaptcha/tasks.py index 3eaf9bd..5c483e3 100644 --- a/python_anticaptcha/tasks.py +++ b/python_anticaptcha/tasks.py @@ -12,6 +12,12 @@ def serialize(self, **result: Any) -> dict[str, Any]: result["type"] = self.type return result + def __repr__(self) -> str: + attrs = {k: v for k, v in self.__dict__.items() + if not k.startswith("_") and v is not None} + fields = " ".join(f"{k}={v!r}" for k, v in attrs.items()) + return f"<{self.__class__.__name__} {fields}>" + class UserAgentMixin(BaseTask): def __init__(self, *args: Any, **kwargs: Any) -> None: