Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions mailtrap/client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import warnings
from typing import Optional
from typing import Union
from typing import cast

import importlib.metadata

from typing import Optional, Union, cast

from pydantic import TypeAdapter

Expand Down Expand Up @@ -34,6 +35,10 @@ class MailtrapClient:
DEFAULT_PORT = 443
BULK_HOST = BULK_HOST
SANDBOX_HOST = SANDBOX_HOST
DEFAULT_USER_AGENT = (
f"mailtrap-python/{importlib.metadata.version('mailtrap')} "
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a version though? I didn't see it advertised in our other SDKs. We'll need to update analytics to parse the version out for groupping

"(https://github.com/railsware/mailtrap-python)"
)
Comment on lines +38 to +41
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

PackageNotFoundError at module import time if package is not installed.

importlib.metadata.version('mailtrap') is evaluated when the class body is executed (i.e., when mailtrap.client is first imported). In a raw git checkout without pip install -e ., this raises PackageNotFoundError, making the entire module unimportable. Consider a fallback:

🛡️ Proposed fallback
+try:
+    _mailtrap_version = importlib.metadata.version("mailtrap")
+except importlib.metadata.PackageNotFoundError:
+    _mailtrap_version = "unknown"
+
 class MailtrapClient:
     ...
     DEFAULT_USER_AGENT = (
-        f"mailtrap-python/{importlib.metadata.version('mailtrap')} "
+        f"mailtrap-python/{_mailtrap_version} "
         "(https://github.com/railsware/mailtrap-python)"
     )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mailtrap/client.py` around lines 40 - 43, The module-level DEFAULT_USER_AGENT
currently calls importlib.metadata.version('mailtrap') at import time which
raises PackageNotFoundError in a raw checkout; change this to handle that
exception (catch PackageNotFoundError from importlib.metadata.version) and fall
back to a safe default version string (e.g., "0+unknown" or "local") or compute
the version lazily when needed; update the DEFAULT_USER_AGENT construction in
mailtrap.client to use the try/except fallback (or a helper like a
get_user_agent() function) so importing mailtrap.client no longer fails when the
package metadata is missing.


def __init__(
self,
Expand All @@ -44,6 +49,7 @@ def __init__(
sandbox: bool = False,
account_id: Optional[str] = None,
inbox_id: Optional[str] = None,
user_agent: Optional[str] = None,
) -> None:
self.token = token
self.api_host = api_host
Expand All @@ -52,6 +58,9 @@ def __init__(
self.sandbox = sandbox
self.account_id = account_id
self.inbox_id = inbox_id
self._user_agent = (
user_agent if user_agent is not None else self.DEFAULT_USER_AGENT
)

self._validate_itself()

Expand Down Expand Up @@ -147,9 +156,7 @@ def headers(self) -> dict[str, str]:
return {
"Authorization": f"Bearer {self.token}",
"Content-Type": "application/json",
"User-Agent": (
"mailtrap-python (https://github.com/railsware/mailtrap-python)"
),
"User-Agent": self._user_agent,
}

@property
Expand Down
10 changes: 7 additions & 3 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ def test_headers_should_return_appropriate_dict(self) -> None:
assert client.headers == {
"Authorization": "Bearer fake_token",
"Content-Type": "application/json",
"User-Agent": (
"mailtrap-python (https://github.com/railsware/mailtrap-python)"
),
"User-Agent": mt.MailtrapClient.DEFAULT_USER_AGENT,
}

def test_headers_should_use_custom_user_agent_when_provided(self) -> None:
custom_ua = "MyApp/1.0 (custom)"
client = self.get_client(user_agent=custom_ua)

assert client.headers["User-Agent"] == custom_ua
Loading