Open james-pcdr opened 3 months ago
@james-pcdr
Thank you for your interest! Yes, type annotations are something that is missing and would be great to have.
I've tried to add type annotations. My attempts are on the mypy-fixes branch, but it's quite messy and unstructured. I've pushed a new branch mypy-mvp with a minimal example that shows the problem I am stuck at. This minimal example adds type annotations to the classes B
(base class), Pipe
, Map
, and Filter
. The error I encountered is documented in the README of that branch.
I don't know Python types well enough to solve this error. Answers from ChatGPT and Claude didn't help. I think solving this minimal example problem is a good starting point.
One of the goals for this project is to have simple methods, preferably one-liners (if possible, although this requirement can be relaxed). To achieve this, I've put type annotations in a separate pipe21.pyi
stub file instead of the file with actual implementations.
Feel free to ask any follow-up questions, and PRs are welcome!
Thanks for the detailed response!
A few thoughts/disclaimers:
More to follow another day.
I have a few inter-related comments/questions.
class Map(Generic[T, U]):
def __init__(self, f: Callable[[T], U], ta: Optional[Type[T]] = None):
self.f = f
def __ror__(self, x: Iterable[T]) -> Iterator[U]:
return map(self.f, x)
The disadvantage is it requires stating the type in places where mypy struggles, e.g.
["Tuesday", "cheese"] | Map(lambda x: x.endswith("day"), str)
# explicit type ^^^
However, it does have one benefit: you get editor autocompletion as long as you type the second arg first:
Map(lambda x: x.endswi , str)
# ^ imagine the cursor here
Map
looks complicated to me.After some experimentation with mypy, pytype, and pylyzer, I've decided to wait for the Python typing ecosystem to get better before tackling this. Sorry that I didn't find an answer, and feel free to close this issue (or not, in case you want it to remain as an invitation to others).
A few follow-up thoughts:
def types_map_pipe_frozenset(x: str) -> frozenset[int]:
def frozenset_int(it: Iterable[int]) -> frozenset[int]:
return frozenset(it)
return x | Map(int) | Pipe(frozenset_int)
.pyi
file.
Hello, I've been exploring ways to incorporate FP in Python, and I like the syntax of this project. The only downside (IMO) is the lack of type annotations.
Would you be interested in typing-related PRs?
(If the answer is yes, I'll follow-up questions about the details, such as whether to incorporate the existing
mypy-fixes
branch.)Thanks!