Skip to content

Commit 1b210ea

Browse files
committed
Add static code analysis, both type and style checking
Also made the type hinting more uniform
1 parent baa43ee commit 1b210ea

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

.pycodestyle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pycodestyle]
2+
max-line-length = 119

src/pytest_docker_compose/__init__.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import typing
1+
from typing import List
22
from pathlib import Path
33
import warnings
44
from datetime import datetime
@@ -23,8 +23,7 @@ class ContainersAlreadyExist(Exception):
2323

2424

2525
class NetworkInfo:
26-
def __init__(self, container_port: typing.Text,
27-
hostname: typing.Text, host_port: int,):
26+
def __init__(self, container_port: str, hostname: str, host_port: int,):
2827
"""
2928
Container for info about how to connect to a service exposed by a
3029
Docker container.
@@ -39,7 +38,7 @@ def __init__(self, container_port: typing.Text,
3938
self.host_port = host_port
4039

4140

42-
def create_network_info_for_container(container):
41+
def create_network_info_for_container(container: Container):
4342
"""
4443
Generates :py:class:`NetworkInfo` instances corresponding to all available
4544
port bindings in a container
@@ -140,7 +139,7 @@ def docker_project(self, request):
140139
return project
141140

142141
@classmethod
143-
def generate_scoped_containers_fixture(cls, scope):
142+
def generate_scoped_containers_fixture(cls, scope: str):
144143
"""
145144
Create scoped fixtures that retrieve or spin up all containers, and add
146145
network info objects to containers and then yield the containers for
@@ -152,21 +151,20 @@ def generate_scoped_containers_fixture(cls, scope):
152151
@pytest.fixture(scope=scope)
153152
def scoped_containers_fixture(docker_project: Project, request):
154153
now = datetime.utcnow()
155-
if request.config.getoption("--use-running-containers"):
156-
containers = docker_project.containers() # type: typing.List[Container]
157-
else:
154+
if not request.config.getoption("--use-running-containers"):
158155
if any(docker_project.containers()):
159156
raise ContainersAlreadyExist(
160157
'pytest-docker-compose tried to start containers but there are'
161158
' already running containers: %s, you probably scoped your'
162159
' tests wrong' % docker_project.containers())
163-
containers = docker_project.up() # type: typing.List[Container]
160+
containers = docker_project.up() # type: List[Container]
164161
if not containers:
165162
raise ValueError("`docker-compose` didn't launch any containers!")
166163

167-
yield ContainerDict(docker_project)
164+
container_dict = ContainerDict(docker_project)
165+
yield container_dict
168166

169-
for container in sorted(containers, key=lambda c: c.name):
167+
for container in sorted(container_dict.values(), key=lambda c: c.name):
170168
header = "Logs from {name}:".format(name=container.name)
171169
print(header, '\n', "=" * len(header))
172170
print(container.logs(since=now).decode("utf-8", errors="replace")
@@ -185,16 +183,17 @@ def scoped_containers_fixture(docker_project: Project, request):
185183

186184

187185
class ContainerDict(dict):
188-
"""A read-only dictionary that re-fetches containers from a docker-compose
189-
project dynamically every time they are accessed."""
190-
186+
"""
187+
A read-only dictionary that re-fetches containers from a docker-compose
188+
project dynamically every time they are accessed.
189+
"""
191190
def __init__(self, docker_project: Project) -> None:
192191
super().__init__()
193192
self.docker_project = docker_project
194193
for container in docker_project.containers():
195194
super().__setitem__(container.name, container)
196195

197-
def __getitem__(self, key):
196+
def __getitem__(self, key: str) -> Container:
198197
container = [container for container in self.docker_project.containers()
199198
if container.name == key][0]
200199
container.network_info = create_network_info_for_container(container)

tox.ini

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ deps =
66
pytest3: pytest>=3,<4
77
pytest4: pytest>=4,<5
88
pytest5: pytest>=5,<6
9+
docker-compose==1.24.1
10+
pycodestyle
11+
mypy
912
whitelist_externals=
1013
bash
1114
commands=
15+
pycodestyle --config .pycodestyle src
16+
mypy --namespace-packages --ignore-missing-imports src
1217
bash -c '! pytest -m should_fail'
1318
pytest
1419
docker-compose -f tests/pytest_docker_compose_tests/my_network/docker-compose.yml up -d

0 commit comments

Comments
 (0)