-
Notifications
You must be signed in to change notification settings - Fork 308
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.35%
=======================================
Files ? 12
Lines ? 669
Branches ? 105
=======================================
Hits ? 571
Misses ? 75
Partials ? 23 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
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.
Fixed
@@ -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
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.
Super nice, fixed
|
||
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"]) |
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.
Fixed
@@ -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)
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.
This is great, but url.hostname
is still "Any" and we expects "str".
but I totally agree with the possible None
so the logic improvement is critical 🙏
(No need to apologize, trust me you have done above and beyond)
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.
Fixed
core/testcontainers/core/config.py
Outdated
@@ -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.
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.
@CarliJoy Mypy seems to disagree:
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.
Update to latest mypy, still the same behavior
mypy 1.15.0 (compiled: yes)
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.
Found the issue :)
under "[tool.mypy]" on pyproject.toml
we have strict = true
I'll be adding # type: ignore[comparison-overlap]
in the relevant locations (future PRs) @CarliJoy
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.
Fixed
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.
if self == ConnectionMode.bridge_ip:
Seems to be the type correct solution.
f4b0d0a
to
59372c6
Compare
@CarliJoy Updated with all of the Fixes |
Supports: #305
Related : #691 #692 #700
Based on #504, kudos @alexanderankin
Old
New