Skip to content

Commit

Permalink
pyright -> mypy (#11)
Browse files Browse the repository at this point in the history
* update param sweep example

* make mypy work

* add mypy to CI
  • Loading branch information
tlambert03 committed May 4, 2020
1 parent 5a767f1 commit fa099e4
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 37 deletions.
2 changes: 0 additions & 2 deletions .github/ISSUE_TEMPLATE/--feature-request.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,3 @@ labels: ''
assignees: ''

---


30 changes: 18 additions & 12 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.5.0
hooks:
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/psf/black
rev: 19.10b0
hooks:
- id: black
- repo: https://gitlab.com/pycqa/flake8
rev: 3.7.9
hooks:
- id: flake8
pass_filenames: true
# this seems to need to be here in addition to setup.cfg
exclude: vendored|__init__.py|examples
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.770
hooks:
- id: mypy
additional_dependencies: [tokenize-rt==3.2.0]
exclude: 'examples'
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
include:
- { env: TOXENV=lint }
- { env: TOXENV=pydocstyle }
- { env: TOXENV=mypy }
- stage: Deploy
script: skip
deploy:
Expand Down
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ codebase:
- [black](https://github.com/psf/black) - code formatting
- [flake8](https://github.com/PyCQA/flake8) - linting
- [pydocstyle](https://github.com/PyCQA/pydocstyle/) - docstring conventions
- [pyright](https://github.com/microsoft/pyright) - static type anaylsis
- [mypy](http://mypy-lang.org/) - static type anaylsis
- [codecov](https://codecov.io/) - test coverage

To prevent continuous integration failures when contributing, please consider installing
Expand All @@ -72,6 +72,3 @@ prior to checking in new code.
```shell
pre-commit install
```

To run pyright, you will need to install with `npm install -g pyright`,
then run `pyright`. See [their docs](https://github.com/microsoft/pyright) for details.
3 changes: 2 additions & 1 deletion examples/basic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from magicgui import magicgui
from qtpy.QtWidgets import QApplication

from magicgui import magicgui

running = True
app = QApplication.instance()
if not app:
Expand Down
1 change: 0 additions & 1 deletion examples/wrapt_patching.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
Any,
Callable,
Dict,
DefaultDict,
Generator,
Iterable,
Optional,
Expand Down
3 changes: 2 additions & 1 deletion magicgui/_qt.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class GetSetOnChange(NamedTuple):
class HelpfulEnum(EnumMeta):
"""Metaclass that shows the available options on KeyError."""

def __getitem__(self, name: str) -> Enum:
def __getitem__(self, name: str):
"""Get enum by name, or raise helpful KeyError."""
try:
return super().__getitem__(name)
Expand Down Expand Up @@ -131,6 +131,7 @@ def type2widget(type_: type) -> Optional[Type[WidgetType]]:
return simple[type_]
elif isinstance(type_, EnumMeta):
return QDataComboBox
return None


def getter_setter_onchange(widget: WidgetType) -> GetSetOnChange:
Expand Down
18 changes: 12 additions & 6 deletions magicgui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ class instance using setattr(self, self.WIDGET_ATTR.format(name), widget).
TypeError
If ``position`` is provided but is not an integer.
"""
options = options or self.param_options.get(name) or dict()
_widget_type = widget_type or options.get("widget_type")
dtype = dtype or options.get("dtype")
_options: dict = options or self.param_options.get(name) or dict()
_widget_type = widget_type or _options.get("widget_type")
dtype = dtype or _options.get("dtype")

if dtype is not None:
arg_type: Type = dtype
Expand All @@ -297,7 +297,7 @@ class instance using setattr(self, self.WIDGET_ATTR.format(name), widget).

# argument specific choices override _CHOICES registered with `register_type`
choices: Optional[ChoicesType] = (
options.get("choices")
_options.get("choices")
or _type2choices(arg_type)
or (arg_type if isinstance(arg_type, EnumMeta) else None)
)
Expand Down Expand Up @@ -344,7 +344,7 @@ class instance using setattr(self, self.WIDGET_ATTR.format(name), widget).
delattr(self, name)

# instantiate a new widget
widget = api.make_widget(WidgetType, name=name, parent=self, **options)
widget = api.make_widget(WidgetType, name=name, parent=self, **_options)

# connect on_change signals
change_signal = api.getter_setter_onchange(widget).onchange
Expand Down Expand Up @@ -423,7 +423,8 @@ def refresh_choices(
choices = self.get_choices(name)
# choices are set as (name, data) tuples because we assume DataComboBox
if isinstance(choices, EnumMeta):
api.set_categorical_choices(widget, [(x.name, x) for x in choices])
# TODO: figure out typing on Enums
api.set_categorical_choices(widget, [(x.name, x) for x in choices]) # type: ignore # noqa
else:
api.set_categorical_choices(widget, [(str(c), c) for c in choices])

Expand Down Expand Up @@ -543,6 +544,11 @@ def __repr__(self):
# ######### magicgui decorator ######### #


# TODO: make protocol
# class MagicFunction(Protocol):
# Gui: MagicGuiBase


def magicgui(
function: Optional[Callable] = None,
layout: Union[api.Layout, str] = "horizontal",
Expand Down
8 changes: 0 additions & 8 deletions pyrightconfig.json

This file was deleted.

1 change: 1 addition & 0 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pydocstyle>=5.0.2
black==19.10b0
twine>=1.14.0
pre-commit>=2.0.0
mypy==0.770
7 changes: 7 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@ collect_ignore = ['setup.py']
match_dir = magicgui
convention = numpy
add_select = D402,D415,D417

[mypy]
warn_unused_configs = True
ignore_missing_imports = True

[mypy-.examples/]
ignore_errors = True
7 changes: 5 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[tox]
envlist = py36, py37, py38, lint, pydocstyle
envlist = py36, py37, py38, lint, pydocstyle, mypy
toxworkdir=/tmp/.tox

[travis]
python =
3.8: py38, lint, pydocstyle
3.8: py38, lint, pydocstyle, mypy
3.7: py37
3.6: py36

Expand All @@ -20,6 +20,9 @@ commands =
deps = pydocstyle>=5.0.2
commands = pydocstyle

[testenv:mypy]
deps = mypy
commands = mypy magicgui

[testenv]
passenv = CI TRAVIS TRAVIS_*
Expand Down

0 comments on commit fa099e4

Please sign in to comment.