Skip to content

Commit

Permalink
Use inspect to support typeguard version being overridden
Browse files Browse the repository at this point in the history
Summary:
Attempt to solve/mitigate #2043

We stopped pinning typeguard to 2.13.3, but gracefully failing if it doesn't have that version manually changes the version.  This is important so ax can install in a standard way without conflicting with user's other dependencies.

Differential Revision: D51853729
  • Loading branch information
Daniel Cohen authored and facebook-github-bot committed Dec 5, 2023
1 parent 79133c7 commit 2e9971a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions ax/utils/common/kwargs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from typing import Any, Callable, Dict, Iterable, List, Optional

from ax.utils.common.logger import get_logger
from typeguard import check_type
from ax.utils.common.typeutils import version_safe_check_type

logger: Logger = get_logger(__name__)

Expand Down Expand Up @@ -82,7 +82,7 @@ def validate_kwarg_typing(typed_callables: List[Callable], **kwargs: Any) -> Non
# if the keyword is a callable, we only do shallow checks
if not (callable(kw_val) and callable(param.annotation)):
try:
check_type(kw, kw_val, param.annotation)
version_safe_check_type(kw, kw_val, param.annotation)
except TypeError:
message = (
f"`{typed_callable}` expected argument `{kw}` to be of"
Expand Down
16 changes: 15 additions & 1 deletion ax/utils/common/typeutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

from inspect import signature
from typing import Any, Dict, List, Optional, Tuple, Type, TypeVar

import numpy as np

from typeguard import check_type

T = TypeVar("T")
V = TypeVar("V")
Expand Down Expand Up @@ -108,6 +109,19 @@ def checked_cast_to_tuple(typ: Tuple[Type[V], ...], val: V) -> T:
return val


def version_safe_check_type(argname: str, value: T, expected_type: Type[T]) -> None:
"""Excecute the check_type function if it has the expected signature, otherwise
warn. This is done to support newer versions of typeguard with minimal loss
of functionality for users that have dependency conflicts"""
# Get the signature of the check_type function
sig = signature(check_type)
# Get the parameters of the check_type function
params = sig.parameters
# Check if the check_type function has the expected signature
if list(params.keys()) == ["argname", "value", "expected_type"]:
check_type(argname, value, expected_type)


# pyre-fixme[3]: Return annotation cannot be `Any`.
# pyre-fixme[2]: Parameter annotation cannot be `Any`.
def numpy_type_to_python_type(value: Any) -> Any:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"ipywidgets",
# Needed for compatibility with ipywidgets >= 8.0.0
"plotly>=5.12.0",
"typeguard==2.13.3",
"typeguard",
"pyre-extensions",
]

Expand Down

0 comments on commit 2e9971a

Please sign in to comment.