-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Make and test some small improvements to lazy
#1789
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
3 commits
Select commit
Hold shift + click to select a range
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
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,82 @@ | ||
| # Copyright 2019 The TensorFlow Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # ============================================================================== | ||
| """Unit tests for the `tensorboard.lazy` module.""" | ||
|
|
||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
|
|
||
| import six | ||
| import unittest | ||
|
|
||
| from tensorboard import lazy | ||
|
|
||
|
|
||
| class LazyTest(unittest.TestCase): | ||
|
|
||
| def test_self_composition(self): | ||
| """A lazy module should be able to load another lazy module.""" | ||
| # This test can fail if the `LazyModule` implementation stores the | ||
| # cached module as a field on the module itself rather than a | ||
| # closure value. (See pull request review comments on #1781 for | ||
| # details.) | ||
|
|
||
| @lazy.lazy_load("inner") | ||
| def inner(): | ||
| import collections # pylint: disable=g-import-not-at-top | ||
| return collections | ||
|
|
||
| @lazy.lazy_load("outer") | ||
| def outer(): | ||
| return inner | ||
|
|
||
| x1 = outer.namedtuple | ||
| x2 = inner.namedtuple | ||
| self.assertEqual(x1, x2) | ||
|
|
||
| def test_lazy_cycle(self): | ||
| """A cycle among lazy modules should error, not deadlock or spin.""" | ||
| # This test can fail if `_memoize` uses a non-reentrant lock. (See | ||
| # pull request review comments on #1781 for details.) | ||
|
|
||
| @lazy.lazy_load("inner") | ||
| def inner(): | ||
| return outer.foo | ||
|
|
||
| @lazy.lazy_load("outer") | ||
| def outer(): | ||
| return inner | ||
|
|
||
| expected_message = "Circular import when resolving LazyModule 'inner'" | ||
| with six.assertRaisesRegex(self, ImportError, expected_message): | ||
| outer.bar | ||
|
|
||
| def test_repr_before_load(self): | ||
| @lazy.lazy_load("foo") | ||
| def foo(): | ||
| self.fail("Should not need to resolve this module.") | ||
| self.assertEquals(repr(foo), "<module 'foo' via LazyModule (not yet loaded)>") | ||
|
|
||
| def test_repr_after_load(self): | ||
| import collections # pylint: disable=g-import-not-at-top | ||
| @lazy.lazy_load("foo") | ||
| def foo(): | ||
| return collections | ||
| foo.namedtuple | ||
| self.assertEquals(repr(foo), "<%r via LazyModule (loaded)>" % collections) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
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.
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.
Optional but seems like you might as well put this logic in
_memoizeitself.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.
But
_memoizetakes a function that accepts a single hashable argument,so recursion is perfectly fine. If
_memoizetook a niladic function,the argument would be more convincing, I think.
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'd be per-argument, e.g.
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 mind, I’ll keep it as written in this PR, because that
enables us to give a better error message.