[ https://issues.apache.org/jira/browse/BEAM-7060?focusedWorklogId=304684&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-304684 ]
ASF GitHub Bot logged work on BEAM-7060: ---------------------------------------- Author: ASF GitHub Bot Created on: 31/Aug/19 00:21 Start Date: 31/Aug/19 00:21 Worklog Time Spent: 10m Work Description: udim commented on pull request #9283: [BEAM-7060] Type hints from Python 3 annotations URL: https://github.com/apache/beam/pull/9283#discussion_r319600523 ########## File path: sdks/python/apache_beam/typehints/decorators.py ########## @@ -171,22 +269,55 @@ def simple_output_type(self, context): if self.output_types: args, kwargs = self.output_types if len(args) != 1 or kwargs: - raise TypeError('Expected simple output type hint for %s' % context) + raise TypeError( + 'Expected single output type hint for %s but got: %s' % ( + context, self.output_types)) return args[0] + def has_simple_output_type(self): + """Whether there's a single positional output type.""" + return (self.output_types and len(self.output_types[0]) == 1 and + not self.output_types[1]) + + def strip_iterable(self): + """Removes outer Iterable (or equivalent) from output type. + + Only affects instances with simple output types, otherwise is a no-op. + + Example: Generator[Tuple(int, int)] becomes Tuple(int, int) + + Raises: + ValueError if output type is simple and not iterable. + """ + if not self.has_simple_output_type(): + return + yielded_type = typehints.get_yielded_type(self.output_types[0][0]) + self.output_types = ((yielded_type,), {}) + def copy(self): return IOTypeHints(self.input_types, self.output_types) def with_defaults(self, hints): if not hints: return self - elif not self: - return hints - return IOTypeHints(self.input_types or hints.input_types, - self.output_types or hints.output_types) + if self._has_input_types(): + input_types = self.input_types + else: + input_types = hints.input_types + if self._has_output_types(): + output_types = self.output_types + else: + output_types = hints.output_types + return IOTypeHints(input_types, output_types) + + def _has_input_types(self): + return self.input_types is not None and any(self.input_types) Review comment: self.input_types can be `None` or `((), {})`, and both of these values mean "no input hints." ```py >>> a = ((), {}) >>> bool(a) True >>> any(a) False ``` ---------------------------------------------------------------- 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: 304684) Time Spent: 13h 10m (was: 13h) > Design Py3-compatible typehints annotation support in Beam 3. > ------------------------------------------------------------- > > Key: BEAM-7060 > URL: https://issues.apache.org/jira/browse/BEAM-7060 > Project: Beam > Issue Type: Sub-task > Components: sdk-py-core > Reporter: Valentyn Tymofieiev > Assignee: Udi Meiri > Priority: Major > Fix For: 2.16.0 > > Time Spent: 13h 10m > Remaining Estimate: 0h > > Existing [Typehints implementaiton in > Beam|[https://github.com/apache/beam/blob/master/sdks/python/apache_beam/typehints/ > ] heavily relies on internal details of CPython implementation, and some of > the assumptions of this implementation broke as of Python 3.6, see for > example: https://issues.apache.org/jira/browse/BEAM-6877, which makes > typehints support unusable on Python 3.6 as of now. [Python 3 Kanban > Board|https://issues.apache.org/jira/secure/RapidBoard.jspa?rapidView=245&view=detail] > lists several specific typehints-related breakages, prefixed with "TypeHints > Py3 Error". > We need to decide whether to: > - Deprecate in-house typehints implementation. > - Continue to support in-house implementation, which at this point is a stale > code and has other known issues. > - Attempt to use some off-the-shelf libraries for supporting > type-annotations, like Pytype, Mypy, PyAnnotate. > WRT to this decision we also need to plan on immediate next steps to unblock > adoption of Beam for Python 3.6+ users. One potential option may be to have > Beam SDK ignore any typehint annotations on Py 3.6+. > cc: [~udim], [~altay], [~robertwb]. -- This message was sent by Atlassian Jira (v8.3.2#803003)