Skip to content

Commit 8a16d16

Browse files
ConchylicultorThe kauldron Authors
authored andcommitted
Add typeguard in open-source
PiperOrigin-RevId: 726396947
1 parent 205b34e commit 8a16d16

File tree

5 files changed

+10
-15
lines changed

5 files changed

+10
-15
lines changed

.github/workflows/pytest_and_autopublish.yml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ jobs:
3939
# * sweep_utils_test: Depends on kxm
4040
# * lpips_test: Missing VGG weights
4141
# * partial_loader_test: Orbax partial checkpoint loader not yet open-sourced (TODO(epot): Restore)
42-
# * typing tests: Not yet supported due to typeguard version issues.
4342
- name: Run core tests
4443
run: |
4544
pytest -vv -n auto \
@@ -48,9 +47,7 @@ jobs:
4847
--ignore=kauldron/xm/ \
4948
--ignore=kauldron/metrics/lpips_test.py \
5049
--ignore=kauldron/checkpoints/partial_loader_test.py \
51-
--ignore=kauldron/utils/sweep_utils_test.py \
52-
--ignore=kauldron/typing/shape_spec_test.py \
53-
--ignore=kauldron/typing/type_check_test.py
50+
--ignore=kauldron/utils/sweep_utils_test.py
5451
5552
# Auto-publish when version is increased
5653
publish-job:

kauldron/typing/shape_spec.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def _assert_caller_is_typechecked_func() -> None:
8383
if stack[i + 1].function != "_reraise_with_shape_info":
8484
caller_name = stack[i].function
8585
raise AssertionError(
86-
"Dim and Shape not yet supported due to `typeguard` issue."
87-
f" Raised in {caller_name!r}"
86+
"Dim and Shape only work inside of @typechecked functions. But"
87+
f" {caller_name!r} lacks @typechecked."
8888
)
8989

9090

kauldron/typing/type_check.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,10 @@ def check_type(
4646
expected_type: Any,
4747
) -> None:
4848
"""Ensure that value matches expected_type, alias for typeguard.check_type."""
49-
if True: # Typeguard not yet supported
50-
return
5149
return typeguard.check_type(value, expected_type)
5250

5351

54-
exc_cls = Exception
55-
class TypeCheckError(exc_cls):
52+
class TypeCheckError(typeguard.TypeCheckError):
5653
"""Indicates a runtime typechecking error from the @typechecked decorator."""
5754

5855
def __init__(
@@ -112,9 +109,6 @@ def _annotation_repr(ann: Any) -> str:
112109

113110
def typechecked(fn):
114111
"""Decorator to enable runtime type-checking and shape-checking."""
115-
if True: # Typeguard not yet supported
116-
return fn
117-
118112
if hasattr(fn, "__wrapped__"):
119113
raise AssertionError("@typechecked should be the innermost decorator")
120114

@@ -469,3 +463,7 @@ def add_custom_checker_lookup_fn(lookup_fn):
469463
break
470464
else: # prepend
471465
checker_lookup_fns[:0] = [lookup_fn]
466+
467+
468+
add_custom_checker_lookup_fn(_array_spec_checker_lookup)
469+
add_custom_checker_lookup_fn(_dataclass_checker_lookup)

kauldron/typing/type_check_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import jaxtyping as jt
1818
from kauldron.typing import Float, TypeCheckError, typechecked # pylint: disable=g-multiple-import,g-importing-member
19-
from kauldron.typing import type_check
19+
from kauldron.typing import type_check # pylint: disable=g-bad-import-order
2020
import numpy as np
2121
import pytest
2222

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ dependencies = [
4444
"tqdm",
4545
# TODO(klausg): Restore typeguard or switch to something else
4646
# closest match to the internal typeguard
47-
# "typeguard@git+https://github.com/agronholm/typeguard@0dd7f7510b7c694e66a0d17d1d58d185125bad5d",
47+
"typeguard",
4848
"typing_extensions",
4949
"xmanager",
5050
# lazy deps (should ideally remove those)

0 commit comments

Comments
 (0)