From 89f90ef849e855faa1da3cc6d4a8e3a23a133905 Mon Sep 17 00:00:00 2001 From: Mads Marquart Date: Thu, 23 Jan 2020 10:18:33 +0100 Subject: [PATCH] Make all models frozen, and sessions hashable --- fbchat/_client.py | 7 +------ fbchat/_core.py | 5 ++--- fbchat/_exception.py | 19 ++++++++----------- fbchat/_session.py | 2 +- tests/conftest.py | 12 ++++-------- 5 files changed, 16 insertions(+), 29 deletions(-) diff --git a/fbchat/_client.py b/fbchat/_client.py index 5f526aa..157218e 100644 --- a/fbchat/_client.py +++ b/fbchat/_client.py @@ -27,12 +27,7 @@ class Client: """ #: The session to use when making requests. - _session = attr.ib(type=_session.Session) - - @property - def session(self): - """The session that's used when making requests.""" - return self._session + session = attr.ib(type=_session.Session) def fetch_users(self) -> Sequence[_user.UserData]: """Fetch users the client is currently chatting with. diff --git a/fbchat/_core.py b/fbchat/_core.py index fed25af..772e9a0 100644 --- a/fbchat/_core.py +++ b/fbchat/_core.py @@ -8,11 +8,10 @@ log = logging.getLogger("fbchat") kw_only = sys.version_info[:2] > (3, 5) #: Default attrs settings for classes -attrs_default = attr.s(slots=True, kw_only=kw_only) +attrs_default = attr.s(frozen=True, slots=True, kw_only=kw_only) -# Frozen, so that it can be used in sets -@attr.s(frozen=True, slots=True, kw_only=kw_only) +@attrs_default class Image: #: URL to the image url = attr.ib(type=str) diff --git a/fbchat/_exception.py b/fbchat/_exception.py index 35bf621..df99b88 100644 --- a/fbchat/_exception.py +++ b/fbchat/_exception.py @@ -4,10 +4,7 @@ import requests from typing import Any # Not frozen, since that doesn't work in PyPy -attrs_exception = attr.s(slots=True, auto_exc=True) - - -@attrs_exception +@attr.s(slots=True, auto_exc=True) class FacebookError(Exception): """Base class for all custom exceptions raised by ``fbchat``. @@ -18,7 +15,7 @@ class FacebookError(Exception): message = attr.ib(type=str) -@attrs_exception +@attr.s(slots=True, auto_exc=True) class HTTPError(FacebookError): """Base class for errors with the HTTP(s) connection to Facebook.""" @@ -31,7 +28,7 @@ class HTTPError(FacebookError): return "Got {} response: {}".format(self.status_code, self.message) -@attrs_exception +@attr.s(slots=True, auto_exc=True) class ParseError(FacebookError): """Raised when we fail parsing a response from Facebook. @@ -49,7 +46,7 @@ class ParseError(FacebookError): return msg.format(self.message, self.data) -@attrs_exception +@attr.s(slots=True, auto_exc=True) class ExternalError(FacebookError): """Base class for errors that Facebook return.""" @@ -64,7 +61,7 @@ class ExternalError(FacebookError): return "{}: {}".format(self.message, self.description) -@attrs_exception +@attr.s(slots=True, auto_exc=True) class GraphQLError(ExternalError): """Raised by Facebook if there was an error in the GraphQL query.""" @@ -79,7 +76,7 @@ class GraphQLError(ExternalError): return super().__str__() -@attrs_exception +@attr.s(slots=True, auto_exc=True) class InvalidParameters(ExternalError): """Raised by Facebook if: @@ -89,14 +86,14 @@ class InvalidParameters(ExternalError): """ -@attrs_exception +@attr.s(slots=True, auto_exc=True) class NotLoggedIn(ExternalError): """Raised by Facebook if the client has been logged out.""" code = attr.ib(1357001) -@attrs_exception +@attr.s(slots=True, auto_exc=True) class PleaseRefresh(ExternalError): """Raised by Facebook if the client has been inactive for too long. diff --git a/fbchat/_session.py b/fbchat/_session.py index a190813..c0eb3d4 100644 --- a/fbchat/_session.py +++ b/fbchat/_session.py @@ -115,7 +115,7 @@ def get_error_data(html: str, url: str) -> Tuple[Optional[int], Optional[str]]: return code, soup.get_text() or None -@attr.s(slots=True, kw_only=kw_only, repr=False) +@attr.s(slots=True, kw_only=kw_only, repr=False, eq=False) class Session: """Stores and manages state required for most Facebook requests. diff --git a/tests/conftest.py b/tests/conftest.py index e708868..6f324ea 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,13 +1,9 @@ import pytest +import fbchat @pytest.fixture(scope="session") def session(): - class FakeSession: - # TODO: Add a further mocked session - user_id = "31415926536" - - def __repr__(self): - return "" - - return FakeSession() + return fbchat.Session( + user_id="31415926536", fb_dtsg=None, revision=None, session=None + )