Skip to content

Commit b020a0c

Browse files
authored
Merge pull request #1580 from mathbunnyru/asalikhov/fix_typing
Fix all typing issues
2 parents 9d13d95 + 37c510f commit b020a0c

25 files changed

+184
-129
lines changed

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ repos:
1717
- id: black
1818
args: [--target-version=py39]
1919

20+
# Check python code static typing
21+
- repo: https://github.com/pre-commit/mirrors-mypy
22+
rev: v0.931
23+
hooks:
24+
- id: mypy
25+
additional_dependencies: ["pytest", "types-requests", "types-tabulate"]
26+
2027
# Autoformat: YAML, JSON, Markdown, etc.
2128
- repo: https://github.com/pre-commit/mirrors-prettier
2229
rev: v2.5.1

all-spark-notebook/test/test_spark_notebooks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55

6-
import pytest
6+
import pytest # type: ignore
77
from pathlib import Path
88

99
from conftest import TrackedContainer

base-notebook/jupyter_notebook_config.py

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Copyright (c) Jupyter Development Team.
22
# Distributed under the terms of the Modified BSD License.
3-
3+
# mypy: ignore-errors
44
from jupyter_core.paths import jupyter_data_dir
55
import subprocess
66
import os
7-
import errno
87
import stat
98

9+
1010
c = get_config() # noqa: F821
1111
c.NotebookApp.ip = "0.0.0.0"
1212
c.NotebookApp.port = 8888
@@ -16,28 +16,21 @@
1616
c.FileContentsManager.delete_to_trash = False
1717

1818
# Generate a self-signed certificate
19+
OPENSSL_CONFIG = """\
20+
[req]
21+
distinguished_name = req_distinguished_name
22+
[req_distinguished_name]
23+
"""
1924
if "GEN_CERT" in os.environ:
2025
dir_name = jupyter_data_dir()
2126
pem_file = os.path.join(dir_name, "notebook.pem")
22-
try:
23-
os.makedirs(dir_name)
24-
except OSError as exc: # Python >2.5
25-
if exc.errno == errno.EEXIST and os.path.isdir(dir_name):
26-
pass
27-
else:
28-
raise
27+
os.makedirs(dir_name, exist_ok=True)
2928

3029
# Generate an openssl.cnf file to set the distinguished name
3130
cnf_file = os.path.join(os.getenv("CONDA_DIR", "/usr/lib"), "ssl", "openssl.cnf")
3231
if not os.path.isfile(cnf_file):
3332
with open(cnf_file, "w") as fh:
34-
fh.write(
35-
"""\
36-
[req]
37-
distinguished_name = req_distinguished_name
38-
[req_distinguished_name]
39-
"""
40-
)
33+
fh.write(OPENSSL_CONFIG)
4134

4235
# Generate a certificate if one doesn't exist on disk
4336
subprocess.check_call(

base-notebook/test/test_container_options.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import time
55
import logging
66

7-
import pytest
7+
import pytest # type: ignore
88
import requests
99

1010
from conftest import TrackedContainer
@@ -303,6 +303,6 @@ def test_jupyter_env_vars_to_unset_as_root(
303303
"-c",
304304
"echo I like $FRUIT and ${SECRET_FRUIT:-stuff}, and love ${SECRET_ANIMAL:-to keep secrets}!",
305305
],
306-
**root_args,
306+
**root_args, # type: ignore
307307
)
308308
assert "I like bananas and stuff, and love to keep secrets!" in logs

base-notebook/test/test_package_managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Distributed under the terms of the Modified BSD License.
33

44
import logging
5-
import pytest
5+
import pytest # type: ignore
66

77
from conftest import TrackedContainer
88

base-notebook/test/test_python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Distributed under the terms of the Modified BSD License.
33
import logging
44

5-
from packaging import version
5+
from packaging import version # type: ignore
66

77
from conftest import TrackedContainer
88

base-notebook/test/test_start_container.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55
from typing import Optional
6-
import pytest
6+
import pytest # type: ignore
77
import requests
88
import time
99

conftest.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# Distributed under the terms of the Modified BSD License.
33
import os
44
import logging
5-
import typing
5+
from typing import Any, Optional
66

77
import docker
88
from docker.models.containers import Container
9-
import pytest
9+
import pytest # type: ignore
1010
import requests
1111

1212
from requests.packages.urllib3.util.retry import Retry
@@ -35,7 +35,7 @@ def docker_client() -> docker.DockerClient:
3535
@pytest.fixture(scope="session")
3636
def image_name() -> str:
3737
"""Image name to test"""
38-
return os.getenv("TEST_IMAGE")
38+
return os.environ["TEST_IMAGE"]
3939

4040

4141
class TrackedContainer:
@@ -56,14 +56,14 @@ def __init__(
5656
self,
5757
docker_client: docker.DockerClient,
5858
image_name: str,
59-
**kwargs: typing.Any,
59+
**kwargs: Any,
6060
):
61-
self.container = None
62-
self.docker_client = docker_client
63-
self.image_name = image_name
64-
self.kwargs = kwargs
61+
self.container: Optional[Container] = None
62+
self.docker_client: docker.DockerClient = docker_client
63+
self.image_name: str = image_name
64+
self.kwargs: Any = kwargs
6565

66-
def run_detached(self, **kwargs: typing.Any) -> Container:
66+
def run_detached(self, **kwargs: Any) -> Container:
6767
"""Runs a docker container using the preconfigured image name
6868
and a mix of the preconfigured container options and those passed
6969
to this method.
@@ -94,11 +94,12 @@ def run_and_wait(
9494
timeout: int,
9595
no_warnings: bool = True,
9696
no_errors: bool = True,
97-
**kwargs: typing.Any,
97+
**kwargs: Any,
9898
) -> str:
9999
running_container = self.run_detached(**kwargs)
100100
rv = running_container.wait(timeout=timeout)
101101
logs = running_container.logs().decode("utf-8")
102+
assert isinstance(logs, str)
102103
LOGGER.debug(logs)
103104
if no_warnings:
104105
assert not self.get_warnings(logs)
@@ -119,14 +120,14 @@ def get_warnings(logs: str) -> list[str]:
119120
def _lines_starting_with(logs: str, pattern: str) -> list[str]:
120121
return [line for line in logs.splitlines() if line.startswith(pattern)]
121122

122-
def remove(self):
123+
def remove(self) -> None:
123124
"""Kills and removes the tracked docker container."""
124125
if self.container:
125126
self.container.remove(force=True)
126127

127128

128129
@pytest.fixture(scope="function")
129-
def container(docker_client: docker.DockerClient, image_name: str):
130+
def container(docker_client: docker.DockerClient, image_name: str) -> Container:
130131
"""Notebook container with initial configuration appropriate for testing
131132
(e.g., HTTP port exposed to the host for HTTP calls).
132133

minimal-notebook/test/test_nbconvert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55

6-
import pytest
6+
import pytest # type: ignore
77
from pathlib import Path
88

99
from conftest import TrackedContainer

mypy.ini

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[mypy]
2+
python_version = 3.9
3+
follow_imports = normal
4+
strict = False
5+
no_incremental = True
6+
7+
[mypy-docker.*]
8+
ignore_missing_imports = True
9+
10+
[mypy-matplotlib.*]
11+
ignore_missing_imports = True
12+
13+
[mypy-packaging.*]
14+
ignore_missing_imports = True
15+
16+
[mypy-pandas.*]
17+
ignore_missing_imports = True
18+
19+
[mypy-plumbum.*]
20+
ignore_missing_imports = True
21+
22+
[mypy-pyspark.*]
23+
ignore_missing_imports = True
24+
25+
[mypy-tensorflow.*]
26+
ignore_missing_imports = True

0 commit comments

Comments
 (0)