Uploaded image for project: 'Beam'
  1. Beam
  2. BEAM-8487

Python typehints: support forward references

Details

    • Bug
    • Status: Open
    • P3
    • Resolution: Unresolved
    • None
    • None
    • sdk-py-core
    • None

    Description

      Typehints may be given as string literals: https://www.python.org/dev/peps/pep-0484/#forward-references
      These are currently not evaluated and result in errors.
      Example 1:

        def test_typed_callable_string_hints(self):
          def do_fn(element: 'int') -> 'typehints.List[str]':
            return [[str(element)] * 2]
      
          result = [1, 2] | beam.ParDo(do_fn)
          self.assertEqual([['1', '1'], ['2', '2']], sorted(result))
      

      This results in:

      >     return issubclass(sub, base)
      E     TypeError: issubclass() arg 2 must be a class or tuple of classes
      
      typehints.py:1168: TypeError
      

      Example 2:

        def test_typed_dofn_string_hints(self):
          class MyDoFn(beam.DoFn):
            def process(self, element: 'int') -> 'typehints.List[str]':
              return [[str(element)] * 2]
      
          result = [1, 2] | beam.ParDo(MyDoFn())
          self.assertEqual([['1', '1'], ['2', '2']], sorted(result))
      

      This results in:

      >     raise ValueError('%s is not iterable' % type_hint)
      E     ValueError: typehints.List[str] is not iterable
      
      typehints.py:1194: ValueError
      

      where the non-iterable entity the error refers to is a string literal ("typehints.List[str]").

      Attachments

        Activity

          People

            Unassigned Unassigned
            udim Udi Meiri
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 2h 10m
                2h 10m