Make it possible to set a custom user agent#61
Conversation
📝 WalkthroughWalkthroughThe MailtrapClient now allows runtime customization of the User-Agent header. A new Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment Tip You can customize the high-level summary generated by CodeRabbit.Configure the |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@mailtrap/client.py`:
- Line 63: The assignment to self._user_agent on the line using the ternary
expression (user_agent if user_agent is not None else self.DEFAULT_USER_AGENT)
exceeds the 90-char limit; fix it by splitting the expression across multiple
lines or by assigning the chosen value to a short temporary variable before
setting self._user_agent so that the final assignment line is under 90
characters — update the code around the self._user_agent assignment (where
user_agent and DEFAULT_USER_AGENT are referenced) to use a wrapped conditional
or intermediate variable to satisfy the line-length rule.
- Around line 1-5: Reorder and format the imports in mailtrap/client.py to
satisfy isort/Black: ensure stdlib imports are grouped together and alphabetized
(place import importlib.metadata and import warnings together in alpha order)
and include from typing import Optional in the correct stdlib group, then run
isort and black (e.g., isort mailtrap/client.py && black mailtrap/client.py) to
apply the final formatting; focus on the imports referencing importlib.metadata,
warnings, and Optional so CI pre-commit hooks pass.
- Around line 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.
| DEFAULT_USER_AGENT = ( | ||
| f"mailtrap-python/{importlib.metadata.version('mailtrap')} " | ||
| "(https://github.com/railsware/mailtrap-python)" | ||
| ) |
There was a problem hiding this comment.
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.
| BULK_HOST = BULK_HOST | ||
| SANDBOX_HOST = SANDBOX_HOST | ||
| DEFAULT_USER_AGENT = ( | ||
| f"mailtrap-python/{importlib.metadata.version('mailtrap')} " |
There was a problem hiding this comment.
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
8fe4303 to
9e6c564
Compare
9e6c564 to
8da73a7
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (1)
mailtrap/client.py (1)
38-41:⚠️ Potential issue | 🟡 Minor
PackageNotFoundErrorat module import time if package is not installed.This issue was flagged in a previous review but appears unaddressed. When importing this module from a raw git checkout (without
pip install -e .),importlib.metadata.version('mailtrap')will raisePackageNotFoundError, making the module unimportable. Consider adding 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 38 - 41, The DEFAULT_USER_AGENT construction currently calls importlib.metadata.version('mailtrap') at import time which raises importlib.metadata.PackageNotFoundError for raw checkouts; wrap the version lookup in a try/except that catches importlib.metadata.PackageNotFoundError (or Exception) and falls back to a safe default string (e.g., a generic version token or empty version) when importlib.metadata.version('mailtrap') fails so DEFAULT_USER_AGENT is always defined; update the DEFAULT_USER_AGENT expression to use that protected lookup instead of calling importlib.metadata.version(...) directly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@mailtrap/client.py`:
- Around line 38-41: The DEFAULT_USER_AGENT construction currently calls
importlib.metadata.version('mailtrap') at import time which raises
importlib.metadata.PackageNotFoundError for raw checkouts; wrap the version
lookup in a try/except that catches importlib.metadata.PackageNotFoundError (or
Exception) and falls back to a safe default string (e.g., a generic version
token or empty version) when importlib.metadata.version('mailtrap') fails so
DEFAULT_USER_AGENT is always defined; update the DEFAULT_USER_AGENT expression
to use that protected lookup instead of calling importlib.metadata.version(...)
directly.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: eaa41914-b33c-47da-a673-1fb977af5f51
📒 Files selected for processing (2)
mailtrap/client.pytests/unit/test_client.py
Motivation
Make it possible to set a custom user agent to identify specific clients
Changes
Summary by CodeRabbit
New Features
Tests