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

function compatability check #12

Open
d-chambers opened this issue Feb 4, 2017 · 5 comments
Open

function compatability check #12

d-chambers opened this issue Feb 4, 2017 · 5 comments

Comments

@d-chambers
Copy link

Awesome project, I think it will be very useful.
One question, is there anything that might be used to check the compatibility between two functions?
I am imagining something like this:


from typing import List, Any, Sequence
from typeguard import functions_compatible


def f1(var1: int) -> List[int]:
    return [var1]


def f2(var1: List[int]) -> Any:
    return var1
    
    
functions_compatible(f1, f2)  # True 
functions_compatible(f2, f1)  # True,  because Any can be int
functions_compatible(f2, f1, strict=True)  # False, because Any is not always int

I am working on a configurable data pipeline that is meant to run for a very long time, but the exact function connections are not always known until import time or later.

@agronholm
Copy link
Owner

I had this planned for the future, but it's a considerable amount of work and I haven't had much time to dedicate to anything except for bug fixing.

@d-chambers
Copy link
Author

Ok thanks,
If I get some time in the next few weeks I will dig into the code and see if I cant help out with this.

@Stewori
Copy link

Stewori commented May 20, 2018

This looks related to the functionality of @OverRide in pytypes, see https://github.com/Stewori/pytypes#usage-example. I think what you are asking for is already done there internally. It should be doable to make the right pieces available in public API. This still requires some thought, but given what's already there it should be a comparably low hanging fruit.

@agronholm
Copy link
Owner

I just revisited this issue. What is the intended use case here? The closest thing on my agenda is #84.

@antonagestam
Copy link
Contributor

typeguard mainly deals with runtime type checking of function arguments. To check whether arguments are compatible for a function call, we want to know if they are of the correct type. What's asked for in this issue is different, here instead we want to know whether two static types have subtype relationship. I.e. were comparing two types, instead of comparing an instance and a type.

Since that's a whole different problem realm, I'll make the argument that it'd be feature-creep to add it to typeguard, and that it's better solved elsewhere.

You can get something sort of close in this way:

from typing import List, Any, Sequence
from typeguard import functions_compatible


def f1(var1: int) -> List[int]:
    return [var1]


def f2(var1: List[int]) -> Any:
    return var1
    
    
functions_compatible(f1, f2)  # True 
functions_compatible(f2, f1)  # True,  because Any can be int
functions_compatible(f2, f1, strict=True)  # False, because Any is not always int

It's worth noting that essentially nothing from current typeguard implementation could help in making the subtype check more feature-complete. To support arbitrary types, you'd essentially need to implement a full static type checker, or do something crazy like calling into mypy from runtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants