[
https://issues.apache.org/jira/browse/BEAM-7746?focusedWorklogId=339087&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-339087
]
ASF GitHub Bot logged work on BEAM-7746:
----------------------------------------
Author: ASF GitHub Bot
Created on: 06/Nov/19 00:24
Start Date: 06/Nov/19 00:24
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_r342865083
##########
File path: sdks/python/apache_beam/coders/coder_impl.py
##########
@@ -838,11 +911,12 @@ def encode_to_stream(self, value, out, nested):
out.write_var_int64(0)
def decode_from_stream(self, in_stream, nested):
+ # type: (create_InputStream, bool) -> Sequence
size = in_stream.read_bigendian_int32()
if size >= 0:
elements = [self._elem_coder.decode_from_stream(in_stream, True)
- for _ in range(size)]
+ for _ in range(size)] # type: Iterable[Any]
Review comment:
This is a bit less obvious than that. mypy knows that this is a
`List[Any]`, but below this `element` is rebound to `_ConcatSequence`. Without
this annotation, we get this error:
```
apache_beam/coders/coder_impl.py:935: error: Incompatible types in
assignment (expression has type "_ConcatSequence", variable has type
"List[Any]") [assignment]
```
Here's the full context:
```python
if size >= 0:
elements = [self._elem_coder.decode_from_stream(in_stream, True)
for _ in range(size)] # type: Iterable[Any]
else:
elements = []
count = in_stream.read_var_int64()
while count > 0:
for _ in range(count):
elements.append(self._elem_coder.decode_from_stream(in_stream,
True))
count = in_stream.read_var_int64()
if count == -1:
if self._read_state is None:
raise ValueError(
'Cannot read state-written iterable without state reader.')
state_token = in_stream.read_all(True)
elements = _ConcatSequence(
elements, self._read_state(state_token, self._elem_coder))
```
The first time that a variable appears in the code we can take that
opportunity to relax the type definition, in this case from `List` to
`Iterable`. `_ConcatSequence` is also an `Iterable`, so it meets the type
requirement is allowed to be assigned to `elements`.
----------------------------------------------------------------
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:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 339087)
Time Spent: 18.5h (was: 18h 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: 18.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)