Skip to content

Commit

Permalink
feat(pre-commit): add mypy to pre-commit hook
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Jan 13, 2025
1 parent ff376ea commit 39ac206
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
11 changes: 10 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ repos:
hooks:
- id: clang-format
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.8.6
rev: v0.9.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down Expand Up @@ -63,6 +63,15 @@ repos:
hooks:
- id: codespell
additional_dependencies: [".[toml]"]
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v1.14.1
hooks:
- id: mypy
exclude: |
(?x)(
^tests/|
^setup.py$
)
- repo: local
hooks:
- id: pylint
Expand Down
11 changes: 7 additions & 4 deletions optree/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,10 @@ def __call__(self, metadata: MetaData, children: Children[T], /) -> Collection[T
"""Unflatten the children and metadata back into the container."""


def _override_with_(cxx_implementation: F, /) -> Callable[[F], F]:
def _override_with_(
cxx_implementation: Callable[P, T],
/,
) -> Callable[[Callable[P, T]], Callable[P, T]]:
"""Decorator to override the Python implementation with the C++ implementation.
>>> @_override_with_(any)
Expand All @@ -375,15 +378,15 @@ def _override_with_(cxx_implementation: F, /) -> Callable[[F], F]:
True
"""

def wrapper(python_implementation: F, /) -> F:
def wrapper(python_implementation: Callable[P, T], /) -> Callable[P, T]:
@functools.wraps(python_implementation)
def wrapped(*args: Any, **kwargs: Any) -> Any:
def wrapped(*args: P.args, **kwargs: P.kwargs) -> T:
return cxx_implementation(*args, **kwargs)

wrapped.__cxx_implementation__ = cxx_implementation # type: ignore[attr-defined]
wrapped.__python_implementation__ = python_implementation # type: ignore[attr-defined]

return wrapped # type: ignore[return-value]
return wrapped

return wrapper

Expand Down
1 change: 0 additions & 1 deletion tests/test_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ class SubclassedAutoEntry(optree.AutoEntry):


def test_flattened_entry_call():

@optree.register_pytree_node_class(namespace='namespace')
class MyObject:
def __init__(self, x, y, z):
Expand Down
1 change: 0 additions & 1 deletion tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,6 @@ def test_make_dataclass_with_duplicate_registrations():
TypeError,
match=r'@optree\.dataclasses\.dataclass\(\) cannot be applied to .* more than once\.',
):

optree.dataclasses.dataclass(Foo2, namespace='other-error')

Foo = optree.register_pytree_node_class(namespace='other-namespace')( # noqa: N806
Expand Down
1 change: 0 additions & 1 deletion tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ def is_namedtuple_(obj):
optree.is_namedtuple_instance.__python_implementation__,
),
):

assert not is_namedtuple_((1, 2))
assert not is_namedtuple_([1, 2])
assert not is_namedtuple_(sys.float_info)
Expand Down

0 comments on commit 39ac206

Please sign in to comment.