Skip to content
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

MAINT: type sph vor function signatures #11955

Closed
wants to merge 2 commits into from

Conversation

tylerjereddy
Copy link
Contributor

  • add type hints to _spherical_voronoi.py module
    function signatures

  • expansion of the ckdtree.pyi stub module with a few
    more call signatures was needed to allow type checking of
    the Cython calls without errors

@tylerjereddy tylerjereddy added scipy.spatial maintenance Items related to regular maintenance tasks static typing Items related to static typing labels Apr 27, 2020
@tylerjereddy
Copy link
Contributor Author

doc build fail is related, somewhat surprisingly

@tylerjereddy
Copy link
Contributor Author

@pmla @person142 thoughts? I haven't looked into this deeply yet, but hopefully an easy fix.

@tylerjereddy
Copy link
Contributor Author

I was able to reproduce the doc build error and fix locally using approach described by an astropy dev.

In short, sphinx can't find Union[numpy.ndarray, List, None]', though I don't think that is surprising?!

Hopefully CI agrees..

class cKDTree:
def __init__(self,
data: np.ndarray,
leafsize: int = 16,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In stubs like this it's conventional to leave the actual default values out, so this would become leafsize: int = .... That prevents you from having to keep them in-sync with the real code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've made this change for arguments in the header/stub file.

r: float,
p: float = 2.,
eps: float = 0,
output_type: str = 'set') -> Union[set, np.ndarray]: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So with this you can get a more precise return type by using @overload and Literal. So that would look something like

from typing import overload, Set
from typing_extensions import Literal

@overload
def query_pairs(
    ....
) -> Set: ...

@overload
def query_pairs(
    ....
    output_type: Literal['set']
) -> Set: ...

@overload
def query_pairs(
    ...
    output_type: Literal['ndarray']
) -> np.ndarray: ...

The numpy-stubs already require typing_extensions, so we're fine using them.

Generally unions in return types tend to be a pain (all calling code needs to be prepared to deal with all return types or use ignores, even when you really know what the return type is), so it's best to avoid them if at all possible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, I think I disagree--I tend to side with the confusion observed in this mypy issue:
python/mypy#5407

I spent ~20 minutes trying stuff and getting the same issue with Overloaded function signature 2 will never be matched.

This seems rather verbose and complex and mypy currently passes, so I'd rather not do that for now--maybe cleanup in the future?

* add type hints to `_spherical_voronoi.py` module
function signatures

* expansion of the `ckdtree.pyi` stub module with a few
more call signatures was needed to allow type checking of
the Cython calls without errors

* sphinx can't find `Union[numpy.ndarray, List, None]` so
we ignore this link using the approach descrbied by an
astropy dev here:
https://stackoverflow.com/a/30624034/2942522
* don't use default values in type hint header
files to reduce maintenance burden
@MartinThoma
Copy link
Contributor

MartinThoma commented Jul 7, 2020

Just a question; might have been answered elsewhere: When does scipy use type annotations directly in the file and when a .pyi stub module? What is the reason for mixing both styles? Is it just for fixing Cython errors?

@rgommers
Copy link
Member

rgommers commented Jul 7, 2020

@MartinThoma in Python code it can be inline, in compiled code it can't.

@tylerjereddy
Copy link
Contributor Author

tylerjereddy commented Aug 30, 2023

I'll let someone else drive this forward if it hasn't already been taken care of. A type hint PR that has been open for three years might as well be closed, and it is mine anyways so shouldn't annoy anyone.

@tylerjereddy tylerjereddy deleted the sph_vor_type_hints_defs branch August 30, 2023 19:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maintenance Items related to regular maintenance tasks scipy.spatial static typing Items related to static typing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants