Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TYP: bootstrap type-checking #355

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,12 @@ omit = [
"unyt/_on_demand_imports.py",
"unyt/tests/test_linters.py",
]

[tool.mypy]
python_version = 3.8
show_error_codes = true
ignore_missing_imports = true
warn_unused_configs = true
warn_unused_ignores = true
warn_unreachable = true
show_error_context = true
13 changes: 11 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[tox]
envlist = py38-docs,begin,py38-dependencies,py38-versions,py{38,39,310},py38-unyt-module-test-function,end
envlist = py38-docs,begin,py38-dependencies,py38-versions,py{38,39,310},py38-unyt-module-test-function,typecheck,end
isolated_build = True

[gh-actions]
python =
3.8: py38, py38-docs, py38-dependencies, py38-versions, py38-unyt-module-test-function
3.8: py38, py38-docs, py38-dependencies, py38-versions, py38-unyt-module-test-function, typecheck
3.9: py39
3.10: py310

Expand Down Expand Up @@ -79,6 +79,15 @@ depends = py38
commands =
python -c 'import unyt; unyt.test()'

[testenv:typecheck]
deps =
mypy==0.991
numpy==1.17.5
sympy==1.5

commands =
python -m mypy unyt

[testenv:begin]
commands =
coverage erase
Expand Down
4 changes: 2 additions & 2 deletions unyt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ def test(): # pragma: no cover


# isort: off
from unyt.mpl_interface import matplotlib_support
from unyt.mpl_interface import MatplotlibSupport

matplotlib_support = matplotlib_support()
matplotlib_support = MatplotlibSupport()
2 changes: 1 addition & 1 deletion unyt/_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
from packaging.version import Version

from unyt import delta_degC
from unyt import delta_degC # type: ignore [attr-defined]
from unyt.array import NULL_UNIT, unyt_array
from unyt.dimensions import temperature
from unyt.exceptions import (
Expand Down
13 changes: 12 additions & 1 deletion unyt/_mpl_array_converter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
from typing import TYPE_CHECKING
from weakref import WeakKeyDictionary

from matplotlib.units import AxisInfo, ConversionInterface

from unyt.array import unyt_array, unyt_quantity
from unyt.unit_object import Unit

if TYPE_CHECKING:
import sys

from matplotlib.axes import Axis

if sys.version_info >= (3, 9):
from collections.abc import MutableMapping
else:
from typing import MutableMapping


class unyt_arrayConverter(ConversionInterface):
"""Matplotlib interface for unyt_array"""

_instance = None
_labelstyle = "()"
_axisnames = WeakKeyDictionary()
_axisnames: "MutableMapping[Axis, str]" = WeakKeyDictionary()

# ensure that unyt_arrayConverter is a singleton
def __new__(cls):
Expand Down
3 changes: 2 additions & 1 deletion unyt/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
bitwise_or,
bitwise_xor,
ceil,
clip,
conj,
copysign,
cos,
Expand Down Expand Up @@ -101,7 +102,7 @@
true_divide,
trunc,
)
from numpy.core.umath import _ones_like, clip
from numpy.core.umath import _ones_like
from sympy import Rational

from unyt._on_demand_imports import _astropy, _dask, _pint
Expand Down
4 changes: 2 additions & 2 deletions unyt/mpl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

from ._on_demand_imports import _matplotlib

__all__ = ["matplotlib_support"]
__all__ = ["MatplotlibSupport"]


class matplotlib_support:
class MatplotlibSupport:
"""Context manager for enabling the feature

When used in a with statement, the feature is enabled during the context and
Expand Down
2 changes: 1 addition & 1 deletion unyt/tests/test_array_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pytest
from packaging.version import Version

from unyt import K, cm, degC, degF, delta_degC, g, km, s
from unyt import K, cm, degC, degF, delta_degC, g, km, s # type: ignore [attr-defined]
from unyt._array_functions import _HANDLED_FUNCTIONS as HANDLED_FUNCTIONS
from unyt.array import unyt_array, unyt_quantity
from unyt.exceptions import (
Expand Down
5 changes: 3 additions & 2 deletions unyt/tests/test_dask_arrays.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pickle
from collections import defaultdict
from typing import Any, DefaultDict, Tuple

import numpy as np
import pytest
Expand All @@ -19,7 +20,7 @@
unyt_from_dask,
)
from unyt.exceptions import UnitOperationError
from unyt.unit_symbols import cm, g, m
from unyt.unit_symbols import cm, g, m # type: ignore [attr-defined]


def test_unyt_dask_creation():
Expand Down Expand Up @@ -263,7 +264,7 @@ def test_unyt_type_result():
assert result == unyt_quantity(1, m)


_func_args = defaultdict(lambda: ())
_func_args: DefaultDict[str, Tuple[Any, ...]] = defaultdict(lambda: ())
_func_args["reshape"] = ((100, 1),)
_func_args["rechunk"] = ((5, 5),)
_func_args["cumsum"] = (0,)
Expand Down
9 changes: 8 additions & 1 deletion unyt/tests/test_mpl_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
import numpy as np
import pytest

from unyt import K, m, matplotlib_support, s, unyt_array, unyt_quantity
from unyt import ( # type: ignore [attr-defined]
K,
m,
matplotlib_support,
s,
unyt_array,
unyt_quantity,
)
from unyt._on_demand_imports import NotAModule, _matplotlib

try:
Expand Down
13 changes: 11 additions & 2 deletions unyt/tests/test_unyt_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@
)
from packaging.version import Version

from unyt import K, R, Unit, degC, degF, delta_degC, delta_degF, dimensions
from unyt import ( # type: ignore [attr-defined]
K,
R,
Unit,
degC,
degF,
delta_degC,
delta_degF,
dimensions,
)
from unyt._on_demand_imports import _astropy, _h5py, _pint
from unyt._physical_ratios import metallicity_sun, speed_of_light_cm_per_s
from unyt.array import (
Expand Down Expand Up @@ -63,7 +72,7 @@
assert_array_equal_units,
)
from unyt.unit_registry import UnitRegistry
from unyt.unit_symbols import cm, degree, g, m
from unyt.unit_symbols import cm, degree, g, m # type: ignore [attr-defined]


def operate_and_compare(a, b, op, answer):
Expand Down