[ https://issues.apache.org/jira/browse/BEAM-7746?focusedWorklogId=348349&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-348349 ]
ASF GitHub Bot logged work on BEAM-7746: ---------------------------------------- Author: ASF GitHub Bot Created on: 22/Nov/19 20:39 Start Date: 22/Nov/19 20:39 Worklog Time Spent: 10m Work Description: chadrik commented on pull request #9915: [BEAM-7746] Add python type hints (part 1) URL: https://github.com/apache/beam/pull/9915#discussion_r349785083 ########## File path: sdks/python/apache_beam/utils/windowed_value.py ########## @@ -249,10 +268,13 @@ class _IntervalWindowBase(object): """Optimized form of IntervalWindow storing only microseconds for endpoints. """ - def __init__(self, start, end): + def __init__(self, + start, # type: Optional[Union[int, float, Timestamp]] Review comment: Yeah, this code is a bit obtuse. There is one place in the code that I can see None being passed, in `IntervalWindowCoderImpl`: ```python def decode_from_stream(self, in_, nested): # type: (create_InputStream, bool) -> IntervalWindow if not TYPE_CHECKING: global IntervalWindow if IntervalWindow is None: from apache_beam.transforms.window import IntervalWindow typed_value = IntervalWindow(None, None) typed_value._end_micros = ( 1000 * self._to_normal_time(in_.read_bigendian_uint64())) typed_value._start_micros = ( typed_value._end_micros - 1000 * in_.read_var_int64()) return typed_value ``` The problem with the `None` code path is that calling almost any method on the resulting instance will result in `AttributeError` because neither `_start_micros` nor `_end_micros` is created. You can see in the example above, those values are patched in, which works, but it's pretty hacky. It seems like this would be more straight-forward: ```python def decode_from_stream(self, in_, nested): # type: (create_InputStream, bool) -> IntervalWindow if not TYPE_CHECKING: global IntervalWindow if IntervalWindow is None: from apache_beam.transforms.window import IntervalWindow end_micros = 1000 * self._to_normal_time(in_.read_bigendian_uint64()) end = Timestamp(0, end_micros) start = Timestamp(0, end_micros - 1000 * in_.read_var_int64()) typed_value = IntervalWindow(start, end) return typed_value ``` ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 348349) Time Spent: 29.5h (was: 29h 20m) > Add type hints to python code > ----------------------------- > > Key: BEAM-7746 > URL: https://issues.apache.org/jira/browse/BEAM-7746 > Project: Beam > Issue Type: New Feature > Components: sdk-py-core > Reporter: Chad Dombrova > Assignee: Chad Dombrova > Priority: Major > Time Spent: 29.5h > Remaining Estimate: 0h > > As a developer of the beam source code, I would like the code to use pep484 > type hints so that I can clearly see what types are required, get completion > in my IDE, and enforce code correctness via a static analyzer like mypy. > This may be considered a precursor to BEAM-7060 > Work has been started here: [https://github.com/apache/beam/pull/9056] > > -- This message was sent by Atlassian Jira (v8.3.4#803005)