-
Notifications
You must be signed in to change notification settings - Fork 85
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
Stubs for Array, ArrayOrNone, and CArray #1682
Changes from 4 commits
c09b46c
67883f3
7e51c53
d0e0310
6487f51
2b1efa7
1d5f713
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# (C) Copyright 2005-2022 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
from typing import Any, List, Optional, Tuple, Type, Union | ||
|
||
import numpy as np | ||
|
||
from .trait_type import _TraitType | ||
|
||
# Things that are allowed as individual shape elements in the 'shape' | ||
# tuple or list. | ||
_ShapeElement = Union[None, int, Tuple[int, Union[None, int]]] | ||
|
||
# Type for the shape parameter. | ||
_Shape = Union[Tuple[_ShapeElement, ...], List[_ShapeElement]] | ||
|
||
# The "Array" trait type is not as permissive as NumPy's asarray: it | ||
# accepts only NumPy arrays, lists and tuples. | ||
_ArrayLike = Union[List[Any], Tuple[Any, ...], np.ndarray[Any, Any]] | ||
|
||
# Synonym for the "stores" type of the trait. | ||
_Array = np.ndarray[Any, Any] | ||
|
||
# Things that are accepted as dtypes. This doesn't attempt to cover | ||
# all legal possibilities - only those that are common. | ||
_DTypeLike = Union[np.dtype[Any], Type[Any], str] | ||
|
||
class Array(_TraitType[_ArrayLike, _Array]): | ||
def __init__( | ||
self, | ||
dtype: Optional[_DTypeLike] = ..., | ||
shape: Optional[_Shape] = ..., | ||
value: Optional[_ArrayLike] = ..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this array-like, or do we expect an actual array here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Array-like: e.g., you can provide a list:
|
||
*, | ||
casting: str = ..., | ||
**metadata: Any, | ||
) -> None: ... | ||
|
||
class ArrayOrNone( | ||
_TraitType[Optional[_ArrayLike], Optional[_Array]] | ||
): | ||
def __init__( | ||
self, | ||
dtype: Optional[_DTypeLike] = ..., | ||
shape: Optional[_Shape] = ..., | ||
value: Optional[_ArrayLike] = ..., | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question: is this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same answer. :-) |
||
*, | ||
casting: str = ..., | ||
**metadata: Any, | ||
) -> None: ... | ||
|
||
class CArray(_TraitType[_ArrayLike, _Array]): | ||
def __init__( | ||
self, | ||
dtype: Optional[_DTypeLike] = ..., | ||
shape: Optional[_Shape] = ..., | ||
value: Optional[_ArrayLike] = ..., | ||
*, | ||
casting: str = ..., | ||
**metadata: Any, | ||
) -> None: ... |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# (C) Copyright 2005-2022 Enthought, Inc., Austin, TX | ||
# All rights reserved. | ||
# | ||
# This software is provided without warranty under the terms of the BSD | ||
# license included in LICENSE.txt and may be redistributed only under | ||
# the conditions described in the aforementioned license. The license | ||
# is also available online at http://www.enthought.com/licenses/BSD.txt | ||
# | ||
# Thanks for using Enthought open source! | ||
|
||
import numpy as np | ||
|
||
from traits.api import Array, ArrayOrNone, HasTraits | ||
|
||
|
||
class HasArrayTraits(HasTraits): | ||
spectrum = Array(shape=(None,), dtype=np.float64) | ||
maybe_image = ArrayOrNone(shape=(None, None, 3), dtype=np.float64) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want a test for Similarly, it might be good to test the dimension ranges (eg. `maybe_rgba = Array(shape=(None, None, (3,4)), ...) Also, is it worth testing for invalid trait parameters (eg. passing a default value of Some of this may be overkill if we expect that things will Just Work. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should probably test I can flesh out the tests a bit more to test invalid parameters. As an aside, I think at some point we're going to need a better framework for these tests; right now, there's a lot of interaction between tests that should be independent of one another. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added some tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That particular example is troublesome: as far as I can tell, in general there's no way to type hint a function parameter that has a default value of I've added a few more tests, including tests for bad declarations. |
||
|
||
|
||
obj = HasArrayTraits() | ||
obj.spectrum = np.array([2, 3, 4], dtype=np.float64) | ||
obj.spectrum = "not an array" # E: assignment | ||
obj.spectrum = None # E: assignment | ||
|
||
obj.maybe_image = None | ||
obj.maybe_image = np.zeros((5, 5, 3)) | ||
obj.maybe_image = 2.3 # E: assignment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need
AbstractArray
as well?A quick search and I can't see any uses of it, but it is nominally public.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AbstractArray
isn't exposed intraits.api
, so I'm happy leaving it out of the stubs for now.