-
Notifications
You must be signed in to change notification settings - Fork 275
Global version guard is not applied in functions #1165
Copy link
Copy link
Closed
astral-sh/ruff
#24179Labels
Milestone
Description
Summary
I tried adding ty to svcs in hynek/svcs#141 since it's heavy on typing stuff and I've encountered this: https://play.ty.dev/004edf8e-54d0-4a7c-897b-78ebcfa2cdf8
Basically I want to define a function depending on the Python version and use different APIs. If I pull the version_info check into the function, it passes.
import sys, inspect
from collections.abc import Callable
if sys.version_info >= (3, 10):
def _robust_signature(factory: Callable) -> inspect.Signature | None:
try:
# Provide the locals so that `eval_str` will work even if the user
# places the `Container` under a `if TYPE_CHECKING` block.
sig = inspect.signature(
factory,
locals={"Container": object()},
eval_str=True,
)
except Exception: # noqa: BLE001
# Retry without `eval_str` since if the annotation is "svcs.Container"
# the eval will fail due to it not finding the `svcs` module
try:
sig = inspect.signature(factory)
except Exception: # noqa: BLE001
return None
return sig
else:
def _robust_signature(factory: Callable) -> inspect.Signature | None:
try:
return inspect.signature(factory)
except Exception: # noqa: BLE001
return NoneVersion
ty 0.0.1-alpha.20 (f41f00a 2025-09-03)
Reactions are currently unavailable