-
-
Notifications
You must be signed in to change notification settings - Fork 25
🐛 cluster.hierarchy: use generic return type for DisjointSet.__getitem__
#992
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4b1f398
:bug: use generic return type for DisjointSet.__getitem__
lispandfound b3d04a6
:bug: `cluster.hierarchy`: `__init__` should accept `Iterable[_T]` no…
lispandfound 9a9988e
:bug: `custer.hierarchy`: `__contains__` should use type `_T`
lispandfound 830c869
:recycle: `cluster.hierarchy`: do not infer `object` when initialisin…
lispandfound 203f942
:white_check_mark: `cluster.hierarchy`: add test suite for `DisjointSet`
lispandfound c700eaa
:bug: `cluster.hierarchy`: appropriately set `_T` to resolve `__hash_…
lispandfound 582e22c
:white_check_mark: `cluster.hierarchy`: use assignment statements for…
lispandfound a2f97c3
Revert ":bug: `custer.hierarchy`: `__contains__` should use type `_T`"
lispandfound 3a937d7
:memo: `cluster.hierarchy`: Document `np.generic` upper bound for `Di…
lispandfound b8f7f51
:white_check_mark: `cluster.hierarchy`: remove `__contains__` test
lispandfound File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| from collections.abc import Iterator | ||
| from typing import Any, assert_type | ||
|
|
||
| import numpy as np | ||
| import optype.numpy as onp | ||
|
|
||
| from scipy.cluster.hierarchy import DisjointSet | ||
|
|
||
| ### | ||
| # DisjointSet | ||
|
|
||
| py_str_1d: list[str] | ||
| py_int_1d: list[int] | ||
|
|
||
| i32_1d: onp.Array1D[np.int32] | ||
| i64_1d: onp.Array1D[np.int64] | ||
| # DisjointSet(Iterable[T]) produces a DisjointSet[T] with universal set of type T. | ||
| assert_type(DisjointSet(py_str_1d), DisjointSet[str]) | ||
| assert_type(DisjointSet(py_int_1d), DisjointSet[int]) | ||
| # NOTE: Directly using assert_type fails with numpy arrays for all numpy<=2.0. Instead, use assignment statements. | ||
| _10: DisjointSet[np.int32] = DisjointSet(i32_1d) | ||
| _11: DisjointSet[np.int64] = DisjointSet(i64_1d) | ||
| # DisjointSet() produces a DisjointSet[Any] because T is unbound. | ||
| assert_type(DisjointSet(), DisjointSet[Any]) | ||
|
|
||
| disjoint_set_str: DisjointSet[str] | ||
| disjoint_set_i64: DisjointSet[np.int64] | ||
|
|
||
| # __iter__ produces an iterator over the universal set. | ||
| assert_type(iter(disjoint_set_str), Iterator[str]) | ||
| assert_type(iter(disjoint_set_i64), Iterator[np.int64]) | ||
|
|
||
| # __len__ returns the length of the universal set | ||
| assert_type(len(disjoint_set_str), int) | ||
|
|
||
| # __contains__ accepts an element of the universal set and returns a boolean | ||
| assert_type("a" in disjoint_set_str, bool) | ||
| assert_type(np.int64(2) in disjoint_set_i64, bool) | ||
|
|
||
| # __getitem__ returns an element of the universal set | ||
| assert_type(disjoint_set_str["a"], str) | ||
| disjoint_set_str[1] # type: ignore[index] # pyright: ignore[reportArgumentType] | ||
| assert_type(disjoint_set_i64[np.int64(1)], np.int64) | ||
|
|
||
| # add accepts an element of type T and adds it to the data structure (i.e. returns None) | ||
| assert_type(disjoint_set_str.add("a"), None) | ||
| disjoint_set_str.add(1) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | ||
| assert_type(disjoint_set_i64.add(np.int64(1)), None) | ||
|
|
||
| # merge accepts two elements of type T and returns a boolean indicating if they belonged to the same subset | ||
| assert_type(disjoint_set_str.merge("a", "b"), bool) | ||
| disjoint_set_str.merge(1, 2) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | ||
| assert_type(disjoint_set_i64.merge(np.int64(1), np.int64(2)), bool) | ||
|
|
||
| # connected accepts two elements of type T and returns a boolean indicating if they belonged to the same subset | ||
| assert_type(disjoint_set_str.connected("a", "b"), bool) | ||
| disjoint_set_str.connected(1, 2) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | ||
| assert_type(disjoint_set_i64.connected(np.int64(1), np.int64(2)), bool) | ||
|
|
||
| # subset accepts one element of type T and returns its containing subset. | ||
| assert_type(disjoint_set_str.subset("a"), set[str]) | ||
| disjoint_set_str.subset(1) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | ||
| assert_type(disjoint_set_i64.subset(np.int64(1)), set[np.int64]) | ||
|
|
||
| # subset_size accepts one element of type T and returns the *size* of its subset. | ||
| assert_type(disjoint_set_str.subset_size("a"), int) | ||
| disjoint_set_str.subset_size(1) # type: ignore[arg-type] # pyright: ignore[reportArgumentType] | ||
| assert_type(disjoint_set_i64.subset_size(np.int64(1)), int) | ||
|
|
||
| # subsets returns a list of all subsets of type T | ||
| assert_type(disjoint_set_str.subsets(), list[set[str]]) | ||
| assert_type(disjoint_set_i64.subsets(), list[set[np.int64]]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.