Typing NDCube - #950
Conversation
* Potential bug referencing self.table_coords v self._table_coords
* Fix a potential but where with nan operation on data with no nans
* Remove assert in import code paths
* Possibl bug and missing methods on ABC
e1808f4 to
4d1a861
Compare
|
Not sure how to resolve the doc build issue I think its because there is more then one |
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def resample(self, factor: float | Iterable[float], offset: float | Iterable[float] = 0, |
There was a problem hiding this comment.
I don't think we've decided that resample should live in the ABC. My initial reaction is that I'm not against it. But we shouldn't add this without making an active decision.
There was a problem hiding this comment.
Ok yea can pull it from this PR and update the type hints if needed
| helpers.assert_collections_equal(del_collection, expected) | ||
|
|
||
|
|
||
| def test_slice_by_keys_unaligned_collection(): |
There was a problem hiding this comment.
Can you explain what this test is doing? Is doesn't appear to be testing slicing as its comments suggest.
There was a problem hiding this comment.
Yea so unaligned_collection looks like this and then it sliced with unaligned_collection[("cube0", "cube1")]
<ndcube.ndcollection.NDCollection object at 0x10ffbc4d0>
NDCollection
------------
Cube keys: ('cube0', 'cube1', 'cube2')
Number of Cubes: 3
Aligned dimensions: None
Aligned physical types: None
| """ | ||
|
|
||
| def __init__(self, key_data_pairs, aligned_axes=None, meta=None, **kwargs): | ||
| def __init__(self, key_data_pairs: Any, aligned_axes: Any = None, meta: Any = None, **kwargs: Any) -> None: |
There was a problem hiding this comment.
Should key_data_pairs not be Iterable[Any, Any]? Or whatever the correct equivalent is?
There was a problem hiding this comment.
Yep so this should accept a dict of type dict[str, NDCube|NDSequence] or tuple on the tuple is is a tuple of tuple e.g tuple[tuple[str, NDCube|NDSequence] or a long list of key, data pair e.g tuple[any, ...]. I see if I can worked it out from the code.
|
|
||
| # Convert aligned axes to required format. | ||
| sanitize_inputs = kwargs.pop("sanitize_inputs", True) | ||
| keys: tuple[Any, ...] = () |
There was a problem hiding this comment.
Why is this needed? keys is defined a couple line below. And key_data_pairs must contain at least one valid key so keys cannot be empty. Seems to me like it isn't needed. But am I missing something?
There was a problem hiding this comment.
So this is because pyright raises and possibly unbound warning because aligned_axes is None the variable isn't defined - could add local comment disable that warning
|
|
||
| @property | ||
| def aligned_axes(self): | ||
| def aligned_axes(self) -> dict[Any, tuple[Any, ...]] | None: |
There was a problem hiding this comment.
This should return None or a dict where the value assigned to each key is int or tuple[int]. Can this be captured in typing here?
There was a problem hiding this comment.
Yea I think dict[Any, int] | dict[Any, tuple[int, ...]] | None`
Also where typing could potentially help design - as consumer of this I need to check if the value is an int or tuple then maybe the length of the tuple but if it was always returned a tuple (), (1,), (1, 2, 3) can always use len` - not suggestion this should be changed by the way just a thought.
There was a problem hiding this comment.
So if I change the type annotation to it raises an additional type errors as in the current code later on this properties value is subscripted so if were an int this would raise an error.
Lines 229 to 255 in 9befb9b
Internally its always converted to a tuple the only place I can see where it can be an int is in _sanitize_user_aligned_axes also tuple(int, ...) cover
Co-authored-by: DanRyanIrish <ryand5@tcd.ie>
PR Description
Add types and type checking to NDCube
QuantityLikeAI Assistance Disclosure
AI tools were used for: