-
Notifications
You must be signed in to change notification settings - Fork 301
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
fix(core): Typing in docker_client #702
base: main
Are you sure you want to change the base?
fix(core): Typing in docker_client #702
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #702 +/- ##
=======================================
Coverage ? 85.37%
=======================================
Files ? 12
Lines ? 670
Branches ? 105
=======================================
Hits ? 572
Misses ? 75
Partials ? 23 ☔ View full report in Codecov by Sentry. |
Please note: core/testcontainers/core/config.py:46: error: X | Y syntax for unions requires Python 3.10 [syntax]
connection_mode: str | None = environ.get("TESTCONTAINERS_CONNECTION_MODE") Found when rebasing and updated to support 3.9 (I'm not aware of deprecating 3.9 in tc-python, feel free to update) |
@alexanderankin @kiview can you please rerun this? (I assume this was an issue with the docker registry) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use cast
only when required (i.e. for dict[str, Any]
).
If possible always improve the runtime type safety.
|
||
def bridge_ip(self, container_id: str) -> str: | ||
""" | ||
Get the bridge ip address for a container. | ||
""" | ||
container = self.get_container(container_id) | ||
network_name = self.network_name(container_id) | ||
return container["NetworkSettings"]["Networks"][network_name]["IPAddress"] | ||
return cast(str, container["NetworkSettings"]["Networks"][network_name]["IPAddress"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return cast(str, container["NetworkSettings"]["Networks"][network_name]["IPAddress"]) | |
return str(container["NetworkSettings"]["Networks"][network_name]["IPAddress"]) |
Ensures the typing instead of suggesting it.
It more readable and more runtime safe.
@@ -190,15 +190,15 @@ def network_name(self, container_id: str) -> str: | |||
name = container["HostConfig"]["NetworkMode"] | |||
if name == "default": | |||
return "bridge" | |||
return name | |||
return cast(str, name) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of
container = self.get_container(container_id)
name = container["HostConfig"]["NetworkMode"]
if name == "default":
return "bridge"
return cast(str, name)
Use
container = self.get_container(container_id)
name = str(container["HostConfig"]["NetworkMode"])
if name == "default":
return "bridge"
return name
It is more runtime safe
|
||
def gateway_ip(self, container_id: str) -> str: | ||
""" | ||
Get the gateway ip address for a container. | ||
""" | ||
container = self.get_container(container_id) | ||
network_name = self.network_name(container_id) | ||
return container["NetworkSettings"]["Networks"][network_name]["Gateway"] | ||
return cast(str, container["NetworkSettings"]["Networks"][network_name]["Gateway"]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return cast(str, container["NetworkSettings"]["Networks"][network_name]["Gateway"]) | |
return str(container["NetworkSettings"]["Networks"][network_name]["Gateway"]) |
@@ -237,7 +237,7 @@ def host(self) -> str: | |||
# see https://github.com/testcontainers/testcontainers-python/issues/415 | |||
if url.hostname == "localnpipe" and utils.is_windows(): | |||
return "localhost" | |||
return url.hostname | |||
return cast(str, url.hostname) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of
if url.hostname == "localnpipe" and utils.is_windows():
return "localhost"
return cast(str, url.hostname)
use
hostname = url.hostname
if not hostname or (hostname == "localnpipe" and utils.is_windows()):
return "localhost"
return url.hostname
Don't cast a possible None
into a str
it is not runtime safe.
Instead handle the case correctly.
(Sorry suggestion in Gitlab don't work good if you want to suggest a larger change)
@@ -19,7 +19,7 @@ def use_mapped_port(self) -> bool: | |||
|
|||
This is true for everything but bridge mode. | |||
""" | |||
if self == self.bridge_ip: | |||
if cast(str, self) == self.bridge_ip: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Supports: #305
Related : #691 #692 #700
Based on #504, kudos @alexanderankin
Old
New