From 39455b60e847438793a5b1302de25dd90fe6f14c Mon Sep 17 00:00:00 2001 From: mayankbohradev Date: Wed, 18 Nov 2026 18:01:05 +0530 Subject: [PATCH] fix(topics): add visibility to topic types The API accepts `visibility` on create and update, and returns it on get and list. The SDK already sends and returns the value, but the types rejected it, so mypy reported documented usage as an error. - Add `TopicVisibility` ("public" or "private"). - Add `visibility` to `Topics.CreateParams` and `Topics.UpdateParams`. - Add `visibility` to `Topic`. Tested with tox lint, mypy and py (874 passed). --- examples/topics.py | 1 + resend/__init__.py | 3 +- resend/topics/_topic.py | 8 +++- resend/topics/_topics.py | 15 ++++++- resend/version.py | 2 +- tests/topics_async_test.py | 91 +++++++++++++++++++++++++++++++++++++ tests/topics_test.py | 92 ++++++++++++++++++++++++++++++++++++++ 7 files changed, 208 insertions(+), 4 deletions(-) diff --git a/examples/topics.py b/examples/topics.py index 40f954e..ecb1e09 100644 --- a/examples/topics.py +++ b/examples/topics.py @@ -11,6 +11,7 @@ "name": "Weekly Newsletter", "default_subscription": "opt_in", "description": "Subscribe to our weekly newsletter for updates", + "visibility": "public", } topic: resend.Topics.CreateTopicResponse = resend.Topics.create(create_params) diff --git a/resend/__init__.py b/resend/__init__.py index 6e5aa88..25f2d84 100644 --- a/resend/__init__.py +++ b/resend/__init__.py @@ -88,7 +88,7 @@ SuppressionsBatch) from .templates._template import Template, TemplateListItem, Variable from .templates._templates import Templates -from .topics._topic import Topic +from .topics._topic import Topic, TopicVisibility from .topics._topics import Topics from .usage._usage import (AiCreditsUsage, AutomationRunsUsage, BroadcastsUsage, ContactsUsage, DomainsUsage, @@ -325,6 +325,7 @@ "InboxDraftListItem", "InboxDraftType", "Topic", + "TopicVisibility", "OAuthGrant", "OAuthGrantClient", "BatchValidationError", diff --git a/resend/topics/_topic.py b/resend/topics/_topic.py index 15d40f9..bdeb3ab 100644 --- a/resend/topics/_topic.py +++ b/resend/topics/_topic.py @@ -1,4 +1,6 @@ -from typing_extensions import TypedDict +from typing_extensions import Literal, NotRequired, TypedDict + +TopicVisibility = Literal["public", "private"] class Topic(TypedDict): @@ -18,6 +20,10 @@ class Topic(TypedDict): """ The topic description. """ + visibility: NotRequired[TopicVisibility] + """ + The visibility of the topic on the unsubscribe page. Possible values: public or private. + """ created_at: str """ The date and time the topic was created. diff --git a/resend/topics/_topics.py b/resend/topics/_topics.py index 8d05550..cb48062 100644 --- a/resend/topics/_topics.py +++ b/resend/topics/_topics.py @@ -12,7 +12,7 @@ except ImportError: pass -from ._topic import Topic +from ._topic import Topic, TopicVisibility class Topics: @@ -44,6 +44,13 @@ class CreateParams(TypedDict): """ The topic description. Max length is 200 characters. """ + visibility: NotRequired[TopicVisibility] + """ + The visibility of the topic on the unsubscribe page. Possible values: public or private. + private: only contacts who are opted in to the topic can see it. + public: all contacts can see it. + Defaults to private when not provided. + """ class UpdateTopicResponse(BaseResponse): """ @@ -67,6 +74,10 @@ class UpdateParams(TypedDict, total=False): """ The topic description. Max length is 200 characters. """ + visibility: TopicVisibility + """ + The visibility of the topic on the unsubscribe page. Possible values: public or private. + """ class RemoveTopicResponse(BaseResponse): """ @@ -143,6 +154,7 @@ def create(cls, params: CreateParams) -> CreateTopicResponse: - name: The topic name (max 50 characters) - default_subscription: The default subscription preference ("opt_in" or "opt_out") - description: Optional topic description (max 200 characters) + - visibility: Optional topic visibility ("public" or "private") Returns: CreateTopicResponse: The created topic response with the topic ID @@ -183,6 +195,7 @@ def update(cls, id: str, params: UpdateParams) -> UpdateTopicResponse: params (UpdateParams): The topic update parameters - name: Optional topic name (max 50 characters) - description: Optional topic description (max 200 characters) + - visibility: Optional topic visibility ("public" or "private") Returns: UpdateTopicResponse: The updated topic response with the topic ID diff --git a/resend/version.py b/resend/version.py index 5cd4c2a..4e3219f 100644 --- a/resend/version.py +++ b/resend/version.py @@ -1,4 +1,4 @@ -__version__ = "2.54.0" +__version__ = "2.54.1" def get_version() -> str: diff --git a/tests/topics_async_test.py b/tests/topics_async_test.py index 9ea0d88..a2a690c 100644 --- a/tests/topics_async_test.py +++ b/tests/topics_async_test.py @@ -1,4 +1,7 @@ +from unittest.mock import AsyncMock + import pytest +from typing_extensions import assert_type import resend from resend.exceptions import NoContentError @@ -138,3 +141,91 @@ async def test_should_list_topics_async_raise_exception_when_no_content( self.set_mock_json(None) with pytest.raises(NoContentError): _ = await resend.Topics.list_async() + + async def test_topics_get_async_returns_visibility(self) -> None: + self.set_mock_json( + { + "object": "topic", + "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e", + "name": "Weekly Newsletter", + "description": "Weekly newsletter for our subscribers", + "default_subscription": "opt_in", + "visibility": "public", + "created_at": "2026-04-08 00:11:13.110779+00", + } + ) + + topic = await resend.Topics.get_async(id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e") + assert_type(topic["visibility"], resend.TopicVisibility) + assert topic["visibility"] == "public" + + async def test_topics_list_async_returns_visibility(self) -> None: + self.set_mock_json( + { + "object": "list", + "has_more": False, + "data": [ + { + "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e", + "name": "Weekly Newsletter", + "description": "Weekly newsletter for our subscribers", + "default_subscription": "opt_in", + "visibility": "public", + "created_at": "2026-04-08 00:11:13.110779+00", + } + ], + } + ) + + topics = await resend.Topics.list_async() + assert_type(topics["data"][0]["visibility"], resend.TopicVisibility) + assert topics["data"][0]["visibility"] == "public" + + +class TestTopicsRequestBodyAsync: + def setup_method(self) -> None: + self.previous_api_key = resend.api_key + resend.api_key = "re_123" + self.mock_client = AsyncMock() + self.mock_client.request.return_value = ( + b'{"object": "topic", "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e"}', + 200, + {"content-type": "application/json"}, + ) + self.previous_async_http_client = resend.default_async_http_client + resend.default_async_http_client = self.mock_client + + def teardown_method(self) -> None: + resend.default_async_http_client = self.previous_async_http_client + resend.api_key = self.previous_api_key + + async def test_create_async_sends_visibility(self) -> None: + params: resend.Topics.CreateParams = { + "name": "Weekly Newsletter", + "default_subscription": "opt_in", + "visibility": "public", + } + await resend.Topics.create_async(params) + + _, kwargs = self.mock_client.request.call_args + assert kwargs["method"] == "post" + assert kwargs["url"] == f"{resend.api_url}/topics" + assert kwargs["json"] == { + "name": "Weekly Newsletter", + "default_subscription": "opt_in", + "visibility": "public", + } + + async def test_update_async_sends_visibility(self) -> None: + params: resend.Topics.UpdateParams = {"visibility": "private"} + await resend.Topics.update_async( + id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e", params=params + ) + + _, kwargs = self.mock_client.request.call_args + assert kwargs["method"] == "patch" + assert ( + kwargs["url"] + == f"{resend.api_url}/topics/b6d24b8e-af0b-4c3c-be0c-359bbd97381e" + ) + assert kwargs["json"] == {"visibility": "private"} diff --git a/tests/topics_test.py b/tests/topics_test.py index 8099c85..ced62c7 100644 --- a/tests/topics_test.py +++ b/tests/topics_test.py @@ -1,5 +1,11 @@ +from unittest import TestCase +from unittest.mock import create_autospec + +from typing_extensions import assert_type + import resend from resend.exceptions import NoContentError +from resend.http_client import HTTPClient from tests.conftest import ResendBaseTest # flake8: noqa @@ -237,3 +243,89 @@ def test_topics_list_with_before_param(self) -> None: assert topics["has_more"] is False assert len(topics["data"]) == 1 assert topics["data"][0]["id"] == "topic-3" + + def test_topics_get_returns_visibility(self) -> None: + self.set_mock_json( + { + "object": "topic", + "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e", + "name": "Weekly Newsletter", + "description": "Weekly newsletter for our subscribers", + "default_subscription": "opt_in", + "visibility": "public", + "created_at": "2026-04-08 00:11:13.110779+00", + } + ) + + topic = resend.Topics.get(id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e") + assert_type(topic["visibility"], resend.TopicVisibility) + assert topic["visibility"] == "public" + + def test_topics_list_returns_visibility(self) -> None: + self.set_mock_json( + { + "object": "list", + "has_more": False, + "data": [ + { + "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e", + "name": "Weekly Newsletter", + "description": "Weekly newsletter for our subscribers", + "default_subscription": "opt_in", + "visibility": "public", + "created_at": "2026-04-08 00:11:13.110779+00", + } + ], + } + ) + + topics = resend.Topics.list() + assert_type(topics["data"][0]["visibility"], resend.TopicVisibility) + assert topics["data"][0]["visibility"] == "public" + + +class TestTopicsRequestBody(TestCase): + def setUp(self) -> None: + self.previous_api_key = resend.api_key + resend.api_key = "re_123" + self.mock_client = create_autospec(HTTPClient, instance=True) + self.mock_client.request.return_value = ( + b'{"object": "topic", "id": "b6d24b8e-af0b-4c3c-be0c-359bbd97381e"}', + 200, + {"Content-Type": "application/json"}, + ) + self.previous_http_client = resend.default_http_client + resend.default_http_client = self.mock_client + + def tearDown(self) -> None: + resend.default_http_client = self.previous_http_client + resend.api_key = self.previous_api_key + + def test_create_sends_visibility(self) -> None: + params: resend.Topics.CreateParams = { + "name": "Weekly Newsletter", + "default_subscription": "opt_in", + "visibility": "public", + } + resend.Topics.create(params) + + _, kwargs = self.mock_client.request.call_args + assert kwargs["method"] == "post" + assert kwargs["url"] == f"{resend.api_url}/topics" + assert kwargs["json"] == { + "name": "Weekly Newsletter", + "default_subscription": "opt_in", + "visibility": "public", + } + + def test_update_sends_visibility(self) -> None: + params: resend.Topics.UpdateParams = {"visibility": "private"} + resend.Topics.update(id="b6d24b8e-af0b-4c3c-be0c-359bbd97381e", params=params) + + _, kwargs = self.mock_client.request.call_args + assert kwargs["method"] == "patch" + assert ( + kwargs["url"] + == f"{resend.api_url}/topics/b6d24b8e-af0b-4c3c-be0c-359bbd97381e" + ) + assert kwargs["json"] == {"visibility": "private"}