-
Notifications
You must be signed in to change notification settings - Fork 3
don't coerce to ndarray when initializing/expanding arrays #13
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
Conversation
try: | ||
shp = value.shape # type: ignore | ||
except AttributeError: | ||
return np.full(shape, value) |
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.
exceptions can be expensive. Perhaps check with hasattr
instead?
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.
Good point, I found this
https://stackoverflow.com/a/903238
Sounds like if value
has a shape
then try/except
is faster but if not, it's slower. I'm not sure what proportion of arrays vs scalars we can expect. The difference isn't huge in either case but maybe worth our own benchmark at some point.
return np.full(shape, value.item()) # type: ignore | ||
if shp != shape: | ||
raise ValueError(f"Shape mismatch, got {shp}, expected {shape}") | ||
# any way to avoid the cast? |
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.
Casting gets optimized away by python and doesn't do anything at all. It's a pass-through function.
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.
yeah, I just wonder if there's any way to write this so mypy is happy without the cast.
might fix #9 ?