Skip to content

Commit ff100cc

Browse files
authored
Merge pull request #143 from rstudio/mbh-mypy
Add mypy and address the mypy-identified sadness
2 parents e49fc2c + 8cb57b2 commit ff100cc

File tree

10 files changed

+105
-42
lines changed

10 files changed

+105
-42
lines changed

Makefile

+12-3
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ deps-%:
7171
lint-%:
7272
$(RUNNER) 'pipenv run black --check --diff .'
7373
$(RUNNER) 'pipenv run flake8 rsconnect/'
74+
$(RUNNER) 'pipenv run mypy -p rsconnect'
7475

7576
.PHONY: lint-2.7
7677
lint-2.7: .lint-unsupported
@@ -83,10 +84,18 @@ lint-3.5: .lint-unsupported
8384
@echo ERROR: This python version cannot run the linting tools
8485
@exit 1
8586

86-
.PHONY: clean clean-stores
87+
.PHONY: clean
8788
clean:
88-
@rm -rf build dist rsconnect_python.egg-info
89-
89+
$(RM) -r \
90+
./.coverage \
91+
./.mypy_cache \
92+
./.pytest_cache \
93+
./build \
94+
./dist \
95+
./htmlcov \
96+
./rsconnect_python.egg-info
97+
98+
.PHONY: clean-stores
9099
clean-stores:
91100
@find . -name "rsconnect-python" | xargs rm -rf
92101

Pipfile

+2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ flake8 = "*"
1010
funcsigs = "*"
1111
importlib-metadata = "*"
1212
ipython = "*"
13+
mypy = "*"
1314
pytest = "*"
1415
pytest-cov = "*"
16+
pytest-mypy = "*"
1517
setuptools_scm = "*"
1618
toml = "*"
1719
twine = "*"

Pipfile.lock

+60-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mypy.ini

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[mypy]
2+
ignore_missing_imports = True
3+
strict_optional = False

rsconnect/actions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def inspect_environment(
137137
environment_json = check_output(args, universal_newlines=True)
138138
except subprocess.CalledProcessError as e:
139139
raise api.RSConnectException("Error inspecting environment: %s" % e.output)
140-
return Environment(**json.loads(environment_json))
140+
return Environment(**json.loads(environment_json)) # type: ignore
141141

142142

143143
def _verify_server(connect_server):

rsconnect/bundle.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
# noinspection SpellCheckingInspection
3737
def make_source_manifest(entrypoint, environment, app_mode):
38-
# type: (str, Environment, AppMode) -> dict
38+
# type: (str, Environment, AppMode) -> typing.Dict[str, typing.Any]
3939
package_manager = environment.package_manager
4040

4141
# noinspection SpellCheckingInspection
@@ -188,7 +188,7 @@ def make_notebook_source_bundle(
188188
environment, # type: Environment
189189
extra_files=None, # type: typing.Optional[typing.List[str]]
190190
):
191-
# type: (...) -> io.BufferedRandom
191+
# type: (...) -> typing.IO[bytes]
192192
"""Create a bundle containing the specified notebook and python environment.
193193
194194
Returns a file-like object containing the bundle tarball.
@@ -240,7 +240,7 @@ def make_notebook_html_bundle(
240240
python, # type: str
241241
check_output=subprocess.check_output, # type: typing.Callable
242242
):
243-
# type: (...) -> io.BufferedRandom
243+
# type: (...) -> typing.IO[bytes]
244244
# noinspection SpellCheckingInspection
245245
cmd = [
246246
python,
@@ -413,7 +413,7 @@ def make_api_manifest(
413413
extra_files=None, # type: typing.Optional[typing.List[str]]
414414
excludes=None, # type: typing.Optional[typing.List[str]]
415415
):
416-
# type: (...) -> typing.Tuple[str, typing.List[str]]
416+
# type: (...) -> typing.Tuple[typing.Dict[str, typing.Any], typing.List[str]]
417417
"""
418418
Makes a manifest for an API.
419419
@@ -444,7 +444,7 @@ def make_api_bundle(
444444
extra_files=None, # type: typing.Optional[typing.List[str]]
445445
excludes=None, # type: typing.Optional[typing.List[str]]
446446
):
447-
# type: (...) -> io.BufferedRandom
447+
# type: (...) -> typing.IO[bytes]
448448
"""
449449
Create an API bundle, given a directory path and a manifest.
450450

rsconnect/environment.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,21 @@
1818
exec_dir = os.path.dirname(sys.executable)
1919

2020

21-
ENVIRONMENT_FIELDS_DEFAULTS = (
22-
("conda", None),
23-
("contents", ""),
24-
("error", None),
25-
("filename", ""),
26-
("locale", ""),
27-
("package_manager", ""),
28-
("pip", None),
29-
("python", None),
30-
("source", None),
31-
)
32-
ENVIRONMENT_FIELDS = tuple([f[0] for f in ENVIRONMENT_FIELDS_DEFAULTS])
33-
ENVIRONMENT_DEFAULTS = tuple([f[1] for f in ENVIRONMENT_FIELDS_DEFAULTS])
34-
3521
if sys.version_info[:2] < (3, 7):
3622

37-
Environment = collections.namedtuple("Environment", ENVIRONMENT_FIELDS)
38-
Environment.__new__.__defaults__ = ENVIRONMENT_DEFAULTS
23+
Environment = collections.namedtuple(
24+
"Environment",
25+
("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
26+
)
27+
Environment.__new__.__defaults__ = (None, "", None, "", "", "", None, None, None)
3928

4029
else:
4130

42-
Environment = collections.namedtuple("Environment", ENVIRONMENT_FIELDS, defaults=ENVIRONMENT_DEFAULTS,)
31+
Environment = collections.namedtuple(
32+
"Environment",
33+
("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
34+
defaults=(None, "", None, "", "", "", None, None, None),
35+
)
4336

4437

4538
class EnvironmentException(Exception):

rsconnect/tests/test_actions.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import os
22
import sys
33

4+
try:
5+
import typing
6+
except ImportError:
7+
typing = None
8+
49
from os.path import dirname, join
510
from unittest import TestCase
611

7-
try:
8-
from inspect import signature
9-
except ImportError:
10-
from funcsigs import signature
12+
from funcsigs import signature
1113

1214
from rsconnect import api
1315

scripts/build-image

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ python -m pip install --upgrade pipenv pip
1212
case "${PYTHON_VERSION}" in
1313
2*|3.5*)
1414
pipenv install --skip-lock --python=/usr/local/bin/python
15-
pipenv run pip install funcsigs pytest pytest-cov
15+
pipenv run pip install \
16+
'coverage[toml]' \
17+
funcsigs \
18+
pytest \
19+
pytest-cov
1620
;;
1721
*)
1822
pipenv install --dev --python=/usr/local/bin/python

scripts/runtests

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ case "${PYTHON_VERSION}" in
1313
pytest ${PYTEST_ARGS} ./rsconnect/
1414
;;
1515
*)
16-
pipenv run pytest ${PYTEST_ARGS} ./rsconnect/
16+
pipenv run pytest ${PYTEST_ARGS} --mypy ./rsconnect/
1717
;;
1818
esac

0 commit comments

Comments
 (0)