You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "TheNeuralBit (via GitHub)" <gi...@apache.org> on 2023/06/07 16:28:44 UTC
[GitHub] [beam] TheNeuralBit commented on a diff in pull request #26290: Inherit Generic for TimestampedValue
TheNeuralBit commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1221880248
##########
sdks/python/apache_beam/typehints/typecheck.py:
##########
@@ -146,9 +148,16 @@ def _type_check_result(self, transform_results):
return transform_results
def type_check_output(o):
- # TODO(robertwb): Multi-output.
- x = o.value if isinstance(o, (TaggedOutput, WindowedValue)) else o
- self.type_check(self._output_type_hint, x, is_input=False)
+ if isinstance(o, TimestampedValue) and hasattr(o, "__orig_class__"):
Review Comment:
Please document what case the hasattr `__orig_class__` is checking. (is this for changes in typehint support between python versions? something else?)
##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,118 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+import unittest
+from typing import Any
+from typing import Dict
+from typing import List
+
+import apache_beam as beam
+from apache_beam.transforms.window import TimestampedValue
+from apache_beam.typehints.decorators import TypeCheckError
+
+
+def ConvertToTimestampedValue(plant: Dict[str, Any]) -> TimestampedValue[str]:
+ return TimestampedValue[str](plant["name"], plant["season"])
+
+
+def ConvertToTimestampedValue_1(plant: Dict[str, Any]) -> TimestampedValue:
+ return TimestampedValue(plant["name"], plant["season"])
+
+
+def ConvertToTimestampedValue_2(
+ plant: Dict[str, Any]) -> TimestampedValue[List[str]]:
+ return TimestampedValue[List[str]](plant["name"], plant["season"])
+
+
+class TypeCheckTimestampedValueTestCase(unittest.TestCase):
+ def setUp(self):
+ self.opts = beam.options.pipeline_options.PipelineOptions(
+ runtime_type_check=True)
+ self.data = [
+ {
+ "name": "Strawberry", "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_1 = [
+ {
+ "name": 1234, "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_2 = [
+ {
+ "name": ["abc", "cde"], "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_3 = [
+ {
+ "name": [123, "cde"], "season": 1585699200
+ }, # April, 2020
+ ]
+
+ def test_pcoll_hints(self):
+ for fn in (ConvertToTimestampedValue, ConvertToTimestampedValue_1):
+ pc = beam.Map(fn)
+ ht = pc.default_type_hints()
+ assert len(ht) == 3
+ # assert ht.output_types[0][0] == TimestampedValue[str]
+ assert ht.output_types[0][0]
+
+ def test_opts_with_check(self):
+ with beam.Pipeline(options=self.opts) as p:
+ _ = (
+ p
+ | "Garden plants" >> beam.Create(self.data)
+ | "With timestamps" >> beam.Map(ConvertToTimestampedValue)
+ | beam.Map(print))
+
+ def test_opts_with_check_list_str(self):
Review Comment:
nit: You might make this a parameterized test with parameters for data and map fn
##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,118 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+import unittest
+from typing import Any
+from typing import Dict
+from typing import List
+
+import apache_beam as beam
+from apache_beam.transforms.window import TimestampedValue
+from apache_beam.typehints.decorators import TypeCheckError
+
+
+def ConvertToTimestampedValue(plant: Dict[str, Any]) -> TimestampedValue[str]:
+ return TimestampedValue[str](plant["name"], plant["season"])
+
+
+def ConvertToTimestampedValue_1(plant: Dict[str, Any]) -> TimestampedValue:
+ return TimestampedValue(plant["name"], plant["season"])
+
+
+def ConvertToTimestampedValue_2(
+ plant: Dict[str, Any]) -> TimestampedValue[List[str]]:
+ return TimestampedValue[List[str]](plant["name"], plant["season"])
+
+
+class TypeCheckTimestampedValueTestCase(unittest.TestCase):
+ def setUp(self):
+ self.opts = beam.options.pipeline_options.PipelineOptions(
+ runtime_type_check=True)
+ self.data = [
+ {
+ "name": "Strawberry", "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_1 = [
+ {
+ "name": 1234, "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_2 = [
+ {
+ "name": ["abc", "cde"], "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_3 = [
+ {
+ "name": [123, "cde"], "season": 1585699200
+ }, # April, 2020
+ ]
+
+ def test_pcoll_hints(self):
+ for fn in (ConvertToTimestampedValue, ConvertToTimestampedValue_1):
+ pc = beam.Map(fn)
+ ht = pc.default_type_hints()
+ assert len(ht) == 3
+ # assert ht.output_types[0][0] == TimestampedValue[str]
+ assert ht.output_types[0][0]
Review Comment:
It looks like this may not be done?
##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,118 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+import unittest
+from typing import Any
+from typing import Dict
+from typing import List
+
+import apache_beam as beam
+from apache_beam.transforms.window import TimestampedValue
+from apache_beam.typehints.decorators import TypeCheckError
+
+
+def ConvertToTimestampedValue(plant: Dict[str, Any]) -> TimestampedValue[str]:
+ return TimestampedValue[str](plant["name"], plant["season"])
Review Comment:
It may be good to test the case where the function takes a generic T and returns TimestampedValue[T]
##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,118 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.
+#
+
+import unittest
+from typing import Any
+from typing import Dict
+from typing import List
+
+import apache_beam as beam
+from apache_beam.transforms.window import TimestampedValue
+from apache_beam.typehints.decorators import TypeCheckError
+
+
+def ConvertToTimestampedValue(plant: Dict[str, Any]) -> TimestampedValue[str]:
+ return TimestampedValue[str](plant["name"], plant["season"])
+
+
+def ConvertToTimestampedValue_1(plant: Dict[str, Any]) -> TimestampedValue:
+ return TimestampedValue(plant["name"], plant["season"])
+
+
+def ConvertToTimestampedValue_2(
+ plant: Dict[str, Any]) -> TimestampedValue[List[str]]:
+ return TimestampedValue[List[str]](plant["name"], plant["season"])
+
+
+class TypeCheckTimestampedValueTestCase(unittest.TestCase):
+ def setUp(self):
+ self.opts = beam.options.pipeline_options.PipelineOptions(
+ runtime_type_check=True)
+ self.data = [
+ {
+ "name": "Strawberry", "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_1 = [
+ {
+ "name": 1234, "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_2 = [
+ {
+ "name": ["abc", "cde"], "season": 1585699200
+ }, # April, 2020
+ ]
+ self.data_3 = [
+ {
+ "name": [123, "cde"], "season": 1585699200
+ }, # April, 2020
+ ]
+
+ def test_pcoll_hints(self):
+ for fn in (ConvertToTimestampedValue, ConvertToTimestampedValue_1):
+ pc = beam.Map(fn)
+ ht = pc.default_type_hints()
+ assert len(ht) == 3
+ # assert ht.output_types[0][0] == TimestampedValue[str]
+ assert ht.output_types[0][0]
+
+ def test_opts_with_check(self):
+ with beam.Pipeline(options=self.opts) as p:
+ _ = (
+ p
+ | "Garden plants" >> beam.Create(self.data)
+ | "With timestamps" >> beam.Map(ConvertToTimestampedValue)
+ | beam.Map(print))
Review Comment:
Should these tests make an assertion about the pcollection's element_type?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@beam.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org