Skip to content

Commit

Permalink
Now vendoring typeguard v2.13.3
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-kidger committed Nov 13, 2024
1 parent a1a9480 commit 9038fb0
Show file tree
Hide file tree
Showing 6 changed files with 1,300 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.7
rev: v0.7.3
hooks:
- id: ruff-format # formatter
types_or: [ python, pyi, jupyter ]
Expand Down
15 changes: 4 additions & 11 deletions jaxtyping/_pytree_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from typing import Any, Generic, TypeVar

import jax.tree_util as jtu
import typeguard

from ._errors import AnnotationError
from ._storage import (
Expand Down Expand Up @@ -95,14 +94,16 @@ def is_check_leaftype(x):
#
# Deliberately not using @jaxtyped so that we share the same `memo` as
# whatever dynamic context we're currently in.
@typeguard.typechecked
from ._typeguard import typechecked

@typechecked
def accepts_leaftype(x: cls.leaftype):
pass

def is_leaftype(x):
try:
accepts_leaftype(x)
except _TypeCheckError:
except TypeError:
return False
else:
return True
Expand Down Expand Up @@ -249,14 +250,6 @@ class X(PyTree):
return X


try:
# new typeguard
_TypeCheckError = (TypeError, typeguard.TypeCheckError)
except AttributeError:
# old typeguard
_TypeCheckError = TypeError


# Can't do `class PyTree(Generic[_T]): ...` because we need to override the
# instancecheck for PyTree[foo], but subclassing
# `type(Generic[int])`, i.e. `typing._GenericAlias` is disallowed.
Expand Down
19 changes: 19 additions & 0 deletions jaxtyping/_typeguard/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This is the MIT license: http://www.opensource.org/licenses/mit-license.php

Copyright (c) Alex Grönholm

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
11 changes: 11 additions & 0 deletions jaxtyping/_typeguard/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Vendored copy of typeguard v2.13.3

We include a vendored copy of typeguard v2.13.3. The reason we need a runtime typechecker is to be able to define `isinstance(..., PyTree[Foo])`.

Of the available options:

- `beartype` does not support `O(n)` checking.
- `typeguard` v4 is notorious for having some bugs (they seem to re-parse the AST or something?? And then they die on the fact that we have strings in our annotations.)
- `typeguard` v2 is what we use here... but we vendor it instead of depending on it, because people may still wish to use typeguard v4 in their own environments. (Notably a number of other packages depend on this, and it's just inconvenient to be incompatible at the package level, when the combinations which don't mix at runtime might never actually be used.)

This is vendored under the terms of the MIT license, which is also reproduced here.
Loading

0 comments on commit 9038fb0

Please sign in to comment.