You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "liferoad (via GitHub)" <gi...@apache.org> on 2023/04/15 00:36:48 UTC

[GitHub] [beam] liferoad opened a new pull request, #26290: Inherit Generic for TimestampedValue

liferoad opened a new pull request, #26290:
URL: https://github.com/apache/beam/pull/26290

   Fix #22547 
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


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


[GitHub] [beam] TheNeuralBit commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "TheNeuralBit (via GitHub)" <gi...@apache.org>.
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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1218596628


##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,99 @@
+from apache_beam.typehints import Dict, Any, List
+from apache_beam.typehints.decorators import TypeCheckError
+from apache_beam.transforms.window import TimestampedValue
+
+import unittest
+import apache_beam as beam
+
+
+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]]:

Review Comment:
   Fixed.



##########
sdks/python/apache_beam/transforms/window.py:
##########
@@ -55,6 +55,8 @@
 from typing import Iterable
 from typing import List
 from typing import Optional
+from typing import Generic

Review Comment:
   Done.



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


[GitHub] [beam] liferoad commented on pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on PR #26290:
URL: https://github.com/apache/beam/pull/26290#issuecomment-1577482098

   R: @TheNeuralBit 


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


[GitHub] [beam] jrmccluskey commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1210443461


##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,99 @@
+from apache_beam.typehints import Dict, Any, List

Review Comment:
   This will fix the RAT PreCommit



##########
sdks/python/apache_beam/transforms/window.py:
##########
@@ -55,6 +55,8 @@
 from typing import Iterable
 from typing import List
 from typing import Optional
+from typing import Generic

Review Comment:
   the Generic type import should be after the `Any`, I'd make a suggestion for it but the UI doesn't want me doing multiline suggestions outside of lines you've touched in the PR



##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,99 @@
+from apache_beam.typehints import Dict, Any, List
+from apache_beam.typehints.decorators import TypeCheckError
+from apache_beam.transforms.window import TimestampedValue
+
+import unittest
+import apache_beam as beam

Review Comment:
   This fixes the RAT precommit and linting errors



##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,99 @@
+from apache_beam.typehints import Dict, Any, List
+from apache_beam.typehints.decorators import TypeCheckError
+from apache_beam.transforms.window import TimestampedValue
+
+import unittest
+import apache_beam as beam
+
+
+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]]:

Review Comment:
   This test case is failing:
   
   ```
    ____ ERROR collecting apache_beam/transforms/timestamped_value_type_test.py ____
   17:08:32 apache_beam/transforms/timestamped_value_type_test.py:18: in <module>
   17:08:32     plant: Dict[str, Any]) -> TimestampedValue[List[str]]:
   17:08:32 /usr/lib/python3.8/typing.py:261: in inner
   17:08:32     return func(*args, **kwds)
   17:08:32 /usr/lib/python3.8/typing.py:886: in __class_getitem__
   17:08:32     params = tuple(_type_check(p, msg) for p in params)
   17:08:32 /usr/lib/python3.8/typing.py:886: in <genexpr>
   17:08:32     params = tuple(_type_check(p, msg) for p in params)
   17:08:32 /usr/lib/python3.8/typing.py:149: in _type_check
   17:08:32     raise TypeError(f"{msg} Got {arg!r:.100}.")
   17:08:32 E   TypeError: Parameters to generic types must be types. Got List[<class 'str'>].
   ```



##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,99 @@
+from apache_beam.typehints import Dict, Any, List
+from apache_beam.typehints.decorators import TypeCheckError
+from apache_beam.transforms.window import TimestampedValue
+
+import unittest
+import apache_beam as beam

Review Comment:
   ```suggestion
   #
   # 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
   
   import apache_beam as beam
   from apache_beam.transforms.window import TimestampedValue
   from apache_beam.typehints import Any
   from apache_beam.typehints import Dict
   from apache_beam.typehints import List
   from apache_beam.typehints.decorators import TypeCheckError
   ```



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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1227278078


##########
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:
   We only transfer the typing module to the beam type now per: https://github.com/apache/beam/blob/master/sdks/python/apache_beam/typehints/native_type_compatibility.py#L261
   I am not sure we want to treat TimestampedValue differently. The current design is to do the runtime checks with the given type for TimestampedValue.



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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1227278460


##########
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:
   `runtime_type_check=True` in self.opts does that check.



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


[GitHub] [beam] codecov[bot] commented on pull request #26290: Inherit Generic for TimestampedValue

Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #26290:
URL: https://github.com/apache/beam/pull/26290#issuecomment-1577479595

   ## [Codecov](https://app.codecov.io/gh/apache/beam/pull/26290?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   > Merging [#26290](https://app.codecov.io/gh/apache/beam/pull/26290?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (c35e09a) into [master](https://app.codecov.io/gh/apache/beam/commit/3dc07743b4e4cecd959760fd0d7484affeed34e3?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (3dc0774) will **decrease** coverage by `0.04%`.
   > The diff coverage is `100.00%`.
   
   ```diff
   @@            Coverage Diff             @@
   ##           master   #26290      +/-   ##
   ==========================================
   - Coverage   71.51%   71.48%   -0.04%     
   ==========================================
     Files         768      853      +85     
     Lines      103725   103994     +269     
   ==========================================
   + Hits        74184    74344     +160     
   - Misses      27992    28101     +109     
     Partials     1549     1549              
   ```
   
   | Flag | Coverage Δ | |
   |---|---|---|
   | python | `81.03% <100.00%> (-0.09%)` | :arrow_down: |
   
   Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#carryforward-flags-in-the-pull-request-comment) to find out more.
   
   | [Impacted Files](https://app.codecov.io/gh/apache/beam/pull/26290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [sdks/python/apache\_beam/transforms/core.py](https://app.codecov.io/gh/apache/beam/pull/26290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0vdHJhbnNmb3Jtcy9jb3JlLnB5) | `91.76% <100.00%> (ø)` | |
   | [sdks/python/apache\_beam/transforms/window.py](https://app.codecov.io/gh/apache/beam/pull/26290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0vdHJhbnNmb3Jtcy93aW5kb3cucHk=) | `87.50% <100.00%> (+0.14%)` | :arrow_up: |
   | [sdks/python/apache\_beam/typehints/typecheck.py](https://app.codecov.io/gh/apache/beam/pull/26290?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-c2Rrcy9weXRob24vYXBhY2hlX2JlYW0vdHlwZWhpbnRzL3R5cGVjaGVjay5weQ==) | `97.61% <100.00%> (+0.07%)` | :arrow_up: |
   
   ... and [141 files with indirect coverage changes](https://app.codecov.io/gh/apache/beam/pull/26290/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   
   :mega: We’re building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
   


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


[GitHub] [beam] github-actions[bot] commented on pull request #26290: Inherit Generic for TimestampedValue

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #26290:
URL: https://github.com/apache/beam/pull/26290#issuecomment-1577483309

   Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control


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


[GitHub] [beam] jrmccluskey merged pull request #26290: Inherit Generic for TimestampedValue

Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey merged PR #26290:
URL: https://github.com/apache/beam/pull/26290


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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1253618013


##########
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:
   Done.



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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1227278696


##########
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:
   Done.



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


[GitHub] [beam] jrmccluskey commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "jrmccluskey (via GitHub)" <gi...@apache.org>.
jrmccluskey commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1253560948


##########
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:
   This is true but not true, as post-3.9 changes we convert builtins to tying types then leverage the existing code to covert to beam types



##########
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:
   That seems to be the implication, +1 for that extra case



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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1218596014


##########
sdks/python/apache_beam/transforms/timestamped_value_type_test.py:
##########
@@ -0,0 +1,99 @@
+from apache_beam.typehints import Dict, Any, List
+from apache_beam.typehints.decorators import TypeCheckError
+from apache_beam.transforms.window import TimestampedValue
+
+import unittest
+import apache_beam as beam
+
+
+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]]:

Review Comment:
   Fixed. Thanks.



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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1227279772


##########
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:
   I personally prefer to explicitly write down these tests so  I can easily know which test tests what cases with the function name.



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


[GitHub] [beam] liferoad commented on a diff in pull request #26290: Inherit Generic for TimestampedValue

Posted by "liferoad (via GitHub)" <gi...@apache.org>.
liferoad commented on code in PR #26290:
URL: https://github.com/apache/beam/pull/26290#discussion_r1227281209


##########
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:
   Do you mean testing `T = TypeVar('T')` by using `TimestampedValue[T]`?



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