Skip to content

Enable pytype and fix detected issues#1128

Merged
seratch merged 1 commit intoslackapi:mainfrom
seratch:enabe-pytype
Oct 19, 2021
Merged

Enable pytype and fix detected issues#1128
seratch merged 1 commit intoslackapi:mainfrom
seratch:enabe-pytype

Conversation

@seratch
Copy link
Copy Markdown
Contributor

@seratch seratch commented Oct 17, 2021

Summary

This pull request enables pytype (https://github.com/google/pytype) in this project's CI builds. Also, it resolves the issues detected by pytype. No critical bugs are detected but the internals are much improved.

Category (place an x in each of the [ ])

  • slack_sdk.web.WebClient (sync/async) (Web API client)
  • slack_sdk.webhook.WebhookClient (sync/async) (Incoming Webhook, response_url sender)
  • slack_sdk.socket_mode (Socket Mode client)
  • slack_sdk.signature (Request Signature Verifier)
  • slack_sdk.oauth (OAuth Flow Utilities)
  • slack_sdk.models (UI component builders)
  • slack_sdk.scim (SCIM API client)
  • slack_sdk.audit_logs (Audit Logs API client)
  • slack_sdk.rtm_v2 (RTM client)
  • /docs-src (Documents, have you run ./scripts/docs.sh?)
  • /docs-src-v2 (Documents, have you run ./scripts/docs-v2.sh?)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements (place an x in each [ ])

  • I've read and understood the Contributing Guidelines and have done my best effort to follow them.
  • I've read and agree to the Code of Conduct.
  • I've run python3 -m venv .venv && source .venv/bin/activate && ./scripts/run_validation.sh after making the changes.

@codecov
Copy link
Copy Markdown

codecov bot commented Oct 17, 2021

Codecov Report

Merging #1128 (58df9d6) into main (6ce55a3) will decrease coverage by 0.11%.
The diff coverage is 89.03%.

❗ Current head 58df9d6 differs from pull request most recent head 5ab926e. Consider uploading reports for the commit 5ab926e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1128      +/-   ##
==========================================
- Coverage   88.10%   87.99%   -0.12%     
==========================================
  Files         110      110              
  Lines       10681    10708      +27     
==========================================
+ Hits         9410     9422      +12     
- Misses       1271     1286      +15     
Impacted Files Coverage Δ
slack_sdk/audit_logs/v1/async_client.py 89.16% <ø> (ø)
slack_sdk/models/blocks/blocks.py 96.26% <ø> (ø)
slack_sdk/socket_mode/request.py 72.72% <0.00%> (-2.28%) ⬇️
slack_sdk/web/async_base_client.py 98.33% <ø> (ø)
slack_sdk/socket_mode/client.py 76.99% <50.00%> (ø)
slack_sdk/socket_mode/websocket_client/__init__.py 84.17% <50.00%> (-1.44%) ⬇️
slack_sdk/socket_mode/builtin/internals.py 72.00% <55.55%> (-0.65%) ⬇️
...dk/oauth/installation_store/models/installation.py 89.89% <62.50%> (-0.83%) ⬇️
slack_sdk/web/legacy_base_client.py 87.29% <73.33%> (-0.63%) ⬇️
slack_sdk/web/base_client.py 88.67% <75.00%> (-0.27%) ⬇️
... and 36 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update db6621c...5ab926e. Read the comment docs.

self,
*,
query_params: Optional[Dict[str, any]] = None,
query_params: Optional[Dict[str, Any]] = None,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using any works in the Python syntax but pytype recommends using typing.Any instead.

status_code=e.code,
raw_body=response_body,
headers=e.headers,
headers=dict(e.headers.items()),
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HTTPError#headers returns an email.Message object, which mostly behaves like a dict. By changing this, the headers works as a plain dict now.

if e.code == 429:
# for backward-compatibility with WebClient (v.2.5.0 or older)
resp.headers["Retry-After"] = resp.headers["retry-after"]
if "retry-after" not in resp.headers:
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the resp.headers here is consistently a plain dict, these changes are required.


# NOTE: BAN-B310 is already checked above
resp: Optional[HTTPResponse] = None
http_resp: Optional[HTTPResponse] = None
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reusing resp for two types (HTTPResponse and AuditLogsResponse) is not a recommended practice in general. pytype detected this potential issue.

}

fields: List[AttachmentField]
actions: List[Action]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary one

v = (sec_websocket_key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode("utf-8")
expected = encodebytes(hashlib.sha1(v).digest()).decode("utf-8").strip()
actual = headers.get("sec-websocket-accept").strip()
actual = headers.get("sec-websocket-accept", "").strip()
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pytype detected a potential bug here (usually the header value always exists, though)

return "ping"
if opcode == FrameHeader.OPCODE_PONG:
return "pong"
return "-"
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as this method return str not Optional[str], the default should be returned this way. "-" instead of None here does not break anything.

api_url: str,
req_args: dict,
# set the default to an empty array for legacy clients
retry_handlers: List[AsyncRetryHandler] = [],
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using [] / {} for method arg default value is not recommended (the value is cached and reused for the following method calls)

token: str = None,
token: Optional[str] = None,
url: str,
query_params: Dict[str, str] = {},
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not break anything as this is an internal method, which is used only inside this class

async def _perform_http_request(
self, *, body: Dict[str, Any], headers: Dict[str, str]
) -> WebhookResponse:
body = json.dumps(body)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reusing the same variable name is not a great coding practice here (pytype detected the type error for RetryHandler initialization)

@seratch seratch force-pushed the enabe-pytype branch 3 times, most recently from 86b5448 to ac96f77 Compare October 17, 2021 23:47
@seratch seratch merged commit 77e906a into slackapi:main Oct 19, 2021
@seratch seratch deleted the enabe-pytype branch October 19, 2021 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant