Skip to content
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

Merged
merged 4 commits into from
Apr 4, 2016

Conversation

dlstadther
Copy link
Collaborator

Dynamic requires serializes all Parameter. The default serialize(...) 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

@erikbern
Copy link
Contributor

Great!

Is it easy for you to add your previously breaking example as a unit test?

@dlstadther
Copy link
Collaborator Author

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))
Copy link
Contributor

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)))

Copy link
Collaborator Author

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.

@erikbern
Copy link
Contributor

LGTM except the comments above.

is it easy for you to add the test with dynamic requirements too? (not blocking)

def test_list_parameter(self):
a = luigi.ListParameter()
b_list = [1, 2, 3]
self.assertEqual(FooList.a_list, a.parse(a.serialize(b_list)))
Copy link
Contributor

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))

Copy link
Contributor

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

Copy link
Collaborator Author

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.

@dlstadther
Copy link
Collaborator Author

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):
Copy link
Contributor

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.

Copy link
Collaborator Author

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]

@Tarrasch
Copy link
Contributor

Yea. This is the correct way to resurrect the functionality that got lost from #1133. Looks promising!

@dlstadther
Copy link
Collaborator Author

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:

AssertionError: (1, 2, 3) != [1, 2, 3]

@erikbern
Copy link
Contributor

probably because json doesn't have a tuple type so it converts it into a list instead

@erikbern
Copy link
Contributor

you need to cast it back to a tuple

@dlstadther
Copy link
Collaborator Author

I'm confused why i should cast a ListParameter to a tuple

@dlstadther
Copy link
Collaborator Author

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.

@dlstadther dlstadther force-pushed the add_parameters branch 3 times, most recently from 3119eb2 to 54181db Compare March 30, 2016 19:55

.. code-block:: console

$ luigi --module my_tasks MyTask --book_locations <JSON string>
Copy link
Contributor

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)

Copy link
Collaborator Author

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?

Copy link
Collaborator Author

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?

@Tarrasch
Copy link
Contributor

Sorry for asking for even more tests, but this is a pretty core functionality. The docs look great by the way! :)

@dlstadther
Copy link
Collaborator Author

Cli tests keep failing locally, but error message isn't helpful. Checking if travis-ci will throw the same error.

@erikbern
Copy link
Contributor

erikbern commented Apr 1, 2016

lgtm

@dlstadther
Copy link
Collaborator Author

If @Tarrasch approves, are we good to merge?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants