Skip to content
Open
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
1 change: 1 addition & 0 deletions .devcontainer/codespace.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/python-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions plugins/python/python/phlex/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
5 changes: 3 additions & 2 deletions plugins/python/python/phlex/_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 29 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
[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 = [
]
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/
Expand All @@ -17,22 +32,29 @@ 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"
]
exclude = [
"**/.venv/**",
"**/py_virtual_env/**",
"**/build/_deps/**",
"**/out/build/*/_deps/**"
"**/build/**",
"**/out/**",
"**/_deps/**",
"**/node_modules/**",
]
ignore = [
"**/.venv/**",
Expand Down
10 changes: 6 additions & 4 deletions test/python/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,6 +12,8 @@

from phlex import Variant, count_optional_arguments, normalize_type

foo: Any


class TestTYPING:
"""Tests for phlex-specific typing."""
Expand Down Expand Up @@ -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."""
Expand All @@ -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])
Expand Down
1 change: 1 addition & 0 deletions test/python/test_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading