Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pathlike objects in upload util #1656

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

allen-pattern
Copy link

Summary

Allow passing Path objects into file upload functions.

Testing

Attempt to pass a Path object to one of the file upload functions that takes a string path. Also pass a string path to ensure there are no regressions.

Category

  • 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 (Documents)
  • /tutorial (PythOnBoardingBot tutorial)
  • tests/integration_tests (Automated tests for this library)

Requirements

  • 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.

I did get S3 test failures with the latter (without my change), FYI.

Copy link
Contributor

@WilliamBergamin WilliamBergamin left a comment

Choose a reason for hiding this comment

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

This looks like a nice enhancement 💯 thank you!
Any chance you could add unit tests for file passed as str and os.PathLike? if it is not trivial let me know I will take a look

@WilliamBergamin WilliamBergamin added enhancement M-T: A feature request for new functionality semver:patch web-client labels Feb 18, 2025
@WilliamBergamin WilliamBergamin added this to the 3.35.1 milestone Feb 18, 2025
Copy link

codecov bot commented Feb 18, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 85.35%. Comparing base (d6c206a) to head (fe8d087).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1656      +/-   ##
==========================================
+ Coverage   85.33%   85.35%   +0.02%     
==========================================
  Files         113      113              
  Lines       12802    12802              
==========================================
+ Hits        10924    10927       +3     
+ Misses       1878     1875       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Also switch to inbuilt filename encode helper, since it accepts paths
@allen-pattern
Copy link
Author

Added in fe8d087, and it's a good thing you suggested it, because the test failure also prompted me to fix implicit filename handling a few lines down.

Copy link
Contributor

@WilliamBergamin WilliamBergamin left a comment

Choose a reason for hiding this comment

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

Thank you for adding the unit test 💯 I left one nit comment

I was able to test this out manually with the following and it works as expected 🟢

import os
from pathlib import Path

from slack_sdk import WebClient

client = WebClient(
    token=os.environ.get("SLACK_BOT_TOKEN"),
)

current_dir = os.path.dirname(__file__)
file = Path(f"{current_dir}/data/slack_logo.png")
upload = client.files_upload_v2(
    channel="C1234",
    title="Good Old Slack Logo",
    filename="slack_logo.png",
    file=file,
)

But I think most static type checkers like mypy will reject this with the following error

error: Argument "file" to "files_upload_v2" of "WebClient" has incompatible type "Path"; expected "str | bytes | IOBase | None"  [arg-type]

We would need to

  1. Update the type of the file argument in WebClient.files_upload_v2
  2. run python scripts/codegen.py to propagate the changes to WebClient, AsyncWebClient and LegacyWebClient
  3. Write proper unit tests for those changes

If you do not want to do these changes we can merge this PR as is and I can follow up with a PR for type support

@@ -101,6 +102,13 @@ def test_files_upload_v2_issue_1356(self):
file_io_item = _to_v2_file_upload_item({"file": file_io, "filename": "foo.txt"})
assert file_io_item.get("filename") == "foo.txt"

def test_files_upload_v2_paths(self):
Copy link
Contributor

@WilliamBergamin WilliamBergamin Feb 28, 2025

Choose a reason for hiding this comment

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

NIT: maybe this could be name this something more descriptive like

Suggested change
def test_files_upload_v2_paths(self):
def test_to_v2_file_upload_item_can_accept_file_as_path(self):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla:signed enhancement M-T: A feature request for new functionality semver:patch web-client
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants