Skip to content

Commit

Permalink
update using vendor.py mechanism
Browse files Browse the repository at this point in the history
  • Loading branch information
mmacpherson committed Feb 1, 2025
1 parent 52528b7 commit 57bf1fd
Show file tree
Hide file tree
Showing 7 changed files with 1,220 additions and 541 deletions.
15 changes: 0 additions & 15 deletions metaflow/_vendor/typeguard/README.md

This file was deleted.

4 changes: 2 additions & 2 deletions metaflow/_vendor/typeguard/_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
from metaflow._vendor import typing_extensions

# Must use this because typing.is_typeddict does not recognize
# TypedDict from metaflow._vendor.typing_extensions, and as of version 4.12.0
# TypedDict from typing_extensions, and as of version 4.12.0
# typing_extensions.TypedDict is different from typing.TypedDict
# on all versions.
from metaflow._vendor.typing_extensions import is_typeddict
Expand Down Expand Up @@ -68,7 +68,7 @@
from importlib.metadata import entry_points
from typing import ParamSpec
else:
from importlib_metadata import entry_points
from metaflow._vendor.importlib_metadata import entry_points
from metaflow._vendor.typing_extensions import ParamSpec

TypeCheckerCallable: TypeAlias = Callable[
Expand Down
2 changes: 1 addition & 1 deletion metaflow/_vendor/typeguard/_importhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
if sys.version_info >= (3, 10):
from importlib.metadata import PackageNotFoundError, version
else:
from importlib_metadata import PackageNotFoundError, version
from metaflow._vendor.importlib_metadata import PackageNotFoundError, version

try:
OPTIMIZATION = "typeguard" + "".join(version("typeguard").split(".")[:3])
Expand Down
127 changes: 127 additions & 0 deletions metaflow/_vendor/typeguard/_pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
from __future__ import annotations

import sys
import warnings
from typing import TYPE_CHECKING, Any, Literal

from metaflow._vendor.typeguard._config import CollectionCheckStrategy, ForwardRefPolicy, global_config
from metaflow._vendor.typeguard._exceptions import InstrumentationWarning
from metaflow._vendor.typeguard._importhook import install_import_hook
from metaflow._vendor.typeguard._utils import qualified_name, resolve_reference

if TYPE_CHECKING:
from pytest import Config, Parser


def pytest_addoption(parser: Parser) -> None:
def add_ini_option(
opt_type: (
Literal["string", "paths", "pathlist", "args", "linelist", "bool"] | None
),
) -> None:
parser.addini(
group.options[-1].names()[0][2:],
group.options[-1].attrs()["help"],
opt_type,
)

group = parser.getgroup("typeguard")
group.addoption(
"--typeguard-packages",
action="store",
help="comma separated name list of packages and modules to instrument for "
"type checking, or :all: to instrument all modules loaded after typeguard",
)
add_ini_option("linelist")

group.addoption(
"--typeguard-debug-instrumentation",
action="store_true",
help="print all instrumented code to stderr",
)
add_ini_option("bool")

group.addoption(
"--typeguard-typecheck-fail-callback",
action="store",
help=(
"a module:varname (e.g. typeguard:warn_on_error) reference to a function "
"that is called (with the exception, and memo object as arguments) to "
"handle a TypeCheckError"
),
)
add_ini_option("string")

group.addoption(
"--typeguard-forward-ref-policy",
action="store",
choices=list(ForwardRefPolicy.__members__),
help=(
"determines how to deal with unresolveable forward references in type "
"annotations"
),
)
add_ini_option("string")

group.addoption(
"--typeguard-collection-check-strategy",
action="store",
choices=list(CollectionCheckStrategy.__members__),
help="determines how thoroughly to check collections (list, dict, etc)",
)
add_ini_option("string")


def pytest_configure(config: Config) -> None:
def getoption(name: str) -> Any:
return config.getoption(name.replace("-", "_")) or config.getini(name)

packages: list[str] | None = []
if packages_option := config.getoption("typeguard_packages"):
packages = [pkg.strip() for pkg in packages_option.split(",")]
elif packages_ini := config.getini("typeguard-packages"):
packages = packages_ini

if packages:
if packages == [":all:"]:
packages = None
else:
already_imported_packages = sorted(
package for package in packages if package in sys.modules
)
if already_imported_packages:
warnings.warn(
f"typeguard cannot check these packages because they are already "
f"imported: {', '.join(already_imported_packages)}",
InstrumentationWarning,
stacklevel=1,
)

install_import_hook(packages=packages)

debug_option = getoption("typeguard-debug-instrumentation")
if debug_option:
global_config.debug_instrumentation = True

fail_callback_option = getoption("typeguard-typecheck-fail-callback")
if fail_callback_option:
callback = resolve_reference(fail_callback_option)
if not callable(callback):
raise TypeError(
f"{fail_callback_option} ({qualified_name(callback.__class__)}) is not "
f"a callable"
)

global_config.typecheck_fail_callback = callback

forward_ref_policy_option = getoption("typeguard-forward-ref-policy")
if forward_ref_policy_option:
forward_ref_policy = ForwardRefPolicy.__members__[forward_ref_policy_option]
global_config.forward_ref_policy = forward_ref_policy

collection_check_strategy_option = getoption("typeguard-collection-check-strategy")
if collection_check_strategy_option:
collection_check_strategy = CollectionCheckStrategy.__members__[
collection_check_strategy_option
]
global_config.collection_check_strategy = collection_check_strategy
21 changes: 0 additions & 21 deletions metaflow/_vendor/typeguard/patch_typeguard.sh

This file was deleted.

Loading

0 comments on commit 57bf1fd

Please sign in to comment.