Skip to content
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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Tranquility2
Copy link
Contributor

Supports: #305
Related : #691 #692 #700

Based on #504, kudos @alexanderankin

poetry run mypy --config-file pyproject.toml core/testcontainers/core/docker_client.py 
Success: no issues found in 1 source file

Old

                    Error Summary                     
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓
┃ File Path                                 ┃ Errors ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩
│ core/testcontainers/core/version.py       │ 12     │
│ core/testcontainers/core/docker_client.py │ 14     │
│ core/testcontainers/core/image.py         │ 17     │
│ core/testcontainers/core/waiting_utils.py │ 8      │
│ core/testcontainers/core/container.py     │ 20     │
│ core/tests/test_new_docker_api.py         │ 4      │
│ core/tests/test_docker_in_docker.py       │ 2      │
│ core/testcontainers/compose/compose.py    │ 22     │
│ core/testcontainers/compose/__init__.py   │ 2      │
│ core/tests/test_version.py                │ 2      │
│ core/tests/test_ryuk.py                   │ 2      │
│ core/tests/test_registry.py               │ 1      │
│ core/tests/test_image.py                  │ 3      │
│ core/tests/test_compose.py                │ 7      │
└───────────────────────────────────────────┴────────┘
Found 116 errors in 14 files.

New

                    Error Summary                     
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┓
┃ File Path                                 ┃ Errors ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━┩
│ core/testcontainers/core/version.py       │ 12     │
│ core/testcontainers/core/network.py       │ 3      │
│ core/testcontainers/core/image.py         │ 17     │
│ core/testcontainers/core/waiting_utils.py │ 8      │
│ core/testcontainers/core/container.py     │ 19     │
│ core/tests/test_new_docker_api.py         │ 4      │
│ core/tests/test_docker_in_docker.py       │ 2      │
│ core/testcontainers/compose/compose.py    │ 22     │
│ core/testcontainers/compose/__init__.py   │ 2      │
│ core/tests/test_version.py                │ 2      │
│ core/tests/test_ryuk.py                   │ 2      │
│ core/tests/test_registry.py               │ 1      │
│ core/tests/test_image.py                  │ 3      │
│ core/tests/test_compose.py                │ 7      │
└───────────────────────────────────────────┴────────┘
Found 104 errors in 14 files.

Copy link

codecov bot commented Sep 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Please upload report for BASE (main@ace2a7d). Learn more about missing BASE report.

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.
📢 Have feedback on the report? Share it here.

@Tranquility2 Tranquility2 changed the title fix(core): Typed docker_client fix(core): Typing in docker_client Sep 16, 2024
@Tranquility2
Copy link
Contributor Author

Tranquility2 commented Nov 16, 2024

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)

@Tranquility2
Copy link
Contributor Author

@alexanderankin @kiview can you please rerun this? (I assume this was an issue with the docker registry)

Copy link
Contributor

@CarliJoy CarliJoy left a 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"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Contributor

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"])
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Contributor

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if cast(str, self) == self.bridge_ip:
if self == self.bridge_ip:

That is neither required nor correct.

The original code does not issue any Typing Errors:
Not in mypy nor pyright

@Tranquility2
Copy link
Contributor Author

Tranquility2 commented Mar 8, 2025

@CarliJoy apricate you taking the time to comment :) I'll be happy to fix and update as suggested.
I assume/hope #700 & #701 will go in first so I'll know we are really rolling with this track.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants