Skip to content

Commit e4e3256

Browse files
authored
refactor: modernize type hints and enable ruff UP (#12)
refactor: modernize type hints and enable ruff UP
2 parents 7fa8579 + b6f6e5d commit e4e3256

35 files changed

Lines changed: 491 additions & 519 deletions

postmark/clients/account_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import sys
44
import uuid
5-
from typing import Any, Dict, List, Optional, Union
5+
from typing import Any
66

77
import httpx
88
from tenacity import (
@@ -40,7 +40,7 @@ def __init__(
4040
account_token: str,
4141
retries: int = 3,
4242
timeout: float = 30.0,
43-
base_url: Optional[str] = None,
43+
base_url: str | None = None,
4444
):
4545
"""
4646
Initialize the Postmark Account Client.
@@ -187,28 +187,28 @@ async def request(self, method: str, endpoint: str, **kwargs) -> httpx.Response:
187187
raise AssertionError("The Postmark API is unreachable.")
188188

189189
async def get(
190-
self, endpoint: str, params: Optional[Dict[str, Any]] = None
190+
self, endpoint: str, params: dict[str, Any] | None = None
191191
) -> httpx.Response:
192192
return await self.request("GET", endpoint, params=params)
193193

194194
async def post(
195195
self,
196196
endpoint: str,
197-
json: Union[Dict[str, Any], List[Dict[str, Any]], None] = None,
197+
json: dict[str, Any] | list[dict[str, Any]] | None = None,
198198
) -> httpx.Response:
199199
return await self.request("POST", endpoint, json=json)
200200

201201
async def put(
202-
self, endpoint: str, json: Optional[Dict[str, Any]] = None
202+
self, endpoint: str, json: dict[str, Any] | None = None
203203
) -> httpx.Response:
204204
return await self.request("PUT", endpoint, json=json)
205205

206206
async def patch(
207-
self, endpoint: str, json: Optional[Dict[str, Any]] = None
207+
self, endpoint: str, json: dict[str, Any] | None = None
208208
) -> httpx.Response:
209209
return await self.request("PATCH", endpoint, json=json)
210210

211211
async def delete(
212-
self, endpoint: str, params: Optional[Dict[str, Any]] = None
212+
self, endpoint: str, params: dict[str, Any] | None = None
213213
) -> httpx.Response:
214214
return await self.request("DELETE", endpoint, params=params)

postmark/clients/server_client.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33
import sys
44
import uuid
5-
from typing import Any, Dict, List, Optional, Union
5+
from typing import Any
66

77
import httpx
88
from tenacity import (
@@ -45,7 +45,7 @@ def __init__(
4545
server_token: str,
4646
retries: int = 3,
4747
timeout: float = 5,
48-
base_url: Optional[str] = None,
48+
base_url: str | None = None,
4949
):
5050
"""
5151
Initialize the Postmark Server Client.
@@ -202,28 +202,28 @@ async def request(self, method: str, endpoint: str, **kwargs) -> httpx.Response:
202202
raise AssertionError("The Postmark API is unreachable.")
203203

204204
async def get(
205-
self, endpoint: str, params: Optional[Dict[str, Any]] = None
205+
self, endpoint: str, params: dict[str, Any] | None = None
206206
) -> httpx.Response:
207207
return await self.request("GET", endpoint, params=params)
208208

209209
async def post(
210210
self,
211211
endpoint: str,
212-
json: Union[Dict[str, Any], List[Dict[str, Any]], None] = None,
212+
json: dict[str, Any] | list[dict[str, Any]] | None = None,
213213
) -> httpx.Response:
214214
return await self.request("POST", endpoint, json=json)
215215

216216
async def put(
217-
self, endpoint: str, json: Optional[Dict[str, Any]] = None
217+
self, endpoint: str, json: dict[str, Any] | None = None
218218
) -> httpx.Response:
219219
return await self.request("PUT", endpoint, json=json)
220220

221221
async def patch(
222-
self, endpoint: str, json: Optional[Dict[str, Any]] = None
222+
self, endpoint: str, json: dict[str, Any] | None = None
223223
) -> httpx.Response:
224224
return await self.request("PATCH", endpoint, json=json)
225225

226226
async def delete(
227-
self, endpoint: str, params: Optional[Dict[str, Any]] = None
227+
self, endpoint: str, params: dict[str, Any] | None = None
228228
) -> httpx.Response:
229229
return await self.request("DELETE", endpoint, params=params)

postmark/exceptions.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Postmark API exceptions."""
22

33
import re
4-
from typing import Any, Optional
4+
from typing import Any
55

66

77
class PostmarkException(Exception):
@@ -10,8 +10,8 @@ class PostmarkException(Exception):
1010
def __init__(
1111
self,
1212
message: str,
13-
error_code: Optional[int] = None,
14-
http_status: Optional[int] = None,
13+
error_code: int | None = None,
14+
http_status: int | None = None,
1515
):
1616
super().__init__(message)
1717
self.error_code = error_code
@@ -51,7 +51,7 @@ def __init__(
5151
message: str,
5252
error_code: int,
5353
http_status: int,
54-
request_id: Optional[str] = None,
54+
request_id: str | None = None,
5555
):
5656
super().__init__(message, error_code, http_status)
5757
self.request_id = request_id
@@ -75,7 +75,7 @@ def __init__(
7575
message: str,
7676
error_code: int,
7777
http_status: int,
78-
request_id: Optional[str] = None,
78+
request_id: str | None = None,
7979
):
8080
super().__init__(message, error_code, http_status, request_id)
8181
match = re.search(r"Found inactive addresses: ([^.]+)", message)

postmark/models/bounces/manager.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
2+
from collections.abc import AsyncGenerator
23
from datetime import datetime
3-
from typing import AsyncGenerator, Optional
44

55
from postmark.models.page import Page
66
from postmark.utils.pagination import paginate
@@ -33,14 +33,14 @@ async def list(
3333
self,
3434
count: int = 100,
3535
offset: int = 0,
36-
type: Optional[BounceType] = None,
37-
inactive: Optional[bool] = None,
38-
email_filter: Optional[str] = None,
39-
tag: Optional[str] = None,
40-
message_id: Optional[str] = None,
41-
from_date: Optional[datetime] = None,
42-
to_date: Optional[datetime] = None,
43-
message_stream: Optional[str] = None,
36+
type: BounceType | None = None,
37+
inactive: bool | None = None,
38+
email_filter: str | None = None,
39+
tag: str | None = None,
40+
message_id: str | None = None,
41+
from_date: datetime | None = None,
42+
to_date: datetime | None = None,
43+
message_stream: str | None = None,
4444
) -> Page[Bounce]:
4545
"""
4646
List bounces for the server.
@@ -90,14 +90,14 @@ async def stream(
9090
self,
9191
batch_size: int = 500,
9292
max_bounces: int = 1000,
93-
type: Optional[BounceType] = None,
94-
inactive: Optional[bool] = None,
95-
email_filter: Optional[str] = None,
96-
tag: Optional[str] = None,
97-
message_id: Optional[str] = None,
98-
from_date: Optional[datetime] = None,
99-
to_date: Optional[datetime] = None,
100-
message_stream: Optional[str] = None,
93+
type: BounceType | None = None,
94+
inactive: bool | None = None,
95+
email_filter: str | None = None,
96+
tag: str | None = None,
97+
message_id: str | None = None,
98+
from_date: datetime | None = None,
99+
to_date: datetime | None = None,
100+
message_stream: str | None = None,
101101
) -> AsyncGenerator[Bounce, None]:
102102
"""Yield bounces with automatic pagination."""
103103
async for bounce in paginate(

postmark/models/bounces/schemas.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
from typing import List, Optional
32

43
from pydantic import BaseModel, ConfigDict, EmailStr, Field
54

@@ -13,7 +12,7 @@ class Bounce(BaseModel):
1312
type: BounceType = Field(alias="Type")
1413
type_code: int = Field(alias="TypeCode")
1514
name: str = Field(alias="Name")
16-
tag: Optional[str] = Field(None, alias="Tag")
15+
tag: str | None = Field(None, alias="Tag")
1716
message_id: str = Field(alias="MessageID")
1817
server_id: int = Field(alias="ServerID")
1918
message_stream: str = Field(alias="MessageStream")
@@ -26,7 +25,7 @@ class Bounce(BaseModel):
2625
inactive: bool = Field(alias="Inactive")
2726
can_activate: bool = Field(alias="CanActivate")
2827
subject: str = Field(alias="Subject")
29-
content: Optional[str] = Field(None, alias="Content")
28+
content: str | None = Field(None, alias="Content")
3029

3130
model_config = ConfigDict(populate_by_name=True)
3231

@@ -46,7 +45,7 @@ class DeliveryStats(BaseModel):
4645
"""Response from ``GET /deliverystats``"""
4746

4847
inactive_mails: int = Field(alias="InactiveMails")
49-
bounces: List[BounceTypeCount] = Field(alias="Bounces")
48+
bounces: list[BounceTypeCount] = Field(alias="Bounces")
5049

5150
model_config = ConfigDict(populate_by_name=True)
5251

@@ -55,7 +54,7 @@ class BouncesListResponse(BaseModel):
5554
"""Response from ``GET /bounces``"""
5655

5756
total_count: int = Field(alias="TotalCount")
58-
bounces: List[Bounce] = Field(alias="Bounces")
57+
bounces: list[Bounce] = Field(alias="Bounces")
5958

6059
model_config = ConfigDict(populate_by_name=True)
6160

postmark/models/domains/manager.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import Optional
2-
31
from postmark.models.page import Page
42
from postmark.utils.types import HTTPClient
53

@@ -42,7 +40,7 @@ async def get(self, domain_id: int) -> Domain:
4240
async def create(
4341
self,
4442
name: str,
45-
return_path_domain: Optional[str] = None,
43+
return_path_domain: str | None = None,
4644
) -> Domain:
4745
"""
4846
Create a new domain on the account.
@@ -62,7 +60,7 @@ async def create(
6260
async def edit(
6361
self,
6462
domain_id: int,
65-
return_path_domain: Optional[str] = None,
63+
return_path_domain: str | None = None,
6664
) -> Domain:
6765
"""
6866
Update a domain.

postmark/models/domains/schemas.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from typing import List, Optional
2-
31
from pydantic import BaseModel, ConfigDict, Field
42

53

@@ -22,25 +20,25 @@ class Domain(BaseModel):
2220
id: int = Field(alias="ID")
2321
name: str = Field(alias="Name")
2422
spf_verified: bool = Field(False, alias="SPFVerified") # deprecated by Postmark
25-
spf_host: Optional[str] = Field(None, alias="SPFHost")
26-
spf_text_value: Optional[str] = Field(None, alias="SPFTextValue")
23+
spf_host: str | None = Field(None, alias="SPFHost")
24+
spf_text_value: str | None = Field(None, alias="SPFTextValue")
2725
dkim_verified: bool = Field(alias="DKIMVerified")
2826
weak_dkim: bool = Field(alias="WeakDKIM")
29-
dkim_host: Optional[str] = Field(None, alias="DKIMHost")
30-
dkim_text_value: Optional[str] = Field(None, alias="DKIMTextValue")
31-
dkim_pending_host: Optional[str] = Field(None, alias="DKIMPendingHost")
32-
dkim_pending_text_value: Optional[str] = Field(None, alias="DKIMPendingTextValue")
33-
dkim_revoked_host: Optional[str] = Field(None, alias="DKIMRevokedHost")
34-
dkim_revoked_text_value: Optional[str] = Field(None, alias="DKIMRevokedTextValue")
35-
safe_to_remove_revoked_key_from_dns: Optional[bool] = Field(
27+
dkim_host: str | None = Field(None, alias="DKIMHost")
28+
dkim_text_value: str | None = Field(None, alias="DKIMTextValue")
29+
dkim_pending_host: str | None = Field(None, alias="DKIMPendingHost")
30+
dkim_pending_text_value: str | None = Field(None, alias="DKIMPendingTextValue")
31+
dkim_revoked_host: str | None = Field(None, alias="DKIMRevokedHost")
32+
dkim_revoked_text_value: str | None = Field(None, alias="DKIMRevokedTextValue")
33+
safe_to_remove_revoked_key_from_dns: bool | None = Field(
3634
None, alias="SafeToRemoveRevokedKeyFromDNS"
3735
)
38-
dkim_update_status: Optional[str] = Field(None, alias="DKIMUpdateStatus")
39-
return_path_domain: Optional[str] = Field(None, alias="ReturnPathDomain")
40-
return_path_domain_verified: Optional[bool] = Field(
36+
dkim_update_status: str | None = Field(None, alias="DKIMUpdateStatus")
37+
return_path_domain: str | None = Field(None, alias="ReturnPathDomain")
38+
return_path_domain_verified: bool | None = Field(
4139
None, alias="ReturnPathDomainVerified"
4240
)
43-
return_path_domain_cname_value: Optional[str] = Field(
41+
return_path_domain_cname_value: str | None = Field(
4442
None, alias="ReturnPathDomainCNAMEValue"
4543
)
4644

@@ -51,7 +49,7 @@ class DomainsListResponse(BaseModel):
5149
"""Response from ``GET /domains``."""
5250

5351
total_count: int = Field(alias="TotalCount")
54-
domains: List[DomainListItem] = Field(alias="Domains")
52+
domains: list[DomainListItem] = Field(alias="Domains")
5553

5654
model_config = ConfigDict(populate_by_name=True)
5755

postmark/models/inbound/manager.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from datetime import datetime
2-
from typing import Any, Dict, Optional
2+
from typing import Any
33

44
from postmark.models.page import Page
55
from postmark.utils.types import HTTPClient
@@ -15,22 +15,22 @@ async def list(
1515
self,
1616
count: int = 100,
1717
offset: int = 0,
18-
recipient: Optional[str] = None,
19-
from_email: Optional[str] = None,
20-
tag: Optional[str] = None,
21-
subject: Optional[str] = None,
22-
mailbox_hash: Optional[str] = None,
23-
status: Optional[str] = None,
24-
from_date: Optional[datetime] = None,
25-
to_date: Optional[datetime] = None,
18+
recipient: str | None = None,
19+
from_email: str | None = None,
20+
tag: str | None = None,
21+
subject: str | None = None,
22+
mailbox_hash: str | None = None,
23+
status: str | None = None,
24+
from_date: datetime | None = None,
25+
to_date: datetime | None = None,
2626
) -> Page[InboundMessage]:
2727
"""List inbound messages."""
2828
if count > 500:
2929
raise ValueError("Count cannot exceed 500 per request")
3030
if count + offset > 10000:
3131
raise ValueError("Count + Offset cannot exceed 10,000")
3232

33-
params: Dict[str, Any] = {"count": count, "offset": offset}
33+
params: dict[str, Any] = {"count": count, "offset": offset}
3434

3535
if recipient is not None:
3636
params["recipient"] = recipient

0 commit comments

Comments
 (0)