-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Added ListParameter and TupleParameter. #1614
Conversation
Great! Is it easy for you to add your previously breaking example as a unit test? |
I've made my (first) attempt at writing a python test. I used the enum tests as a basis. If it sucks, I'll try again. |
def test_list_parameter(self): | ||
a = luigi.ListParameter() | ||
b_list = [1, 2, 3] | ||
self.assertEqual(FooList.a_list, a.serialize(b_list).parse(b_list)) |
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 think this should say
self.assertEqual(FooList.a_list, a.parse(a.serialize(b_list)))
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.
My bad. That's what I intended, but clearly not what I wrote. Fixed.
LGTM except the comments above. is it easy for you to add the test with dynamic requirements too? (not blocking) |
daedb35
to
dff00a6
Compare
def test_list_parameter(self): | ||
a = luigi.ListParameter() | ||
b_list = [1, 2, 3] | ||
self.assertEqual(FooList.a_list, a.parse(a.serialize(b_list))) |
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 think you are trying to test that when you take a python value and ser-de it it ends up at the same python value, no? So you want something like
assertEqual(b_list, a.parse(a.serialize(b_list))
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.
Also have you tried running tests locally? It helps speed up the test dev process. You can even run just specific tests from just one module, see the CONTRIBUTING doc for tox
commands
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.
Yes, that is what i'm trying to test. Writing tests is still new to me. Thanks! I'll make that correction.
I have some issues here. I'll resume work on it tomorrow. |
@@ -798,3 +798,34 @@ def parse(self, s): | |||
|
|||
def serialize(self, x): | |||
return json.dumps(x, cls=DictParameter.DictParamEncoder) | |||
|
|||
|
|||
class ListParameter(Parameter): |
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.
Can you add docs? In particular how do you specify elements on CLI/config file. I for one don't know how output of json.loads([1,2])
looks like.
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.
Sure can.
json.dumps([1,2])
would return '[1,2]'
json.loads('[1,2]')
would return [1,2]
Yea. This is the correct way to resurrect the functionality that got lost from #1133. Looks promising! |
Can anyone explain to me why Luigi always prints lists as tuples when accessing the value of the parameter directly? class ListFoo(luigi.Task):
my_list = luigi.ListParameter()
def run(self):
pass
class ParameterTest(LuigiTestCase):
def test_list(self):
a = luigi.ListParameter()
b_list = [1, 2, 3]
c = ListFoo(my_list=b_list)
self.assertEqual(c.my_list, a.parse(a.serialize(b_list))) Returns:
|
probably because json doesn't have a tuple type so it converts it into a list instead |
you need to cast it back to a tuple |
I'm confused why i should cast a ListParameter to a tuple |
I believe all i have left now is testing 1) cli usage and 2) dynamic requires. I'll recommit something by the end of my work day today. |
3119eb2
to
54181db
Compare
|
||
.. code-block:: console | ||
|
||
$ luigi --module my_tasks MyTask --book_locations <JSON string> |
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 pretty sure it's book-locations right? (hyphen not dash)
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.
Is this a luigi/python style choice that I'm not aware?
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.
@Tarrasch any explanation of this?
Sorry for asking for even more tests, but this is a pretty core functionality. The docs look great by the way! :) |
b5281b2
to
0fa178c
Compare
Cli tests keep failing locally, but error message isn't helpful. Checking if travis-ci will throw the same error. |
0fa178c
to
9f0359b
Compare
lgtm |
If @Tarrasch approves, are we good to merge? |
Dynamic requires serializes all
Parameter
. The defaultserialize(...)
method casts everything to strings. Lists and tuples need to remain lists and tuples when serialized and parsed.This is added as a solution to Issue #1607