From 846ada629ba24452efb80a630938a19277e34e03 Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Wed, 14 Aug 2019 19:44:40 -0700 Subject: [PATCH 1/3] The coder returned for typehints.List should be IterableCoder --- sdks/python/apache_beam/coders/typecoders.py | 3 ++- sdks/python/apache_beam/coders/typecoders_test.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/sdks/python/apache_beam/coders/typecoders.py b/sdks/python/apache_beam/coders/typecoders.py index 7afb01585d44..56a5ea8c5952 100644 --- a/sdks/python/apache_beam/coders/typecoders.py +++ b/sdks/python/apache_beam/coders/typecoders.py @@ -119,7 +119,8 @@ def get_coder(self, typehint): raise RuntimeError( 'Coder registry has no fallback coder. This can happen if the ' 'fast_coders module could not be imported.') - if isinstance(typehint, typehints.IterableTypeConstraint): + if isinstance(typehint, (typehints.IterableTypeConstraint, + typehints.ListConstraint)): return coders.IterableCoder.from_type_hint(typehint, self) elif typehint is None: # In some old code, None is used for Any. diff --git a/sdks/python/apache_beam/coders/typecoders_test.py b/sdks/python/apache_beam/coders/typecoders_test.py index 64843aede4ca..a0b8dd3c7ebe 100644 --- a/sdks/python/apache_beam/coders/typecoders_test.py +++ b/sdks/python/apache_beam/coders/typecoders_test.py @@ -129,6 +129,13 @@ def test_iterable_coder(self): self.assertEqual(expected_coder, real_coder) self.assertEqual(real_coder.encode(values), expected_coder.encode(values)) + def test_list_coder(self): + real_coder = typecoders.registry.get_coder(typehints.List[bytes]) + expected_coder = coders.IterableCoder(coders.BytesCoder()) + values = [b'abc', b'xyz'] + self.assertEqual(expected_coder, real_coder) + self.assertEqual(real_coder.encode(values), expected_coder.encode(values)) + if __name__ == '__main__': unittest.main() From f88989bff6b24adfcf5d31cf17d064e81ae34926 Mon Sep 17 00:00:00 2001 From: Chad Dombrova Date: Wed, 28 Aug 2019 17:22:10 -0700 Subject: [PATCH 2/3] Clarify the story surrounding the list return value guarantee --- sdks/python/apache_beam/coders/typecoders_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sdks/python/apache_beam/coders/typecoders_test.py b/sdks/python/apache_beam/coders/typecoders_test.py index a0b8dd3c7ebe..e09a16f4bb90 100644 --- a/sdks/python/apache_beam/coders/typecoders_test.py +++ b/sdks/python/apache_beam/coders/typecoders_test.py @@ -135,6 +135,13 @@ def test_list_coder(self): values = [b'abc', b'xyz'] self.assertEqual(expected_coder, real_coder) self.assertEqual(real_coder.encode(values), expected_coder.encode(values)) + # IterableCoder.decode() always returns a list. Its implementation, + # IterableCoderImpl, *can* return a non-list if it is provided a read_state + # object, but this is not possible using the atomic IterableCoder interface. + self.assertIs(list, + type(expected_coder.decode(expected_coder.encode(values)))) + + if __name__ == '__main__': From eb5c7943d7979fd19fd015813e0c50b087b4041f Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 5 Sep 2019 16:49:45 -0700 Subject: [PATCH 3/3] lint --- sdks/python/apache_beam/coders/typecoders_test.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/sdks/python/apache_beam/coders/typecoders_test.py b/sdks/python/apache_beam/coders/typecoders_test.py index e09a16f4bb90..02a8d68d35b4 100644 --- a/sdks/python/apache_beam/coders/typecoders_test.py +++ b/sdks/python/apache_beam/coders/typecoders_test.py @@ -142,7 +142,5 @@ def test_list_coder(self): type(expected_coder.decode(expected_coder.encode(values)))) - - if __name__ == '__main__': unittest.main()