[X] I have searched the existing issues and didn't find my bug already reported there
[X] I have checked that my bug is still present in the latest release
Typeguard version
4.1.5
Python version
3.11.8
What happened?
Passing a Callable with Optional args to another function, then calling it, results in a TypeCheckError:
TypeCheckError: argument "fn" (function) has too many mandatory positional arguments in its declaration; expected 0 but 1 mandatory positional argument(s) declared
from typing import Callable, Optional
# ✅ type-checks fine
def _fn0(fn: Callable[[int], bool], s: int) -> bool:
return fn(s)
def fn0(s: int) -> bool:
return True
# ❌ TypeCheckError: argument "fn" (function) has too many mandatory positional arguments in its declaration; expected 0 but 1 mandatory positional argument(s) declared
def _fn1(fn: Callable[[Optional[int]], bool], s: Optional[int]) -> bool:
return fn(s)
def fn1(s: Optional[int]) -> bool:
return True
def test_fn0():
"""Passing a Callable (``fn0``) works, as long as its arguments are not ``Optional``."""
_fn0(fn0, 123) # ✅ OK
def test_fn1():
"""Functions that take optional arguments are not type-checked properly"""
_fn1(fn1, 123) # ❌ TypeCheckError: argument "fn" (function) has too many mandatory positional arguments in its declaration; expected 0 but 1 mandatory positional argument(s) declared
Found the problem (here): the check for Optional was missing the additional condition that its slice needs to have been erased for the entire node to be removed.
Things to check first
[X] I have searched the existing issues and didn't find my bug already reported there
[X] I have checked that my bug is still present in the latest release
Typeguard version
4.1.5
Python version
3.11.8
What happened?
Passing a
Callable
withOptional
args to another function, then calling it, results in aTypeCheckError
:See test_callables.py:
How can we reproduce the bug?
ryan-williams/typeguard-issues has a working repro; here's the error in Github Actions.