-
Notifications
You must be signed in to change notification settings - Fork 74
Open
Description
As an (temporary?) alternative to full support for Python typing (#69), I'd like to propose adding multipledispatch.TypedList. The use case here is separate functions for different list subtypes, e.g., lists of strings vs lists of integers. See pydata/xarray#1938 for discussion.
I have an example implementation here and am happy to work on putting together a PR if desired:
https://colab.research.google.com/drive/18zdyUpWLNFzFaz08GUOC5vs1GxE_jHg-#scrollTo=XDL0cBeS-lub
Example usage:
@dispatch(TypedList[int])
def f(args):
print('integers:', args)
@dispatch(TypedList[str])
def f(args):
print('strings:', args)
@dispatch(TypedList[str, int])
def f(args):
print('mixed str-int:', args)
f([1, 2]) # integers: [1, 2]
f([1, 2, 'foo']) # mixed str-int: [1, 2, 'foo']
f(['foo', 'bar']) # strings: ['foo', 'bar']
f([[1, 2]]) # NotImplementedError: Could not find signature for f: <TypedList[list]>The exact public facing API is up for discussion. I'm tentatively calling this TypedList for clarity and to distinguish this from typing.List (this is actually equivalent to typing.List[typing.Union[...]).
Metadata
Metadata
Assignees
Labels
No labels