-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
Add type hints #76
Comments
I even noticed that you had already added mypy to the requirements. Just need to do static typing. Lines 3 to 4 in dd179c8
|
Probably blocked by python/mypy#11833, as I'll need something like: P = ParamSpec("P")
I = TypeVar("I")
O = TypeVar("O")
class Pipe(Generic[P, I, O]):
def __init__(self, function: Callable[Concatenate[Iterable[I], P], Iterator[O]]) -> None:
... as later the But yes I agree it would be very nice to be able to write something like: T = TypeVar("T")
take: Pipe[T, int, T] to express than |
Now blocked by mypy issue 1484 as we need a way to cleanly decribe functools.partial, to properly describe:
|
It's more complex than I thought 😳 |
I'm pretty sure this is nearly impossible without being able to represent HKTs (Higher-Kinded Types) in the Python type system. |
@JulienPalard and @devRMA, would you share the code you write so far in a branch or fork? I would love to take a swing at it and try to help you. |
@fabiob pushed my What we really need is a clean way to type |
Seems like python/mypy#1484 was completed with python/mypy#16939. Does the implementation provide what you need? |
I don't think so, they implemented a mypy plugin to handle the single Looks like there's still no way to type annotate |
Currently this works: from collections.abc import Callable
from typing import Concatenate, Generic, ParamSpec, TypeVar
P = ParamSpec('P')
T = TypeVar('T')
U = TypeVar('U')
class Pipe(Generic[U, P, T]):
def __init__(self, function: Callable[Concatenate[U, P], T]):
self.function = function
def run(self, arg1: U) -> T:
return self.function(arg1)
def simple_annotated_function(value: int) -> str:
assert isinstance(value, int)
return "hello"
def main() -> None:
p = Pipe(simple_annotated_function)
reveal_type(p) # demo.Pipe[builtins.int, [], builtins.str]
reveal_type(p.function) # def (builtins.int) -> builtins.str
reveal_type(p.run) # def (arg1: builtins.int) -> builtins.str
p.run("coucou") # Argument 1 to "run" of "Pipe" has incompatible type "str"; expected "int"
main() But it feels like it's only 1/10 of the work, as we need some way to "substract" parameters from a paramspec. |
Starting a new discussion here: python/mypy#17481 to track this. |
Use a static type checker called mypy. In libs, I think it's very important to have type hints, so that when users are going to use the lib, code linters can help, knowing exactly what kind of parameters to receive, and what the return type is.
The ideal solution would be to add type hints as specified by PEP 484, PEP 526, PEP 544, PEP 586, PEP 589, and PEP 591 directly on your code.
The text was updated successfully, but these errors were encountered: