Make models use kw_only (on Python > 3.5)

This commit is contained in:
Mads Marquart
2019-12-11 14:57:40 +01:00
parent 523c320c08
commit 91d4055545
10 changed files with 40 additions and 32 deletions

View File

@@ -956,7 +956,7 @@ class Client:
Raises: Raises:
FBchatException: If request failed FBchatException: If request failed
""" """
thread = thread_type._to_class()(thread_id) thread = thread_type._to_class()(uid=thread_id)
data = thread._to_send_data() data = thread._to_send_data()
data.update(message._to_send_data()) data.update(message._to_send_data())
return self._do_send_request(data) return self._do_send_request(data)
@@ -975,7 +975,7 @@ class Client:
Raises: Raises:
FBchatException: If request failed FBchatException: If request failed
""" """
thread = thread_type._to_class()(thread_id) thread = thread_type._to_class()(uid=thread_id)
data = thread._to_send_data() data = thread._to_send_data()
data["action_type"] = "ma-type:user-generated-message" data["action_type"] = "ma-type:user-generated-message"
data["lightweight_action_attachment[lwa_state]"] = ( data["lightweight_action_attachment[lwa_state]"] = (
@@ -1050,7 +1050,7 @@ class Client:
def _send_location( def _send_location(
self, location, current=True, message=None, thread_id=None, thread_type=None self, location, current=True, message=None, thread_id=None, thread_type=None
): ):
thread = thread_type._to_class()(thread_id) thread = thread_type._to_class()(uid=thread_id)
data = thread._to_send_data() data = thread._to_send_data()
if message is not None: if message is not None:
data.update(message._to_send_data()) data.update(message._to_send_data())
@@ -1118,7 +1118,7 @@ class Client:
`files` should be a list of tuples, with a file's ID and mimetype. `files` should be a list of tuples, with a file's ID and mimetype.
""" """
thread = thread_type._to_class()(thread_id) thread = thread_type._to_class()(uid=thread_id)
data = thread._to_send_data() data = thread._to_send_data()
data.update(self._old_message(message)._to_send_data()) data.update(self._old_message(message)._to_send_data())
data["action_type"] = "ma-type:user-generated-message" data["action_type"] = "ma-type:user-generated-message"
@@ -1284,7 +1284,7 @@ class Client:
Raises: Raises:
FBchatException: If request failed FBchatException: If request failed
""" """
data = Group(thread_id)._to_send_data() data = Group(uid=thread_id)._to_send_data()
data["action_type"] = "ma-type:log-message" data["action_type"] = "ma-type:log-message"
data["log_message_type"] = "log:subscribe" data["log_message_type"] = "log:subscribe"

View File

@@ -1,11 +1,15 @@
import sys
import attr import attr
import logging import logging
import aenum import aenum
log = logging.getLogger("fbchat") log = logging.getLogger("fbchat")
# Enable kw_only if the python version supports it
kw_only = sys.version_info[:2] > (3, 5)
#: Default attrs settings for classes #: Default attrs settings for classes
attrs_default = attr.s(slots=True) # TODO: Add kw_only=True attrs_default = attr.s(slots=True, kw_only=kw_only)
class Enum(aenum.Enum): class Enum(aenum.Enum):
@@ -27,7 +31,7 @@ class Enum(aenum.Enum):
return cls(value) return cls(value)
@attr.s(frozen=True, slots=True) # TODO: Add kw_only=True @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

@@ -42,7 +42,7 @@ class Group(Thread):
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0]) plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
return cls( return cls(
data["thread_key"]["thread_fbid"], uid=data["thread_key"]["thread_fbid"],
participants=set( participants=set(
[ [
node["messaging_actor"]["id"] node["messaging_actor"]["id"]

View File

@@ -236,7 +236,7 @@ class Message:
text=data["message"].get("text"), text=data["message"].get("text"),
mentions=[ mentions=[
Mention( Mention(
m.get("entity", {}).get("id"), thread_id=m.get("entity", {}).get("id"),
offset=m.get("offset"), offset=m.get("offset"),
length=m.get("length"), length=m.get("length"),
) )
@@ -295,7 +295,7 @@ class Message:
return cls( return cls(
text=data.get("body"), text=data.get("body"),
mentions=[ mentions=[
Mention(m.get("i"), offset=m.get("o"), length=m.get("l")) Mention(thread_id=m.get("i"), offset=m.get("o"), length=m.get("l"))
for m in json.loads(data.get("data", {}).get("prng", "[]")) for m in json.loads(data.get("data", {}).get("prng", "[]"))
], ],
emoji_size=EmojiSize._from_tags(tags), emoji_size=EmojiSize._from_tags(tags),
@@ -318,7 +318,7 @@ class Message:
try: try:
mentions = [ mentions = [
Mention( Mention(
str(mention.get("i")), thread_id=str(mention.get("i")),
offset=mention.get("o"), offset=mention.get("o"),
length=mention.get("l"), length=mention.get("l"),
) )

View File

@@ -32,7 +32,7 @@ class Page(Thread):
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0]) plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
return cls( return cls(
data["id"], uid=data["id"],
url=data.get("url"), url=data.get("url"),
city=data.get("city").get("name"), city=data.get("city").get("name"),
category=data.get("category_type"), category=data.get("category_type"),

View File

@@ -98,7 +98,7 @@ def _2fa_helper(session, code, r):
return r return r
@attrs_default # TODO i Python 3: Add kw_only=True @attrs_default
class State: class State:
"""Stores and manages state required for most Facebook requests.""" """Stores and manages state required for most Facebook requests."""

View File

@@ -78,7 +78,7 @@ class User(Thread):
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0]) plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
return cls( return cls(
data["id"], uid=data["id"],
url=data.get("url"), url=data.get("url"),
first_name=data.get("first_name"), first_name=data.get("first_name"),
last_name=data.get("last_name"), last_name=data.get("last_name"),
@@ -123,7 +123,7 @@ class User(Thread):
plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0]) plan = _plan.Plan._from_graphql(data["event_reminders"]["nodes"][0])
return cls( return cls(
user["id"], uid=user["id"],
url=user.get("url"), url=user.get("url"),
name=user.get("name"), name=user.get("name"),
first_name=first_name, first_name=first_name,
@@ -144,7 +144,7 @@ class User(Thread):
@classmethod @classmethod
def _from_all_fetch(cls, data): def _from_all_fetch(cls, data):
return cls( return cls(
data["id"], uid=data["id"],
first_name=data.get("firstName"), first_name=data.get("firstName"),
url=data.get("uri"), url=data.get("uri"),
photo=Image(url=data.get("thumbSrc")), photo=Image(url=data.get("thumbSrc")),

View File

@@ -10,12 +10,12 @@ pytestmark = pytest.mark.online
@pytest.fixture( @pytest.fixture(
scope="module", scope="module",
params=[ params=[
Plan(int(time()) + 100, random_hex()), Plan(time=int(time()) + 100, title=random_hex()),
pytest.param( pytest.param(
Plan(int(time()), random_hex()), Plan(time=int(time()), title=random_hex()),
marks=[pytest.mark.xfail(raises=FBchatFacebookError)], marks=[pytest.mark.xfail(raises=FBchatFacebookError)],
), ),
pytest.param(Plan(0, None), marks=[pytest.mark.xfail()]), pytest.param(Plan(time=0, title=None), marks=[pytest.mark.xfail()]),
], ],
) )
def plan_data(request, client, user, thread, catch_event, compare): def plan_data(request, client, user, thread, catch_event, compare):

View File

@@ -13,26 +13,26 @@ pytestmark = pytest.mark.online
Poll( Poll(
title=random_hex(), title=random_hex(),
options=[ options=[
PollOption(random_hex(), vote=True), PollOption(text=random_hex(), vote=True),
PollOption(random_hex(), vote=True), PollOption(text=random_hex(), vote=True),
], ],
), ),
Poll( Poll(
title=random_hex(), title=random_hex(),
options=[ options=[
PollOption(random_hex(), vote=False), PollOption(text=random_hex(), vote=False),
PollOption(random_hex(), vote=False), PollOption(text=random_hex(), vote=False),
], ],
), ),
Poll( Poll(
title=random_hex(), title=random_hex(),
options=[ options=[
PollOption(random_hex(), vote=True), PollOption(text=random_hex(), vote=True),
PollOption(random_hex(), vote=True), PollOption(text=random_hex(), vote=True),
PollOption(random_hex(), vote=False), PollOption(text=random_hex(), vote=False),
PollOption(random_hex(), vote=False), PollOption(text=random_hex(), vote=False),
PollOption(random_hex()), PollOption(text=random_hex()),
PollOption(random_hex()), PollOption(text=random_hex()),
], ],
), ),
pytest.param( pytest.param(

View File

@@ -22,9 +22,13 @@ EMOJI_LIST = [
] ]
STICKER_LIST = [ STICKER_LIST = [
Sticker("767334476626295"), Sticker(uid="767334476626295"),
pytest.param(Sticker("0"), marks=[pytest.mark.xfail(raises=FBchatFacebookError)]), pytest.param(
pytest.param(Sticker(None), marks=[pytest.mark.xfail(raises=FBchatFacebookError)]), Sticker(uid="0"), marks=[pytest.mark.xfail(raises=FBchatFacebookError)]
),
pytest.param(
Sticker(uid=None), marks=[pytest.mark.xfail(raises=FBchatFacebookError)]
),
] ]
TEXT_LIST = [ TEXT_LIST = [