Skip to content

resampy: more accurate and dynamic TypeVar - #15926

Open
hunterhogan wants to merge 1 commit into
python:mainfrom
hunterhogan:shape
Open

resampy: more accurate and dynamic TypeVar#15926
hunterhogan wants to merge 1 commit into
python:mainfrom
hunterhogan:shape

Conversation

@hunterhogan

Copy link
Copy Markdown
Contributor

If integer, converted to float32. If floating, then it copies the dtype.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@donbarbos donbarbos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! Just a few comments.


# np.floating[Any] because precision is not important
_FloatArray = TypeVar("_FloatArray", bound=np.ndarray[tuple[int, ...], np.dtype[np.floating[Any]]])
_Floating = TypeVar("_Floating", bound=np.floating[Any])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The comment is still relevant

Suggested change
_Floating = TypeVar("_Floating", bound=np.floating[Any])
# np.floating[Any] because precision is not important
_Floating = TypeVar("_Floating", bound=np.floating[Any])

Comment on lines +38 to +50
@overload
def resample(
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]] | np.ndarray[_Shape, np.dtype[_Floating]],
sr_orig: float,
sr_new: float,
axis: int = -1,
filter: _FilterType = "kaiser_best",
parallel: bool = False,
*,
num_zeros: int = 64,
precision: int = 9,
rolloff: float = 0.945,
) -> np.ndarray[_Shape, np.dtype[np.float32]] | np.ndarray[_Shape, np.dtype[_Floating]]: ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm not sure we need the third overload here, since it seems to be already covered by the first two.

Suggested change
@overload
def resample(
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]] | np.ndarray[_Shape, np.dtype[_Floating]],
sr_orig: float,
sr_new: float,
axis: int = -1,
filter: _FilterType = "kaiser_best",
parallel: bool = False,
*,
num_zeros: int = 64,
precision: int = 9,
rolloff: float = 0.945,
) -> np.ndarray[_Shape, np.dtype[np.float32]] | np.ndarray[_Shape, np.dtype[_Floating]]: ...

Comment on lines +78 to +90
@overload
def resample_nu(
x: _FloatArray,
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]] | np.ndarray[_Shape, np.dtype[_Floating]],
sr_orig: float,
t_out: _FloatArray,
t_out: np.ndarray[_Shape, np.dtype[np.float32]] | np.ndarray[_Shape, np.dtype[_Floating]],
axis: int = -1,
filter: _FilterType = "kaiser_best",
parallel: bool = False,
*,
num_zeros: int = 64,
precision: int = 9,
rolloff: float = 0.945,
) -> _FloatArray: ...
) -> np.ndarray[_Shape, np.dtype[np.float32]] | np.ndarray[_Shape, np.dtype[_Floating]]: ...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same here

Suggested change
@overload
def resample_nu(
x: _FloatArray,
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]] | np.ndarray[_Shape, np.dtype[_Floating]],
sr_orig: float,
t_out: _FloatArray,
t_out: np.ndarray[_Shape, np.dtype[np.float32]] | np.ndarray[_Shape, np.dtype[_Floating]],
axis: int = -1,
filter: _FilterType = "kaiser_best",
parallel: bool = False,
*,
num_zeros: int = 64,
precision: int = 9,
rolloff: float = 0.945,
) -> _FloatArray: ...
) -> np.ndarray[_Shape, np.dtype[np.float32]] | np.ndarray[_Shape, np.dtype[_Floating]]: ...

@overload
def resample(
x: _FloatArray,
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

To avoid writing an explanatory comment for Any each time, you can avoid using Any since it is set to default

Suggested change
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]],
x: np.ndarray[_Shape, np.dtype[np.integer]],


@overload
def resample_nu(
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

same here

Suggested change
x: np.ndarray[_Shape, np.dtype[np.integer[Any]]],
x: np.ndarray[_Shape, np.dtype[np.integer]],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants