Conversation
|
In this process, I realize I can add typing and register an operation very cleanly as a function! Going from this: class GetItem(matchpy.Operation):
name = "GetItem"
arity = matchpy.Arity(1, True)
# then in .pyi file
def GetItem(seq: CArray) -> CGetitem:
...To this: @operation
def GetItem(length: CContent, getitem: CGetitem) -> CArray:
...And `operation is pretty simple as well: T = typing.TypeVar("T", bound=typing.Callable)
def operation(fn: T) -> T:
"""
Register a matchpy operation for a function.
The function should have a fixed number of args and one return.
This can also be done manually, by typing the result of `matchpy.Operation.new`
as a callable, but this is more fluent.
"""
sig = inspect.signature(fn)
return matchpy.Operation.new(fn.__name__, matchpy.Arity(len(sig.parameters), True)) |
|
Trick is understanding that all this is just for compile time checking, not runtime. |
c619514 to
3e7b0bc
Compare
|
OK this should be ready to merge. I have added some tests, but no CI yet. The tests just run some notebooks, run mypy and verify the readme works. |
|
Closes #25 |
|
Magic comments only work in the PR description, not in another comment. Might be wise to add it to the PR description. |
3e7b0bc to
5381243
Compare
|
CI is working now on this and it is ready to merge. |
hameerabbasi
left a comment
There was a problem hiding this comment.
I was wondering about the naming convention for MyPy types... Would it be better to have a typing module like Python?
|
@hameerabbasi good idea, I moved the types into separate files. How is that? |
|
It's better, I was also wondering about the naming convention, what does the It'd be nice to have all of them in a simple Examples of both: |
|
C stands for Category. All the names will need to changed, i have gotten feedback from a couple people that the resemblance to Pythons naming (GetItem, Sequence) is confusing. At least at the moment within this PR it is internally consistent that all compile time types are prefixed with C. I expect the final look of how we write these to change. I'll add a few lines to the readme to explain how they are currently. |
|
Perhaps have a |
|
Sounds good. Added that. |
|
I am gonna merge this in so that I can share the current state of the repo with others for feedback more easily. |
Thanks to a helpful discussion with @dcharbon on Friday, I have started outlining how this system works.
replace_scanFixes #25