Skip to content

Also check the existence of _fields in addition to__annotations__ for NamedTuple. - #36337

Merged
tvalentyn merged 3 commits into
apache:masterfrom
yilei:py313
Oct 3, 2025
Merged

Also check the existence of _fields in addition to__annotations__ for NamedTuple.#36337
tvalentyn merged 3 commits into
apache:masterfrom
yilei:py313

Conversation

@yilei

@yilei yilei commented Sep 30, 2025

Copy link
Copy Markdown
Contributor

Why? This addresses an issue in Python 3.13 where typing.Tuple might gained an __annotations__ attribute and this will fail to match NamedTuple. (Part of #34869.)

I'm not sure if this (the extra __annotations__) is a bug in CPython or not, but regardless I think it's safer to check the existence of _fields.

from typing import Annotated, Protocol, Tuple, runtime_checkable
@runtime_checkable
class MyProtocol(Protocol):
    def method(self) -> None: ...

print(f"Before, {hasattr(Tuple, '__annotations__')=}")

# After the following check, typing.Tuple.__annotations__ now exists.
isinstance(Annotated[float, 1], MyProtocol)

print(f"After, {hasattr(Tuple, '__annotations__')=}")

Once above code is run, import apache_beam will fail in Python 3.13:

    from apache_beam import metrics
apache_beam/__init__.py:88: in <module>
    from apache_beam import io
apache_beam/io/__init__.py:21: in <module>
    from apache_beam.io.avroio import *
apache_beam/io/avroio.py:59: in <module>
    from apache_beam.io import filebasedsink
apache_beam/io/filebasedsink.py:29: in <module>
    from apache_beam.io import iobase
apache_beam/io/iobase.py:55: in <module>
    from apache_beam.transforms import Impulse
apache_beam/transforms/__init__.py:27: in <module>
    from apache_beam.transforms.stats import *
apache_beam/transforms/stats.py:377: in init apache_beam.transforms.stats
    ???
apache_beam/typehints/typehints.py:611: in __getitem__
    return schemas.union_schema_type(params)
apache_beam/typehints/schemas.py:659: in union_schema_type
    for field in zip(*[named_fields_from_element_type(t) for t in element_types]):
apache_beam/typehints/schemas.py:649: in named_fields_from_element_type
    return named_fields_from_schema(schema_from_element_type(element_type))
apache_beam/typehints/schemas.py:639: in schema_from_element_type
    return named_tuple_to_schema(element_type)
apache_beam/typehints/schemas.py:622: in named_tuple_to_schema
    return typing_to_runner_api(named_tuple, schema_registry).row_type.schema
apache_beam/typehints/schemas.py:201: in typing_to_runner_api
    schema_registry=schema_registry).typing_to_runner_api(type_)
apache_beam/typehints/schemas.py:304: in typing_to_runner_api
    row_type_constraint = row_type.RowTypeConstraint.from_user_type(type_)
apache_beam/typehints/row_type.py:109: in from_user_type
    for name in user_type._fields]
/root/.pyenv/versions/3.13.7/lib/python3.13/typing.py:1365: in __getattr__
    return getattr(self.__origin__, attr)
E   AttributeError: type object 'tuple' has no attribute '_fields'

Tested with Python 3.13.7.


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.

See the Contributor Guide for more tips on how to make review process smoother.

To check the build health, please visit 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
Python tests
Java tests
Go tests

See CI.md for more information about GitHub Actions CI or the workflows README to see a list of phrases to trigger workflows.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @yilei, I'm Gemini Code Assist1! 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 refines the mechanism for identifying NamedTuple types within Apache Beam's type hinting system. By switching the detection from relying on the __annotations__ attribute to the _fields attribute, the change resolves a potential compatibility issue with Python 3.13 where typing.Tuple could be mistakenly identified as a NamedTuple, leading to runtime errors. This ensures more accurate and stable type handling for NamedTuple instances across different Python versions.

Highlights

  • Python 3.13 Compatibility Fix: Addresses a compatibility issue in Python 3.13 where typing.Tuple might unexpectedly gain an __annotations__ attribute, causing NamedTuple detection to fail in Apache Beam.
  • Robust NamedTuple Detection: Changes the NamedTuple identification logic from checking for __annotations__ to checking for the more reliable _fields attribute, ensuring correct type recognition.
  • Preventing AttributeError: This change prevents an AttributeError: type object 'tuple' has no attribute '_fields' that could occur when apache_beam imports in Python 3.13 after certain typing module interactions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

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.

Customization

To customize 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.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 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.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions

Copy link
Copy Markdown
Contributor

Checks are failing. Will not request review until checks are succeeding. If you'd like to override that behavior, comment assign set of reviewers

@yilei
yilei marked this pull request as draft September 30, 2025 21:04
@yilei yilei changed the title Check the existence of _fields instead of __annotations__ for NamedTuple. Also check the existence of _fields in addition to__annotations__ for NamedTuple. Sep 30, 2025
@yilei
yilei force-pushed the py313 branch 3 times, most recently from 6fb8da4 to 8565e5b Compare October 1, 2025 04:51
@yilei

yilei commented Oct 1, 2025

Copy link
Copy Markdown
Contributor Author

assign set of reviewers

@yilei
yilei marked this pull request as ready for review October 1, 2025 06:20
@yilei

yilei commented Oct 1, 2025

Copy link
Copy Markdown
Contributor Author

assign set of reviewers

@yilei

yilei commented Oct 1, 2025

Copy link
Copy Markdown
Contributor Author

I checked the PreCommit error, and I don't think it's related to this PR? I also tried using a dummy comment change in the PR, some TestMilvusSearchEnrichment tests also failed in a similar way, so I'm concluding they are unrelated/flaky.

@github-actions

github-actions Bot commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@github-actions

github-actions Bot commented Oct 1, 2025

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @tvalentyn for label python.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@tvalentyn
tvalentyn merged commit df3384f into apache:master Oct 3, 2025
170 of 190 checks passed
@tvalentyn

Copy link
Copy Markdown
Contributor

Thanks @yilei !

@tvalentyn

Copy link
Copy Markdown
Contributor

Very nice to cross paths again with you!

@yilei

yilei commented Oct 3, 2025

Copy link
Copy Markdown
Contributor Author

@tvalentyn I'm glad too! And thank you for all things beam!

@yilei
yilei deleted the py313 branch October 3, 2025 04:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants