diff --git a/.devcontainer/codespace.code-workspace b/.devcontainer/codespace.code-workspace index 81a554751..7e55375a5 100644 --- a/.devcontainer/codespace.code-workspace +++ b/.devcontainer/codespace.code-workspace @@ -62,6 +62,7 @@ "**/build/CMakeFiles/**": true }, "python.languageServer": "Pylance", + "python.analysis.diagnosticMode": "workspace", "python.terminal.activateEnvironment": false, // Container-only interpreter paths (python.defaultInterpreterPath, // esbonio.server.pythonPath) are intentionally not set here: this file diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index af90e7b00..c8762eb2a 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -43,6 +43,7 @@ ], "kilocode.new.extraCaCerts": "", "python.defaultInterpreterPath": "/opt/spack-environments/phlex-ci/.spack-env/view/bin/python", + "python.analysis.diagnosticMode": "workspace", "python.languageServer": "Pylance", "python.terminal.activateEnvironment": false, "esbonio.server.pythonPath": "/opt/spack-environments/phlex-ci/.spack-env/view/bin/python", diff --git a/.github/workflows/python-check.yaml b/.github/workflows/python-check.yaml index 0924a37a5..075ee3f8a 100644 --- a/.github/workflows/python-check.yaml +++ b/.github/workflows/python-check.yaml @@ -287,7 +287,14 @@ jobs: working-directory: ${{ needs.setup.outputs.checkout_path }} run: | echo "➡️ Checking Python code with MyPy..." - mypy --config-file "$RUNNER_TEMP/workflow-tools/pyproject.toml" --color-output . + export MYPYPATH="$PWD/plugins/python/python:$PWD/scripts:$PWD/test/python" + mypy --config-file "$RUNNER_TEMP/workflow-tools/pyproject.toml" --color-output \ + scripts \ + ci \ + plugins/python/python \ + test/python \ + .devcontainer/configure-host.py \ + docs/source/conf.py continue-on-error: true - name: Evaluate Python checks result diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 99dd1850f..0d6fa367d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,6 +20,8 @@ repos: types_or: [python, pyi] args: [--enable-incomplete-feature=NewGenericSyntax] additional_dependencies: + - numpy + - pytest - types-PyYAML - repo: https://github.com/astral-sh/ruff-pre-commit rev: v0.16.0 diff --git a/plugins/python/python/phlex/_typing.py b/plugins/python/python/phlex/_typing.py index 92ed86be4..1027e02f8 100644 --- a/plugins/python/python/phlex/_typing.py +++ b/plugins/python/python/phlex/_typing.py @@ -235,7 +235,7 @@ def normalize_type(tp: Any, globalns: Dict | None = None, localns: Dict | None = return repr(tp) -def count_optional_arguments(pycall: Callable[..., Any]) -> int: +def count_optional_arguments(pycall: object) -> int: """Count the number of optional arguments that the callable will accept. Uses the inspect module to determine the number of defaulted optional @@ -250,12 +250,12 @@ def count_optional_arguments(pycall: Callable[..., Any]) -> int: Number of optional arguments. """ try: - pycall = pycall.phlex_callable # type: ignore[attr-defined] + pycall = typing.cast(Any, pycall).phlex_callable except AttributeError: pass # not a Variant-style type callable try: - sig = inspect.signature(pycall) + sig = inspect.signature(typing.cast(Callable[..., Any], pycall)) except (ValueError, TypeError): return 0 # unknown or not a callable diff --git a/plugins/python/python/phlex/_variant.py b/plugins/python/python/phlex/_variant.py index 162939894..77cd4830c 100644 --- a/plugins/python/python/phlex/_variant.py +++ b/plugins/python/python/phlex/_variant.py @@ -8,7 +8,8 @@ import collections import copy import inspect -from typing import Any, Callable +from collections.abc import Callable, Mapping +from typing import Any __all__ = [ "MissingAnnotation", @@ -57,7 +58,7 @@ class Variant: def __init__( self, f: Callable, - annotations: dict[str, str | type | Any], + annotations: Mapping[str, str | type | Any], name: str, clone: bool | str = False, allow_call: bool = False, diff --git a/pyproject.toml b/pyproject.toml index 875dca1b1..c73ac413a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,9 @@ [tool.mypy] -ignore_missing_imports = true +python_version = "3.12" +check_untyped_defs = true +exclude_gitignore = true +explicit_package_bases = true +mypy_path = "$MYPY_CONFIG_FILE_DIR/plugins/python/python:$MYPY_CONFIG_FILE_DIR/scripts:$MYPY_CONFIG_FILE_DIR/test/python" no_implicit_optional = true plugins = [ ] @@ -7,6 +11,17 @@ show_error_codes = true warn_unreachable = true disable_error_code = "annotation-unchecked" +[[tool.mypy.overrides]] +module = [ + "cppyy", + "cppyy.*", + "numba", + "numba.*", + "spack", + "spack.*", +] +ignore_missing_imports = true + [tool.pyright] # NOTE: this table is Pylance/Pyright's highest-precedence configuration # source: once it exists, python.analysis.extraPaths (and include/exclude/ @@ -17,13 +32,18 @@ disable_error_code = "annotation-unchecked" # here. The Spack-specific absolute paths only exist inside the phlex-dev # devcontainer and are harmless no-ops elsewhere. -diagnosticMode = "workspace" +include = [ + "scripts", + "ci", + ".devcontainer", + "plugins/python/python", + "test/python", + "docs/source/conf.py", +] +pythonVersion = "3.12" extraPaths = [ "plugins/python/python", "scripts", - "build/_deps", - "out/build/default/_deps", - "out/build/clang-tidy/_deps", "/opt/spack-environments/phlex-ci/.spack-env/view/lib/root", "/opt/spack-environments/phlex-ci/.spack-env/view/lib/python/site-packages", "/spack/lib/spack" @@ -31,8 +51,10 @@ extraPaths = [ exclude = [ "**/.venv/**", "**/py_virtual_env/**", - "**/build/_deps/**", - "**/out/build/*/_deps/**" + "**/build/**", + "**/out/**", + "**/_deps/**", + "**/node_modules/**", ] ignore = [ "**/.venv/**", diff --git a/test/python/test_typing.py b/test/python/test_typing.py index 2fe0aef1c..aa24a0b12 100644 --- a/test/python/test_typing.py +++ b/test/python/test_typing.py @@ -3,7 +3,7 @@ import ctypes import typing -from typing import Dict, List +from typing import Any, Dict, List import numpy as np import numpy.typing as npt @@ -12,6 +12,8 @@ from phlex import Variant, count_optional_arguments, normalize_type +foo: Any + class TestTYPING: """Tests for phlex-specific typing.""" @@ -44,14 +46,14 @@ def test_list_normalization(self): for t in types + [np_type]: assert normalize_type(norm) == norm try: - assert normalize_type(List[t]) == norm + assert normalize_type(List[t]) == norm # type: ignore[valid-type] except SyntaxError as e: # some of the above are not legal Python when used with List, # but are fine when used with list; just ignore these as they # will not show up in user code if "Forward reference must be an expression" not in str(e): raise - assert normalize_type(list[t]) == norm + assert normalize_type(list[t]) == norm # type: ignore[valid-type] def test_numpy_array_normalization(self): """Normalization of standard Numpy typing.""" @@ -68,7 +70,7 @@ def test_numpy_array_normalization(self): (np.float32, "float"), (np.float64, "double"), ): - assert normalize_type(npt.NDArray[t]) == "ndarray[" + s + "]" + assert normalize_type(npt.NDArray[t]) == "ndarray[" + s + "]" # type: ignore[valid-type] # ndarray annotations without a concrete dtype must raise TypeError raises(TypeError, normalize_type, np.ndarray[typing.Any]) diff --git a/test/python/test_variant.py b/test/python/test_variant.py index fe65cecf4..d5293c4da 100644 --- a/test/python/test_variant.py +++ b/test/python/test_variant.py @@ -72,6 +72,7 @@ def __call__(self): c = Container() wrapper = Variant(c, {}, "deep_clone", clone="deep") assert id(wrapper.phlex_callable) != id(c) + assert isinstance(wrapper.phlex_callable, Container) assert id(wrapper.phlex_callable.data) != id(c.data) def test_missing_annotation_raises(self):