Make all models frozen, and sessions hashable

This commit is contained in:
Mads Marquart
2020-01-23 10:18:33 +01:00
parent 7019124d1f
commit 89f90ef849
5 changed files with 16 additions and 29 deletions

View File

@@ -27,12 +27,7 @@ class Client:
""" """
#: The session to use when making requests. #: The session to use when making requests.
_session = attr.ib(type=_session.Session) session = attr.ib(type=_session.Session)
@property
def session(self):
"""The session that's used when making requests."""
return self._session
def fetch_users(self) -> Sequence[_user.UserData]: def fetch_users(self) -> Sequence[_user.UserData]:
"""Fetch users the client is currently chatting with. """Fetch users the client is currently chatting with.

View File

@@ -8,11 +8,10 @@ log = logging.getLogger("fbchat")
kw_only = sys.version_info[:2] > (3, 5) kw_only = sys.version_info[:2] > (3, 5)
#: Default attrs settings for classes #: 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 @attrs_default
@attr.s(frozen=True, slots=True, kw_only=kw_only)
class Image: class Image:
#: URL to the image #: URL to the image
url = attr.ib(type=str) url = attr.ib(type=str)

View File

@@ -4,10 +4,7 @@ import requests
from typing import Any from typing import Any
# Not frozen, since that doesn't work in PyPy # Not frozen, since that doesn't work in PyPy
attrs_exception = attr.s(slots=True, auto_exc=True) @attr.s(slots=True, auto_exc=True)
@attrs_exception
class FacebookError(Exception): class FacebookError(Exception):
"""Base class for all custom exceptions raised by ``fbchat``. """Base class for all custom exceptions raised by ``fbchat``.
@@ -18,7 +15,7 @@ class FacebookError(Exception):
message = attr.ib(type=str) message = attr.ib(type=str)
@attrs_exception @attr.s(slots=True, auto_exc=True)
class HTTPError(FacebookError): class HTTPError(FacebookError):
"""Base class for errors with the HTTP(s) connection to Facebook.""" """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) return "Got {} response: {}".format(self.status_code, self.message)
@attrs_exception @attr.s(slots=True, auto_exc=True)
class ParseError(FacebookError): class ParseError(FacebookError):
"""Raised when we fail parsing a response from Facebook. """Raised when we fail parsing a response from Facebook.
@@ -49,7 +46,7 @@ class ParseError(FacebookError):
return msg.format(self.message, self.data) return msg.format(self.message, self.data)
@attrs_exception @attr.s(slots=True, auto_exc=True)
class ExternalError(FacebookError): class ExternalError(FacebookError):
"""Base class for errors that Facebook return.""" """Base class for errors that Facebook return."""
@@ -64,7 +61,7 @@ class ExternalError(FacebookError):
return "{}: {}".format(self.message, self.description) return "{}: {}".format(self.message, self.description)
@attrs_exception @attr.s(slots=True, auto_exc=True)
class GraphQLError(ExternalError): class GraphQLError(ExternalError):
"""Raised by Facebook if there was an error in the GraphQL query.""" """Raised by Facebook if there was an error in the GraphQL query."""
@@ -79,7 +76,7 @@ class GraphQLError(ExternalError):
return super().__str__() return super().__str__()
@attrs_exception @attr.s(slots=True, auto_exc=True)
class InvalidParameters(ExternalError): class InvalidParameters(ExternalError):
"""Raised by Facebook if: """Raised by Facebook if:
@@ -89,14 +86,14 @@ class InvalidParameters(ExternalError):
""" """
@attrs_exception @attr.s(slots=True, auto_exc=True)
class NotLoggedIn(ExternalError): class NotLoggedIn(ExternalError):
"""Raised by Facebook if the client has been logged out.""" """Raised by Facebook if the client has been logged out."""
code = attr.ib(1357001) code = attr.ib(1357001)
@attrs_exception @attr.s(slots=True, auto_exc=True)
class PleaseRefresh(ExternalError): class PleaseRefresh(ExternalError):
"""Raised by Facebook if the client has been inactive for too long. """Raised by Facebook if the client has been inactive for too long.

View File

@@ -115,7 +115,7 @@ def get_error_data(html: str, url: str) -> Tuple[Optional[int], Optional[str]]:
return code, soup.get_text() or None 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: class Session:
"""Stores and manages state required for most Facebook requests. """Stores and manages state required for most Facebook requests.

View File

@@ -1,13 +1,9 @@
import pytest import pytest
import fbchat
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def session(): def session():
class FakeSession: return fbchat.Session(
# TODO: Add a further mocked session user_id="31415926536", fb_dtsg=None, revision=None, session=None
user_id = "31415926536" )
def __repr__(self):
return "<FakeSession>"
return FakeSession()