-
Notifications
You must be signed in to change notification settings - Fork 8
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 script to generate the stub file #44
base: main
Are you sure you want to change the base?
Conversation
I'm not sure what's the best location for the script. I ended up pulling it outside the |
Thanks @gahjelle that is a good idea!
Out of curiosity, did you try or consider using I'm fine with either solution. If using a script, I'd slightly prefer using Python code for the specs instead of parsing comments (e.g., in Xarray generate_ops.py. The script can be part of the code ; I think that for now it is good enough to run it manually when needed (we don't need to re-generate the type stubs unless we change the specs) and then let black format the generated file via pre-commit. |
Thanks @benbovy No, we didn't think about or explore using a I've done an update to the generator, so that we're listing the specs explicitly in Python code instead of parsing the comments in the But I'm fine with abandoning this script and working with the When you say "The script can be part of the code", do you mean that we should move it into the Thanks again for your feedback. |
Maybe we could define optional parameter types as generic, e.g., something like this: _NameType = TypeVar("_NameType", bound=str)
_ScalarReturnType = TypeVar("_ScalarReturnType", bound=Any)
_ArrayReturnDType = TypeVar("_ArrayReturnDType", bound=Any)
_KwargsType = TypeVar("_KwargsType", bound=TypedDict)
class _VFunc_Nin2_Nout1(Generic[_NameType, _ScalarReturnType, _ArrayReturnDType, _KwargsType]):
...
@overload
def __call__(
self,
a: Geography,
b: Geography,
**kwargs: Unpack[_KwargsType],
) -> _ScalarReturnType: ...
...
class DistanceKwargs(TypedDict):
radius: float
distance: _VFunc_Nin2_Nout1[Literal["distance"], float, float, DistanceKwargs]
Yes I think it is fine moving it there. |
This is a draft for a script that can generate the
spherely.pyi
stub file.During the EuroSciPy 2024 sprint, I worked with @jorisvandenbossche on #43 . We realized that we need several different
_VFunc_*
types to cover the different overloaded arguments for functions going forward. We tried a few different approaches to avoid too much boilerplate/copy-paste code, including ways of creating dynamic types but didn't find anything that was well supported by the type checkers.Instead, the following script can automatically generate code for such classes. It works by looking a
# /// Begin types
marker and lines defining which types should be added. For example, to create a type that takes 2 geography inputs, as well as an optionalradius
parameter of typefloat
, we can add the specification line:Running the script will then update the code in-place between the
# /// Begin types
and the corresponding# /// End types
marker. Any existing edits between those markers will be overwritten, but the rest of the file is left alone.