You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "Abacn (via GitHub)" <gi...@apache.org> on 2026/06/03 20:18:27 UTC

[PR] Normalize types in dataclass field type resolving [beam]

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

   **Please** add a meaningful description for your change here
   
   ------------------------
   
   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://github.com/apache/beam/blob/master/CONTRIBUTING.md#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/actions/workflows/build_wheels.yml/badge.svg?event=schedule&&?branch=master)](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/actions/workflows/python_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/actions/workflows/java_tests.yml/badge.svg?event=schedule&&?branch=master)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/actions/workflows/go_tests.yml/badge.svg?event=schedule&&?branch=master)](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 or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
   


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

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

   ## [Codecov](https://app.codecov.io/gh/apache/beam/pull/38797?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
   :white_check_mark: All modified and coverable lines are covered by tests.
   :white_check_mark: Project coverage is 56.67%. Comparing base ([`bec756f`](https://app.codecov.io/gh/apache/beam/commit/bec756f89b89c01bfca992a409bd5f3b8669f9f2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`b0b27c8`](https://app.codecov.io/gh/apache/beam/commit/b0b27c880e32cad3ea7ae099f1f88dfa874054ca?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
   :warning: Report is 4 commits behind head on master.
   
   <details><summary>Additional details and impacted files</summary>
   
   
   
   ```diff
   @@             Coverage Diff              @@
   ##             master   #38797      +/-   ##
   ============================================
   - Coverage     56.68%   56.67%   -0.01%     
     Complexity     3637     3637              
   ============================================
     Files          1183     1183              
     Lines        187908   187923      +15     
     Branches       3751     3751              
   ============================================
   + Hits         106509   106511       +2     
   - Misses        77933    77946      +13     
     Partials       3466     3466              
   ```
   
   | [Flag](https://app.codecov.io/gh/apache/beam/pull/38797/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Δ | |
   |---|---|---|
   | [python](https://app.codecov.io/gh/apache/beam/pull/38797/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | `79.37% <100.00%> (-0.02%)` | :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.
   </details>
   
   [:umbrella: View full report in Codecov by Harness](https://app.codecov.io/gh/apache/beam/pull/38797?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).   
   :loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache).
   <details><summary> :rocket: New features to boost your workflow: </summary>
   
   - :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
   - :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
   </details>


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

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

   beam_PreCommit_Yaml_Xlang_Direct time out on HEAD as well, not related to this change. Merging for now.


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

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

   R: @chamikaramj @jrmccluskey 


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #38797:
URL: https://github.com/apache/beam/pull/38797#discussion_r3353014832


##########
sdks/python/apache_beam/options/pipeline_options.py:
##########
@@ -888,6 +888,15 @@ def _add_argparse_args(cls, parser):
         default=False,
         action='store_true',
         help='Disable the use of beartype for type checking.')
+    parser.add_argument(
+        '--exclude_infer_dataclass_field_type',
+        default=False,
+        action='store_true',
+        help='Exclude certain typehint inference involving dataclass fields '
+        'and resolve to Any (as in beam<=2.74.0). NOTE: this option is '
+        'for backward compatibility only and the exclusion senario is subject '
+        'to change, until removing in a future version. For details see: '
+        'https://beam.apache.org/releases/pydoc/current/apache_beam.typehints.trivial_inference.html#apache_beam.typehints.trivial_inference.resolve_dataclass_field_type')  # pylint: disable=line-too-long

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Fix typo: 'senario' should be 'scenario'.
   
   ```suggestion
           help='Exclude certain typehint inference involving dataclass fields '
           'and resolve to Any (as in beam<=2.74.0). NOTE: this option is '
           'for backward compatibility only and the exclusion scenario is subject '
           'to change, until removing in a future version. For details see: '
           'https://beam.apache.org/releases/pydoc/current/apache_beam.typehints.trivial_inference.html#apache_beam.typehints.trivial_inference.resolve_dataclass_field_type')  # pylint: disable=line-too-long
   ```



##########
sdks/python/apache_beam/typehints/trivial_inference.py:
##########
@@ -774,3 +774,33 @@ def infer_return_type_func(f, input_types, debug=False, depth=0):
   if debug:
     print(f, id(f), input_types, '->', result)
   return result
+
+
+def resolve_dataclass_field_type(x):
+  """
+  Resolve a type to Beam typehint under global pipeline option context.
+
+  Since Beam 2.75.0, typehints of dataclass fields are honored during type
+  inferences. However, in case of breakage (possible scenarios include
+  incorrect typehints; non-deterministic or nullable types disallowed by
+  consumer transform but check disabled by Any; tests rely on Any),
+  --exclude_infer_dataclass_field_type option to instruct falling back to Any.
+  Fields of builtin primitives are always respected.
+  """
+  from apache_beam.options.pipeline_options_context import get_pipeline_options
+  options = get_pipeline_options()
+  if options:
+    from apache_beam.options.pipeline_options import TypeOptions
+    try:
+      disabled = options.view_as(TypeOptions).exclude_infer_dataclass_field_type
+    except AttributeError:
+      print(options.view_as(TypeOptions)._visible_option_list())
+      raise

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Avoid using `print()` in library code as it writes directly to standard output, which can interfere with user logs or stdout-based tooling. Use the standard `logging` module instead.
   
   ```suggestion
       except AttributeError:
         import logging
         logging.warning(
             "Failed to retrieve exclude_infer_dataclass_field_type option. "
             "Visible options: %s",
             options.view_as(TypeOptions)._visible_option_list())
         raise
   ```



##########
sdks/python/apache_beam/typehints/opcodes.py:
##########
@@ -42,6 +42,7 @@
 from apache_beam.typehints.trivial_inference import Const
 from apache_beam.typehints.trivial_inference import element_type
 from apache_beam.typehints.trivial_inference import key_value_types
+from apache_beam.typehints.trivial_inference import resolve_dataclass_field_type
 from apache_beam.typehints.trivial_inference import union

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Importing `resolve_dataclass_field_type` at the top level of `opcodes.py` introduces a circular import risk. Since `trivial_inference.py` imports `opcodes` at the top level, and `resolve_dataclass_field_type` is defined at the end of `trivial_inference.py`, importing it here will fail with an `ImportError` during module initialization because the name is not yet defined when `opcodes` is loaded. Removing this top-level import and importing it locally where needed resolves this issue.
   
   ```suggestion
   from apache_beam.typehints.trivial_inference import union
   ```



##########
sdks/python/apache_beam/typehints/opcodes.py:
##########
@@ -451,8 +452,9 @@ def _getattr(o, name):
   elif inspect.isclass(o) and dataclasses.is_dataclass(o):
     field = o.__dataclass_fields__.get(name)
     if field is not None:
-      return field.type
+      return resolve_dataclass_field_type(field.type)

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Import `resolve_dataclass_field_type` locally inside `_getattr` to avoid circular import issues during module initialization.
   
   ```suggestion
         from apache_beam.typehints.trivial_inference import resolve_dataclass_field_type
         return resolve_dataclass_field_type(field.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


Re: [PR] Normalize types in dataclass field type resolving [beam]

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

   Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control. If you'd like to restart, comment `assign set of reviewers`


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

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


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on PR #38797:
URL: https://github.com/apache/beam/pull/38797#issuecomment-4618351240

   ## Summary of Changes
   
   Hello, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
   
   This pull request enhances Apache Beam's Python type inference by correctly resolving dataclass field types instead of defaulting to 'Any'. To ensure backward compatibility and mitigate potential breaking changes for existing pipelines, a new configuration flag has been added, providing users with a mechanism to opt-out of this stricter type inference.
   
   ### Highlights
   
   * **Dataclass Type Inference**: Updated the type inference mechanism to honor dataclass field typehints, improving type safety and accuracy in Apache Beam pipelines.
   * **Backward Compatibility**: Introduced a new pipeline option, `--exclude_infer_dataclass_field_type`, allowing users to revert to the previous 'fallback-to-any' behavior if necessary.
   * **Testing**: Added comprehensive test cases to verify both the new type inference behavior and the backward compatibility flag.
   
   <details>
   <summary><b>New Features</b></summary>
   
   🧠 You can now enable **Memory** (public preview) to help **Gemini Code Assist** learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. **Click [here](https://codeassist.google/code-review/login) to enable Memory in your admin console.**
   </details>
   
   <details>
   <summary><b>Using Gemini Code Assist</b></summary>
   <br>
   
   The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips.
   
   <b>Invoking Gemini</b>
   
   You can request assistance from Gemini at any point by creating a comment using either `/gemini <command>` or `@gemini-code-assist <command>`. Below is a summary of the supported commands on the current page.
   
   Feature | Command | Description
   --- | --- | ---
   Code Review | `/gemini review` | Performs a code review for the current pull request in its current state.
   Pull Request Summary | `/gemini summary` | Provides a summary of the current pull request in its current state.
   Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments.
   Help | `/gemini help` | Displays a list of available commands.
   
   <b>Customization</b>
   
   To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a `.gemini/` folder in the base of the repository. Detailed instructions can be found [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github).
   
   <b>Limitations & Feedback</b>
   
   Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up [here](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG).
   </details>
   
   
   [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487).
   


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


Re: [PR] Normalize types in dataclass field type resolving [beam]

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


##########
sdks/python/apache_beam/typehints/opcodes.py:
##########
@@ -42,6 +42,7 @@
 from apache_beam.typehints.trivial_inference import Const
 from apache_beam.typehints.trivial_inference import element_type
 from apache_beam.typehints.trivial_inference import key_value_types
+from apache_beam.typehints.trivial_inference import resolve_dataclass_field_type
 from apache_beam.typehints.trivial_inference import union

Review Comment:
   > Since trivial_inference.py imports opcodes at the top level,
   
   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.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org