-
Notifications
You must be signed in to change notification settings - Fork 23
Value objects use dataclasses #225
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
Changes from all commits
b181104
aaeaf6c
c1cad93
bd5edff
81dc0bf
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 |
|---|---|---|
|
|
@@ -4,37 +4,9 @@ | |
|
|
||
|
|
||
| class TestModule: | ||
| def test_repr(self): | ||
| def test_str(self): | ||
| module = Module("foo.bar") | ||
| assert repr(module) == "<Module: foo.bar>" | ||
|
|
||
| def test_equals(self): | ||
| a = Module("foo.bar") | ||
| b = Module("foo.bar") | ||
| c = Module("foo.bar.baz") | ||
|
|
||
| assert a == b | ||
| assert a != c | ||
| # Also non-Module instances should not be treated as equal. | ||
| assert a != "foo" | ||
|
|
||
| def test_equals_obeys_liskov(self): | ||
| class SpecialModule(Module): | ||
| pass | ||
|
|
||
| module = Module("foo.bar") | ||
| special_module = SpecialModule("foo.bar") | ||
|
|
||
| assert module == special_module | ||
| assert special_module == module | ||
|
|
||
| def test_hash(self): | ||
| a = Module("foo.bar") | ||
| b = Module("foo.bar") | ||
| c = Module("foo.bar.baz") | ||
|
|
||
| assert hash(a) == hash(b) | ||
| assert hash(a) != hash(c) | ||
| assert str(module) == "foo.bar" | ||
|
|
||
| @pytest.mark.parametrize( | ||
| "module, expected", | ||
|
|
@@ -53,101 +25,11 @@ def test_parent(self, module, expected): | |
|
|
||
|
|
||
| class TestDirectImport: | ||
| def test_repr(self): | ||
|
Collaborator
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 think we've lost coverage for the str method, instead of removing this mind tweaking it so we test that instead?
Collaborator
Author
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. True - I've changed this to test the |
||
| def test_str(self): | ||
| import_path = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| assert repr(import_path) == "<DirectImport: foo -> bar (l. 10)>" | ||
|
|
||
| def test_equals(self): | ||
| a = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| b = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| c = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("baz"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| d = DirectImport( | ||
| importer=Module("foobar"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| e = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=11, | ||
| line_contents="import bar", | ||
| ) | ||
| f = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="from . import bar", | ||
| ) | ||
|
|
||
| assert a == b | ||
| assert a != c | ||
| assert a != d | ||
| assert a != e | ||
| assert a != f | ||
| # Also non-DirectImport instances should not be treated as equal. | ||
| assert a != "foo" | ||
|
|
||
| def test_hash(self): | ||
| a = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| b = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| c = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("baz"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| d = DirectImport( | ||
| importer=Module("foobar"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="import bar", | ||
| ) | ||
| e = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=11, | ||
| line_contents="import bar", | ||
| ) | ||
| f = DirectImport( | ||
| importer=Module("foo"), | ||
| imported=Module("bar"), | ||
| line_number=10, | ||
| line_contents="from . import bar", | ||
| ) | ||
|
|
||
| assert hash(a) == hash(b) | ||
| assert hash(a) != hash(c) | ||
| assert hash(a) != hash(d) | ||
| assert hash(a) != hash(e) | ||
| assert hash(a) != hash(f) | ||
| assert str(import_path) == "foo -> bar (l. 10)" | ||
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.
We should do
slots=Trueon the dataclass decorator, 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.
I believe
slotswas only added in 3.10 to the dataclass decorator, and we support 3.9.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.
Oh fair enough. Ok to leave this line in then, with a comment to switch once we drop 3.9? (I'm assuming it will work with dataclasses.)
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'm seeing this error when leaving the
__slots__ = ("name",)line inSo it seems like it doesn't work...
I suppose since the benchmarks are still passing then the performance change to use
slots=Truecan't be so big.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's strange, oh well - soon joblib will be gone I hope.
I seem to remember slots making a noticeable difference to some early profiling I did, but as you say, this is what benchmarks are for! So let's drop it for now.