Skip to content

Commit

Permalink
Run black with python 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jan 15, 2021
1 parent 55f902b commit ce9c514
Show file tree
Hide file tree
Showing 18 changed files with 178 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .github/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ wechatpy 使用 [black](https://github.com/psf/black/) 自动格式化 Python

```bash
poetry run pip install black
poetry run black -l 120 -t py35 -t py36 -t py37 -t py38
poetry run black -l 120 -t py36 -t py37 -t py38 .
```

## 自动化测试
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
poetry install
pip list
- name: Check code style
if: matrix.python-version == 3.6
if: matrix.python-version == 3.8
run: |
pip install black
black -l 120 -t py36 -t py37 -t py38 --check .
Expand Down
11 changes: 3 additions & 8 deletions .github/workflows/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
import sys


MYPY_REGEX = (
r"^(\w:)?(?P<file>[^:]+):(?P<line>\d+):((?P<col>\d+):)?"
r"\s*(?P<type>[^:]+):\s*(?P<msg>.+)"
)
MYPY_REGEX = r"^(\w:)?(?P<file>[^:]+):(?P<line>\d+):((?P<col>\d+):)?" r"\s*(?P<type>[^:]+):\s*(?P<msg>.+)"
MYPY = re.compile(MYPY_REGEX)


def error(
file: str, line: int = 0, col: int = 0, message: str = "error", warn: bool = False
) -> None:
def error(file: str, line: int = 0, col: int = 0, message: str = "error", warn: bool = False) -> None:
"""write an error to stdout"""
kind = "warning" if warn else "error"
print(f"::{kind} file={file},line={line},col={col}::{message}")
Expand Down Expand Up @@ -43,4 +38,4 @@ def main(file_path: str) -> None:
print("you must specify a json file path!")
sys.exit(1)
file_path = sys.argv[1]
main(file_path)
main(file_path)
6 changes: 5 additions & 1 deletion examples/wxjs/main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
from django.conf.urls import patterns, url


urlpatterns = patterns("main.views", url(r"ajax/get_signature$", "jsapi_signature"), url(r"ajax/log$", "log"),)
urlpatterns = patterns(
"main.views",
url(r"ajax/get_signature$", "jsapi_signature"),
url(r"ajax/log$", "log"),
)
6 changes: 5 additions & 1 deletion examples/wxjs/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,8 @@ def jsapi_signature(request):

def log(request):
print("Hello World!")
return JsonResponse({"status": "ok",})
return JsonResponse(
{
"status": "ok",
}
)
7 changes: 6 additions & 1 deletion examples/wxjs/wxjs/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": os.path.join(BASE_DIR, "db.sqlite3"),}}
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": os.path.join(BASE_DIR, "db.sqlite3"),
}
}

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
Expand Down
21 changes: 18 additions & 3 deletions stubs/xmltodict.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
from typing import Any, Optional


class ParsingInterrupted(Exception): ...

def parse(xml_input: Any, encoding: Optional[Any] = ..., expat: Any = ..., process_namespaces: bool = ..., namespace_separator: str = ..., disable_entities: bool = ..., process_comments: bool = ..., **kwargs: Any): ...
def unparse(input_dict: Any, output: Optional[Any] = ..., encoding: str = ..., full_document: bool = ..., short_empty_elements: bool = ..., **kwargs: Any): ...
def parse(
xml_input: Any,
encoding: Optional[Any] = ...,
expat: Any = ...,
process_namespaces: bool = ...,
namespace_separator: str = ...,
disable_entities: bool = ...,
process_comments: bool = ...,
**kwargs: Any,
): ...
def unparse(
input_dict: Any,
output: Optional[Any] = ...,
encoding: str = ...,
full_document: bool = ...,
short_empty_elements: bool = ...,
**kwargs: Any,
): ...
25 changes: 20 additions & 5 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,9 @@ def test_customservice_update_account(self):

def test_customservice_delete_account(self):
with HTTMock(wechat_api_mock):
result = self.client.customservice.delete_account("test1@test",)
result = self.client.customservice.delete_account(
"test1@test",
)
self.assertEqual(0, result["errcode"])

def test_customservice_upload_headimg(self):
Expand Down Expand Up @@ -540,7 +542,9 @@ def test_jsapi_get_jsapi_add_card_params(self):
)
self.assertEqual(
JsApiCardExt(
signature="22dce6bad4db532d4a2ef82ca2ca7bbe1e10ef28", nonce_str=nonce_str, timestamp=timestamp,
signature="22dce6bad4db532d4a2ef82ca2ca7bbe1e10ef28",
nonce_str=nonce_str,
timestamp=timestamp,
),
card_params,
)
Expand Down Expand Up @@ -756,7 +760,12 @@ def test_scan_get_merchant_info(self):

def test_scan_create_product(self):
with HTTMock(wechat_api_mock):
res = self.client.scan.create_product({"keystandard": "ean13", "keystr": "6900000000000",})
res = self.client.scan.create_product(
{
"keystandard": "ean13",
"keystr": "6900000000000",
}
)
self.assertEqual("5g0B4A90aqc", res["pid"])

def test_scan_publish_product(self):
Expand Down Expand Up @@ -786,7 +795,12 @@ def test_scan_list_product(self):

def test_scan_update_product(self):
with HTTMock(wechat_api_mock):
res = self.client.scan.update_product({"keystandard": "ean13", "keystr": "6900000000000",})
res = self.client.scan.update_product(
{
"keystandard": "ean13",
"keystr": "6900000000000",
}
)
self.assertEqual("5g0B4A90aqc", res["pid"])

def test_scan_clear_product(self):
Expand All @@ -802,7 +816,8 @@ def test_scan_check_ticket(self):
def test_change_openid(self):
with HTTMock(wechat_api_mock):
res = self.client.user.change_openid(
"xxxxx", ["oEmYbwN-n24jxvk4Sox81qedINkQ", "oEmYbwH9uVd4RKJk7ZZg6SzL6tTo"],
"xxxxx",
["oEmYbwN-n24jxvk4Sox81qedINkQ", "oEmYbwH9uVd4RKJk7ZZg6SzL6tTo"],
)
self.assertEqual(2, len(res))
self.assertEqual("o2FwqwI9xCsVadFah_HtpPfaR-X4", res[0]["new_openid"])
Expand Down
10 changes: 8 additions & 2 deletions tests/test_component_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,15 @@ class ComponentOAuthTestCase(unittest.TestCase):

def setUp(self):
component = WeChatComponent(
self.component_appid, self.component_appsecret, self.component_token, self.encoding_aes_key,
self.component_appid,
self.component_appsecret,
self.component_token,
self.encoding_aes_key,
)
self.oauth = ComponentOAuth(
component,
self.app_id,
)
self.oauth = ComponentOAuth(component, self.app_id,)

def test_get_authorize_url(self):
authorize_url = self.oauth.get_authorize_url(self.redirect_uri)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_create_reply.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ def test_create_reply_with_text_render(self):
def test_create_reply_with_message(self):
from wechatpy.messages import TextMessage

msg = TextMessage({"FromUserName": "user1", "ToUserName": "user2",})
msg = TextMessage(
{
"FromUserName": "user1",
"ToUserName": "user2",
}
)
reply = create_reply("test", msg, render=False)

self.assertEqual("user1", reply.target)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ def test_check_signature_should_fail(self):

crypto = WeChatCrypto(self.token, self.encoding_aes_key, self.corp_id)
self.assertRaises(
InvalidSignatureException, crypto.check_signature, signature, timestamp, nonce, echo_str,
InvalidSignatureException,
crypto.check_signature,
signature,
timestamp,
nonce,
echo_str,
)

def test_encrypt_message(self):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_external_contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ def test_ec_del_corp_tag(self):
def test_ec_mark_tag(self):
with HTTMock(wechat_api_mock):
res = self.client.external_contact.mark_tag(
"zm", "wmm7wjCgAAkLAv_eiVt53eBokOC3_Tww", add_tag=["etm7wjCgAAD5hhvyfhPUpBbCs0CYuQMg"],
"zm",
"wmm7wjCgAAkLAv_eiVt53eBokOC3_Tww",
add_tag=["etm7wjCgAAD5hhvyfhPUpBbCs0CYuQMg"],
)
self.assertEqual(0, res["errcode"])
44 changes: 38 additions & 6 deletions tests/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,35 @@ def test_base_message(self):
def test_text_message(self):
from wechatpy.messages import TextMessage

msg = TextMessage({"Content": "test",})
msg = TextMessage(
{
"Content": "test",
}
)

self.assertEqual("test", msg.content)

def test_image_message(self):
from wechatpy.messages import ImageMessage

msg = ImageMessage({"PicUrl": "http://www.qq.com/1.png",})
msg = ImageMessage(
{
"PicUrl": "http://www.qq.com/1.png",
}
)

self.assertEqual("http://www.qq.com/1.png", msg.image)

def test_voice_message(self):
from wechatpy.messages import VoiceMessage

msg = VoiceMessage({"MediaId": "123456", "Format": "aac", "Recognition": "test",})
msg = VoiceMessage(
{
"MediaId": "123456",
"Format": "aac",
"Recognition": "test",
}
)

self.assertEqual("123456", msg.media_id)
self.assertEqual("aac", msg.format)
Expand All @@ -50,15 +64,27 @@ def test_voice_message(self):
def test_video_message(self):
from wechatpy.messages import VideoMessage

msg = VideoMessage({"MediaId": "123456", "ThumbMediaId": "123456",})
msg = VideoMessage(
{
"MediaId": "123456",
"ThumbMediaId": "123456",
}
)

self.assertEqual("123456", msg.media_id)
self.assertEqual("123456", msg.thumb_media_id)

def test_location_message(self):
from wechatpy.messages import LocationMessage

msg = LocationMessage({"Location_X": "123", "Location_Y": "456", "Scale": "1", "Label": "test",})
msg = LocationMessage(
{
"Location_X": "123",
"Location_Y": "456",
"Scale": "1",
"Label": "test",
}
)

self.assertEqual("123", msg.location_x)
self.assertEqual("456", msg.location_y)
Expand All @@ -68,7 +94,13 @@ def test_location_message(self):
def test_link_message(self):
from wechatpy.messages import LinkMessage

msg = LinkMessage({"Title": "test", "Description": "test", "Url": "http://www.qq.com",})
msg = LinkMessage(
{
"Title": "test",
"Description": "test",
"Url": "http://www.qq.com",
}
)

self.assertEqual("test", msg.title)
self.assertEqual("test", msg.description)
Expand Down
18 changes: 15 additions & 3 deletions tests/test_payment.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,20 @@ def wechat_api_mock(url, request):

class WeChatPayTestCase(unittest.TestCase):
def setUp(self):
self.client = WeChatPay(appid="abc1234", api_key="test123", mch_id="1192221", mch_cert="", mch_key="",)
self.client = WeChatPay(
appid="abc1234",
api_key="test123",
mch_id="1192221",
mch_cert="",
mch_key="",
)

def test_apply_signing(self):
response = self.client.withhold.apply_signing(
plan_id="t1234", contract_code="w1111", contract_display_account="测试", notify_url="",
plan_id="t1234",
contract_code="w1111",
contract_display_account="测试",
notify_url="",
)
self.assertIn("base_url", response)
self.assertIn("data", response)
Expand All @@ -60,5 +69,8 @@ def test_query_order(self):

def test_apply_cancel_signing(self):
with HTTMock(wechat_api_mock):
response = self.client.withhold.apply_cancel_signing(plan_id="t1234", contract_code="w1111",)
response = self.client.withhold.apply_cancel_signing(
plan_id="t1234",
contract_code="w1111",
)
self.assertEqual(response["result_code"], "SUCCESS")
6 changes: 5 additions & 1 deletion tests/test_replies.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def test_music_reply_properties(self):
self.assertTrue(reply.hq_music_url is None)

reply = MusicReply(
thumb_media_id="123456", title="test", description="test", music_url="http://www.qq.com", hq_music_url=None,
thumb_media_id="123456",
title="test",
description="test",
music_url="http://www.qq.com",
hq_music_url=None,
)
self.assertEqual("123456", reply.thumb_media_id)
self.assertEqual("test", reply.title)
Expand Down
16 changes: 13 additions & 3 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ def test_check_signature_should_fail(self):
timestamp = "1410685589"
nonce = "test"
self.assertRaises(
InvalidSignatureException, check_signature, token, signature, timestamp, nonce,
InvalidSignatureException,
check_signature,
token,
signature,
timestamp,
nonce,
)

def test_check_wxa_signature(self):
Expand All @@ -56,7 +61,11 @@ def test_check_wxa_signature(self):

client_signature = "fake_sign"
self.assertRaises(
InvalidSignatureException, check_wxa_signature, session_key, raw_data, client_signature,
InvalidSignatureException,
check_wxa_signature,
session_key,
raw_data,
client_signature,
)

# 带中文的示例
Expand Down Expand Up @@ -86,5 +95,6 @@ def test_rsa_encrypt_decrypt(self):
) as private_fp:
encrypted_string = rsa_encrypt(target_string, public_fp.read(), b64_encode=False)
self.assertEqual(
rsa_decrypt(encrypted_string, private_fp.read()), target_string.encode("utf-8"),
rsa_decrypt(encrypted_string, private_fp.read()),
target_string.encode("utf-8"),
)

0 comments on commit ce9c514

Please sign in to comment.