From f04d57acbc5ef639c0dc70fde800cf5c24d0b967 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Sun, 20 Nov 2022 20:22:48 +0100 Subject: [PATCH] refactor: use is for True/False --- mastodon/Mastodon.py | 69 +++++++++++++++++++-------------------- tests/test_bookmarks.py | 4 +-- tests/test_constructor.py | 6 ++-- tests/test_filters.py | 14 ++++---- tests/test_instance.py | 2 +- tests/test_media.py | 2 +- tests/test_push.py | 16 ++++----- tests/test_streaming.py | 8 ++--- tests/test_timeline.py | 4 +-- 9 files changed, 61 insertions(+), 64 deletions(-) diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index bbd06be..72eb727 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -632,7 +632,7 @@ class Mastodon: self.__logged_in_id = None # Retry version check if needed (might be required in limited federation mode) - if self.version_check_worked == False: + if not self.version_check_worked: self.retrieve_mastodon_version() return response['access_token'] @@ -698,7 +698,7 @@ class Mastodon: params['client_id'] = self.client_id params['client_secret'] = self.client_secret - if agreement == False: + if not agreement: del params['agreement'] # Step 1: Get a user-free token via oauth @@ -876,13 +876,13 @@ class Mastodon: params_initial = locals() - if local == False: + if not local: del params_initial['local'] - if remote == False: + if not remote: del params_initial['remote'] - if only_media == False: + if not only_media: del params_initial['only_media'] if timeline == "local": @@ -1188,13 +1188,13 @@ class Mastodon: since_id = self.__unpack_id(since_id, dateconv=True) params = self.__generate_params(locals(), ['id']) - if pinned == False: + if not pinned: del params["pinned"] - if only_media == False: + if not only_media: del params["only_media"] - if exclude_replies == False: + if not exclude_replies: del params["exclude_replies"] - if exclude_reblogs == False: + if not exclude_reblogs: del params["exclude_reblogs"] url = '/api/v1/accounts/{0}/statuses'.format(str(id)) @@ -1359,7 +1359,7 @@ class Mastodon: continue filter_string = re.escape(keyword_filter["phrase"]) - if keyword_filter["whole_word"] == True: + if keyword_filter["whole_word"]: filter_string = "\\b" + filter_string + "\\b" filter_strings.append(filter_string) filter_re = re.compile("|".join(filter_strings), flags=re.IGNORECASE) @@ -1426,7 +1426,7 @@ class Mastodon: for search that are available only starting with 2.8.0 are specified. """ if any(item is not None for item in (account_id, offset, min_id, max_id)): - if self.verify_minimum_version("2.8.0", cached=True) == False: + if not self.verify_minimum_version("2.8.0", cached=True): raise MastodonVersionError("Advanced search parameters require Mastodon 2.8.0+") @api_version("1.1.0", "2.8.0", __DICT_VERSION_SEARCHRESULT) @@ -1455,7 +1455,7 @@ class Mastodon: Returns a `search result dict`_, with tags as `hashtag dicts`_. """ - if self.verify_minimum_version("2.4.1", cached=True) == True: + if self.verify_minimum_version("2.4.1", cached=True): return self.search_v2(q, resolve=resolve, result_type=result_type, account_id=account_id, offset=offset, min_id=min_id, max_id=max_id) else: self.__ensure_search_params_acceptable( @@ -1471,7 +1471,7 @@ class Mastodon: Returns a `search result dict`_. """ params = self.__generate_params(locals()) - if resolve == False: + if not resolve: del params['resolve'] return self.__api_request('GET', '/api/v1/search', params) @@ -1489,10 +1489,10 @@ class Mastodon: account_id, offset, min_id, max_id) params = self.__generate_params(locals()) - if resolve == False: + if not resolve: del params["resolve"] - if exclude_unreviewed == False or not self.verify_minimum_version("3.0.0", cached=True): + if not exclude_unreviewed or not self.verify_minimum_version("3.0.0", cached=True): del params["exclude_unreviewed"] if "result_type" in params: @@ -2587,7 +2587,7 @@ class Mastodon: status_ids = list(map(lambda x: self.__unpack_id(x), status_ids)) params_initial = locals() - if forward == False: + if not forward: del params_initial['forward'] params = self.__generate_params(params_initial) @@ -2953,7 +2953,7 @@ class Mastodon: params = self.__generate_params( locals(), ['remote', 'status', 'staff_only']) - if remote == True: + if remote: params["remote"] = True mod_statuses = ["active", "pending", @@ -2961,7 +2961,7 @@ class Mastodon: if not status in mod_statuses: raise ValueError("Invalid moderation status requested.") - if staff_only == True: + if staff_only: params["staff"] = True for mod_status in mod_statuses: @@ -3073,7 +3073,7 @@ class Mastodon: if action is None: action = "none" - if send_email_notification == False: + if not send_email_notification: send_email_notification = None id = self.__unpack_id(id) @@ -3112,7 +3112,7 @@ class Mastodon: if target_account_id is not None: target_account_id = self.__unpack_id(target_account_id) - if resolved == False: + if not resolved: resolved = None params = self.__generate_params(locals()) @@ -3269,12 +3269,12 @@ class Mastodon: # Figure out what size to decode to decode_components_x, decode_components_y = blurhash.components( media_dict["blurhash"]) - if size_per_component == False: - decode_size_x = out_size[0] - decode_size_y = out_size[1] - else: + if size_per_component: decode_size_x = decode_components_x * out_size[0] decode_size_y = decode_components_y * out_size[1] + else: + decode_size_x = out_size[0] + decode_size_y = out_size[1] # Decode decoded_image = blurhash.decode( @@ -3584,13 +3584,12 @@ class Mastodon: response_object = None try: kwargs = dict(headers=headers, files=files, timeout=self.request_timeout) - if use_json == False: - if method == 'GET': - kwargs['params'] = params - else: - kwargs['data'] = params - else: + if use_json: kwargs['json'] = params + elif method == 'GET': + kwargs['params'] = params + else: + kwargs['data'] = params # Block list with exactly three entries, matching on hashes of the instance API domain # For more information, have a look at the docs @@ -3679,7 +3678,7 @@ class Mastodon: request_complete = False continue - if skip_error_check == False: + if not skip_error_check: if response_object.status_code == 404: ex_type = MastodonNotFoundError if not error_msg: @@ -3708,7 +3707,7 @@ class Mastodon: if return_response_object: return response_object - if parse == True: + if parse: try: response = response_object.json( object_hook=self.__json_hooks) @@ -3968,10 +3967,8 @@ class Mastodon: param_keys = list(params.keys()) for key in param_keys: - if isinstance(params[key], bool) and params[key] == False: - params[key] = '0' - if isinstance(params[key], bool) and params[key] == True: - params[key] = '1' + if isinstance(params[key], bool): + params[key] = '1' if params[key] else '0' for key in param_keys: if params[key] is None or key in exclude: diff --git a/tests/test_bookmarks.py b/tests/test_bookmarks.py index 2e1261b..54134e9 100644 --- a/tests/test_bookmarks.py +++ b/tests/test_bookmarks.py @@ -4,7 +4,7 @@ import pytest def test_bookmarks(api, status): status_bookmarked = api.status_bookmark(status) assert status_bookmarked - assert status_bookmarked.bookmarked == True + assert status_bookmarked.bookmarked bookmarked_statuses = api.bookmarks() assert bookmarked_statuses @@ -18,7 +18,7 @@ def test_bookmarks(api, status): status_unbookmarked = api.status_unbookmark(status_bookmarked) assert status_unbookmarked - assert status_unbookmarked.bookmarked == False + assert not status_unbookmarked.bookmarked bookmarked_statuses_2 = api.bookmarks() assert bookmarked_statuses_2 is not None diff --git a/tests/test_constructor.py b/tests/test_constructor.py index ebc9b85..d997a5d 100644 --- a/tests/test_constructor.py +++ b/tests/test_constructor.py @@ -33,9 +33,9 @@ def test_constructor_missing_client_secret(): @pytest.mark.vcr() def test_verify_version(api): - assert api.verify_minimum_version("2.3.3") == True - assert api.verify_minimum_version("9999.9999.9999") == False - assert api.verify_minimum_version("1.0.0") == True + assert api.verify_minimum_version("2.3.3") is True + assert api.verify_minimum_version("9999.9999.9999") is False + assert api.verify_minimum_version("1.0.0") is True def test_supported_version(api): assert Mastodon.get_supported_version() \ No newline at end of file diff --git a/tests/test_filters.py b/tests/test_filters.py index 3ffa726..d3dab8d 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -7,12 +7,12 @@ def test_filter_create(api): with vcr.use_cassette('test_filter_create.yaml', cassette_library_dir='tests/cassettes_pre_4_0_0', record_mode='none'): keyword_filter = api.filter_create("anime", ['notifications'], irreversible = False, whole_word = True, expires_in = None) try: - assert(keyword_filter) + assert keyword_filter all_filters = api.filters() - assert(keyword_filter in all_filters) - assert(keyword_filter.irreversible == False) - assert(keyword_filter.whole_word == True) + assert keyword_filter in all_filters + assert keyword_filter.irreversible is False + assert keyword_filter.whole_word is True keyword_filter_2 = api.filter(keyword_filter.id) assert(keyword_filter == keyword_filter_2) @@ -22,9 +22,9 @@ def test_filter_create(api): keyword_filter = api.filter_create("anime", ['notifications'], irreversible = False, whole_word = False, expires_in = None) try: - assert(keyword_filter) - assert(keyword_filter.irreversible == False) - assert(keyword_filter.whole_word == False) + assert keyword_filter + assert keyword_filter.irreversible is False + assert keyword_filter.whole_word is False all_filters = api.filters() assert(keyword_filter in all_filters) diff --git a/tests/test_instance.py b/tests/test_instance.py index 8f3f142..1fbd692 100644 --- a/tests/test_instance.py +++ b/tests/test_instance.py @@ -38,7 +38,7 @@ def test_emoji(api): @pytest.mark.vcr() def test_health(api): - assert api.instance_health() == True + assert api.instance_health() is True @pytest.mark.vcr() def test_server_time(api): diff --git a/tests/test_media.py b/tests/test_media.py index 9668f59..e16fb4d 100644 --- a/tests/test_media.py +++ b/tests/test_media.py @@ -21,7 +21,7 @@ def test_media_post_v1(api): assert status try: - assert status['sensitive'] == False + assert status['sensitive'] is False assert status['media_attachments'] assert status['media_attachments'][0]['description'] == "John Lennon doing a funny walk" assert status['media_attachments'][0]['meta']['focus']['x'] == -0.5 diff --git a/tests/test_push.py b/tests/test_push.py index daa99c7..b815910 100644 --- a/tests/test_push.py +++ b/tests/test_push.py @@ -57,14 +57,14 @@ def test_push_update(api): print(sub3) print(api.push_subscription()) - assert sub3.alerts.follow == False - assert sub3.alerts.favourite == False - assert sub3.alerts.reblog == False - assert sub3.alerts.mention == False - assert sub2.alerts.follow == True - assert sub2.alerts.favourite == True - assert sub2.alerts.reblog == True - assert sub2.alerts.mention == True + assert sub3.alerts.follow is False + assert sub3.alerts.favourite is False + assert sub3.alerts.reblog is False + assert sub3.alerts.mention is False + assert sub2.alerts.follow is True + assert sub2.alerts.favourite is True + assert sub2.alerts.reblog is True + assert sub2.alerts.mention is True @pytest.mark.vcr(match_on=['path']) diff --git a/tests/test_streaming.py b/tests/test_streaming.py index fb60fe1..53a71ee 100644 --- a/tests/test_streaming.py +++ b/tests/test_streaming.py @@ -20,7 +20,7 @@ close_connections = False def patch_streaming(): global streaming_is_patched global close_connections - if streaming_is_patched == True: + if streaming_is_patched is True: return streaming_is_patched = True @@ -35,7 +35,7 @@ def patch_streaming(): response = real_connection_real_get_response(*args, **kwargs) real_body = b"" try: - while close_connections == False: + while close_connections is False: if len(select.select([response], [], [], 0.01)[0]) > 0: chunk = response.read(1) real_body += chunk @@ -165,7 +165,7 @@ def test_unknown_event(): 'data: {}', '', ]) - assert listener.bla_called == True + assert listener.bla_called is True assert listener.updates == [] assert listener.notifications == [] assert listener.deletes == [] @@ -195,7 +195,7 @@ def test_dotted_unknown_event(): 'data: {}', '', ]) - assert listener.do_something_called == True + assert listener.do_something_called is True assert listener.updates == [] assert listener.notifications == [] assert listener.deletes == [] diff --git a/tests/test_timeline.py b/tests/test_timeline.py index bc6728f..239fac3 100644 --- a/tests/test_timeline.py +++ b/tests/test_timeline.py @@ -60,8 +60,8 @@ def test_conversations(api, api2): assert conversations assert status.id in map(lambda x: x.last_status.id, conversations) assert account.id in map(lambda x: x.accounts[0].id, conversations) - assert conversations[0].unread == True - assert conversations2[0].unread == False + assert conversations[0].unread is True + assert conversations2[0].unread is False @pytest.mark.vcr() def test_min_max_id(api, status):