11import time
22from io import BytesIO
33from tarfile import TarFile , TarInfo
4- from typing import Optional
4+ from typing import TYPE_CHECKING , Optional
55
66import bcrypt
7- from requests import Response , get
7+ from requests import get
88from requests .auth import HTTPBasicAuth
99from requests .exceptions import ConnectionError , ReadTimeout
10+
1011from testcontainers .core .container import DockerContainer
1112from testcontainers .core .waiting_utils import wait_container_is_ready
1213
14+ if TYPE_CHECKING :
15+ from requests import Response
16+
17+
1318class DockerRegistryContainer (DockerContainer ):
1419 # https://docs.docker.com/registry/
1520 credentials_path : str = "/htpasswd/credentials.txt"
@@ -18,8 +23,8 @@ def __init__(
1823 self ,
1924 image : str = "registry:2" ,
2025 port : int = 5000 ,
21- username : str = None ,
22- password : str = None ,
26+ username : Optional [ str ] = None ,
27+ password : Optional [ str ] = None ,
2328 ** kwargs ,
2429 ) -> None :
2530 super ().__init__ (image = image , ** kwargs )
@@ -34,11 +39,9 @@ def _copy_credentials(self) -> None:
3439 self .password .encode ("utf-8" ),
3540 bcrypt .gensalt (rounds = 12 , prefix = b"2a" ),
3641 ).decode ("utf-8" )
37- content = f"{ self .username } :{ hashed_password } " .encode ("utf-8" )
42+ content : bytes = f"{ self .username } :{ hashed_password } " .encode ("utf-8" ) # noqa: UP012
3843
39- with BytesIO () as tar_archive_object , TarFile (
40- fileobj = tar_archive_object , mode = "w"
41- ) as tmp_tarfile :
44+ with BytesIO () as tar_archive_object , TarFile (fileobj = tar_archive_object , mode = "w" ) as tmp_tarfile :
4245 tarinfo : TarInfo = TarInfo (name = self .credentials_path )
4346 tarinfo .size = len (content )
4447 tarinfo .mtime = time .time ()
@@ -65,7 +68,7 @@ def start(self):
6568 else :
6669 super ().start ()
6770
68- self ._readiness_probe ()
71+ self ._readiness_probe ()
6972 return self
7073
7174 def get_registry (self ) -> str :
0 commit comments