Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use inspect to support typeguard version being overridden #2044

Closed
Closed
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
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
17 changes: 16 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,20 @@ 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
params = set(params.keys())
if all(arg in params for arg in ["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