-
Notifications
You must be signed in to change notification settings - Fork 116
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
Type checks missed for Callable[[str], str]
#245
Comments
Such type checks would have to compare annotations against annotations which is better covered by static type checkers. It's huge effort to implement such checks in typeguard, and I'm not entirely sure it's possible. This is a duplicate of #12 anyway. |
Hi @agronholm , thanks a lot for your quick reply and for the work on typeguard! |
I totally understand your arguments for why this is not an important issue for typeguard. But let me maybe add some context (which might be interesting for other typeguard users as well).
I agree with you in case the functions are available outside of runtime. In the case, I was looking at that was not the case since the functions were dynamically instantiated based on some config. Of course, there might still be other ways of guaranteeing that the instantiated function objects end up respecting the desired interface. typeguard was just the first thing I found which looked like it might do the trick. I guess a naive but workable alternative could be to just try to call the functions with desired inputs and assert on the output types. |
It would be, but the extraordinary effort required to implement checks of annotations vs annotations was too much.
The vast majority of functions are visible to static type checkers.
What does "instantiating" mean for functions?
Do you mean wrapping the callables with something like |
sorry for confusing language. what I really mean is sth like (*) def get_function(function_name: str) -> Callable[[X], Y]:
"""Returns the function named `function_name` from the library of functions."""
... What I want is a check that what
given the context explained above what I meant was something as naive as candidate = get_function("my_func")
try:
y = candidate(x) # where x is some test object of class X
typeguard.check_type("y", y, Y)
except TypeError:
raise TypeError("function `candidate` is not of expected type Callable[[X], Y]") does that explain better what I had in mind? (*) note that, in reality, |
check_type
applied to callables seems to only care about the number of arguments and not its type.To Reproduce
Expected behavior
The example above should fail at
typeguard.check_type("function", function, Callable[[int], str])
.Additional context
Version: typeguard==2.13.3
What already works are checks that the number of arguments matches the expectation:
The text was updated successfully, but these errors were encountered: