Skip to content

Commit

Permalink
Merge pull request #143 from rstudio/mbh-mypy
Browse files Browse the repository at this point in the history
Add mypy and address the mypy-identified sadness
  • Loading branch information
meatballhat authored Jun 23, 2020
2 parents e49fc2c + 8cb57b2 commit ff100cc
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 42 deletions.
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ deps-%:
lint-%:
$(RUNNER) 'pipenv run black --check --diff .'
$(RUNNER) 'pipenv run flake8 rsconnect/'
$(RUNNER) 'pipenv run mypy -p rsconnect'

.PHONY: lint-2.7
lint-2.7: .lint-unsupported
Expand All @@ -83,10 +84,18 @@ lint-3.5: .lint-unsupported
@echo ERROR: This python version cannot run the linting tools
@exit 1

.PHONY: clean clean-stores
.PHONY: clean
clean:
@rm -rf build dist rsconnect_python.egg-info

$(RM) -r \
./.coverage \
./.mypy_cache \
./.pytest_cache \
./build \
./dist \
./htmlcov \
./rsconnect_python.egg-info

.PHONY: clean-stores
clean-stores:
@find . -name "rsconnect-python" | xargs rm -rf

Expand Down
2 changes: 2 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ flake8 = "*"
funcsigs = "*"
importlib-metadata = "*"
ipython = "*"
mypy = "*"
pytest = "*"
pytest-cov = "*"
pytest-mypy = "*"
setuptools_scm = "*"
toml = "*"
twine = "*"
Expand Down
70 changes: 60 additions & 10 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[mypy]
ignore_missing_imports = True
strict_optional = False
2 changes: 1 addition & 1 deletion rsconnect/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def inspect_environment(
environment_json = check_output(args, universal_newlines=True)
except subprocess.CalledProcessError as e:
raise api.RSConnectException("Error inspecting environment: %s" % e.output)
return Environment(**json.loads(environment_json))
return Environment(**json.loads(environment_json)) # type: ignore


def _verify_server(connect_server):
Expand Down
10 changes: 5 additions & 5 deletions rsconnect/bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

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

# noinspection SpellCheckingInspection
Expand Down Expand Up @@ -188,7 +188,7 @@ def make_notebook_source_bundle(
environment, # type: Environment
extra_files=None, # type: typing.Optional[typing.List[str]]
):
# type: (...) -> io.BufferedRandom
# type: (...) -> typing.IO[bytes]
"""Create a bundle containing the specified notebook and python environment.
Returns a file-like object containing the bundle tarball.
Expand Down Expand Up @@ -240,7 +240,7 @@ def make_notebook_html_bundle(
python, # type: str
check_output=subprocess.check_output, # type: typing.Callable
):
# type: (...) -> io.BufferedRandom
# type: (...) -> typing.IO[bytes]
# noinspection SpellCheckingInspection
cmd = [
python,
Expand Down Expand Up @@ -413,7 +413,7 @@ def make_api_manifest(
extra_files=None, # type: typing.Optional[typing.List[str]]
excludes=None, # type: typing.Optional[typing.List[str]]
):
# type: (...) -> typing.Tuple[str, typing.List[str]]
# type: (...) -> typing.Tuple[typing.Dict[str, typing.Any], typing.List[str]]
"""
Makes a manifest for an API.
Expand Down Expand Up @@ -444,7 +444,7 @@ def make_api_bundle(
extra_files=None, # type: typing.Optional[typing.List[str]]
excludes=None, # type: typing.Optional[typing.List[str]]
):
# type: (...) -> io.BufferedRandom
# type: (...) -> typing.IO[bytes]
"""
Create an API bundle, given a directory path and a manifest.
Expand Down
27 changes: 10 additions & 17 deletions rsconnect/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@
exec_dir = os.path.dirname(sys.executable)


ENVIRONMENT_FIELDS_DEFAULTS = (
("conda", None),
("contents", ""),
("error", None),
("filename", ""),
("locale", ""),
("package_manager", ""),
("pip", None),
("python", None),
("source", None),
)
ENVIRONMENT_FIELDS = tuple([f[0] for f in ENVIRONMENT_FIELDS_DEFAULTS])
ENVIRONMENT_DEFAULTS = tuple([f[1] for f in ENVIRONMENT_FIELDS_DEFAULTS])

if sys.version_info[:2] < (3, 7):

Environment = collections.namedtuple("Environment", ENVIRONMENT_FIELDS)
Environment.__new__.__defaults__ = ENVIRONMENT_DEFAULTS
Environment = collections.namedtuple(
"Environment",
("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
)
Environment.__new__.__defaults__ = (None, "", None, "", "", "", None, None, None)

else:

Environment = collections.namedtuple("Environment", ENVIRONMENT_FIELDS, defaults=ENVIRONMENT_DEFAULTS,)
Environment = collections.namedtuple(
"Environment",
("conda", "contents", "error", "filename", "locale", "package_manager", "pip", "python", "source",),
defaults=(None, "", None, "", "", "", None, None, None),
)


class EnvironmentException(Exception):
Expand Down
10 changes: 6 additions & 4 deletions rsconnect/tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import os
import sys

try:
import typing
except ImportError:
typing = None

from os.path import dirname, join
from unittest import TestCase

try:
from inspect import signature
except ImportError:
from funcsigs import signature
from funcsigs import signature

from rsconnect import api

Expand Down
6 changes: 5 additions & 1 deletion scripts/build-image
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ python -m pip install --upgrade pipenv pip
case "${PYTHON_VERSION}" in
2*|3.5*)
pipenv install --skip-lock --python=/usr/local/bin/python
pipenv run pip install funcsigs pytest pytest-cov
pipenv run pip install \
'coverage[toml]' \
funcsigs \
pytest \
pytest-cov
;;
*)
pipenv install --dev --python=/usr/local/bin/python
Expand Down
2 changes: 1 addition & 1 deletion scripts/runtests
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ case "${PYTHON_VERSION}" in
pytest ${PYTEST_ARGS} ./rsconnect/
;;
*)
pipenv run pytest ${PYTEST_ARGS} ./rsconnect/
pipenv run pytest ${PYTEST_ARGS} --mypy ./rsconnect/
;;
esac

0 comments on commit ff100cc

Please sign in to comment.