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
Fix late-import numpy in stream.core #81790
Conversation
`numpy` is not listed in main requirements and is imported early by `conftest`. Split out from home-assistant#81678 Co-authored-by: uvjustin <46082645+uvjustin@users.noreply.github.com>
Hey there @hunterjm, @uvjustin, @allenporter, mind taking a look at this pull request as it has been labeled with an integration ( Code owner commandsCode owners of
|
if orientation == 8: # Rotate right | ||
return np.rot90(image, -1).copy() | ||
return image # Transforms 0 and 1 (and others not yet defined) are identity |
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.
if orientation == 8: # Rotate right | |
return np.rot90(image, -1).copy() | |
return image # Transforms 0 and 1 (and others not yet defined) are identity | |
return np.rot90(image, -1).copy() # Rotate right |
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.
See above
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.
I'd rather not have an out-of-range value mean "rotate right" 😁
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.
If you don't apply, then you end up with a gap in code coverage, so you'd need a different way to solve that.
(unless _transform_image
is not covered?)
Edit: I agree that oritentation 999 shouldn't be "rotate right".
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.
It won't get out of range. It is initialized to 1 and only gets changed via websocket which restricts it to 1 through 8.
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.
That is the only way it's currently called. If code was added that calls it a different way then reviewers should be aware of the behavior - perhaps add a comment?
Even if an unexpected value is encountered, I don't think returning a different rotation on an unexpected value is really any better/worse than returning the original (if we are being anal then we should be raising regardless).
I don't see a good reason to complicate the code here.
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.
That is the only way it's currently called.
My point is that at some point someone might refactor it elsewhere for some other use and start using it without knowing you'd need to validate the orientation
argument first, and at that point it becomes "oh that's the weird function that turns your picture sideways if you call it wrong".
Anyway, pushed a change that adds internal validation (in the early-return branch) and gets rid of the last if...
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.
Why is it weird to turn it sideways if it's called wrong? I would argue that's probably even better than passing it unchanged as at least you know something is wrong.
This is an internal function that's used only in stream. If someone tries to use it elsewhere they should read it and figure out how it works.
Perhaps the orientation should be typed as an enum but that's beyond the scope of this PR.
I would really prefer it if you could change it as in my original suggestion.
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.
Why is it weird to turn it sideways if it's called wrong? I would argue that's probably even better than passing it unchanged as at least you know something is wrong.
I would not want to be the future someone debugging why on earth some images are being turned sideways for some reason, and to maybe eventually find out the reason is somehow something is passing 9 to this function.
The principle of least astonishment applies here, too.
If you really strongly do feel that this function should rotate images 90 degrees right when miscalled, I can begrudgingly do that, but that's also not what the old implementation did (which was to raise a KeyError
).
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.
I've told you what I think already. This is an internally used function that only gets passed ints from 1 to 8. You've added extra code to account for other values to satisfy some hypothetical future where another caller uses it wrongly.
I think if we're very concerned about that, just add a comment clarifying that it should only be called with 1 through 8 and will default to 8.
Should we add numpy to the stream requirements? |
Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
if orientation < 2 or orientation > 8: # Unused/identity orientations | ||
return image | ||
|
||
# Keep import here so that we can import stream integration without installing reqs |
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.
So just to be clear, why are we adding this additional complexity?
If it is for the case to "just be able to run tests in an incomplete environment" I would highly vote against this change. We should not build/tailor our codebase to such cases.
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.
Agree with Frenck.
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.
It sounds like a huge waste of energy, time, bandwidth, and disk space to expect all 593 packages listed in requirements_test_all.txt
to be installed (in e.g. codespaces, devcontainers, ...) just to be able to test a single component's changes (which is my use case).
This, with #81791 and adding recorder
requirements to requirements_test.txt
(which happens in #81795) enables being able to do that.
Of course, numpy
could just be made a hard dependency for stream
(given the import it essentially is) and I could add stream
's requirements to requirements_test.txt
in #81795, and that would work too...
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.
sounds like a huge waste of energy, time, bandwidth, and disk space to expect all 593 packages listed in requirements_test_all.txt to be installed (in e.g. codespaces, devcontainers, ...) just to be able to test a single component's changes (which is my use case).
Codespaces are pre-built, so that should not be a factor. Anyways, testing a single component and its dependencies depends on what you are testing. Meaning, not installing @ full becomes unpredictable, regardless of these changes. Therefore, I don't think we should add exceptions/complexity to support these cases.
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.
Codespaces are pre-built, so that should not be a factor.
Codespace prebuilds apparently need to be enabled separately for everyone working on a fork. I can't access prebuilds made for home-assistant/core
. 😞 It takes some 5 minutes for a codespace to spin up (but happily, apparently, it doesn't install everything from all
).
EDIT: Besides, the CI infrastructure will test with all
nevertheless...
Anyways, testing a single component and its dependencies depends on what you are testing. Meaning, not installing @ full becomes unpredictable, regardless of these changes.
Hm, isn't this what the strict dependencies
/requirements
manifest system tries to avoid? Even better, not installing all
when working on a single component means you can better notice a dependency that's accidentally missing.
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.
Hm, isn't this what the strict dependencies/requirements manifest system tries to avoid?
No?
The point is: This PR added tailored changes and add complexity that enabled the testing of "some" integrations in a non-fully set-up development environment. To me, this is solving a specific and limited use case, that, above all, is inconsistent (as it will not always work). Therefore, I'm against this change, and I think we should close it for that reason.
../Frenck
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.
Fair. Merging #81841 (which just notes that numpy
is a hard requirement for stream
, which it is) will close this one.
stream.core imports numpy, so it's a hard requirement for the stream component Closes home-assistant#81790 (supersedes it)
Proposed change
numpy
is not listed in main requirements and is imported early byconftest
.Import it late to avoid an ImportError.
Split out from #81678.
Type of change
Checklist
black --fast homeassistant tests
)To help with the load of incoming pull requests: