Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempt to use more specific transformer when executing remote entities #3111

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

wild-endeavor
Copy link
Contributor

@wild-endeavor wild-endeavor commented Feb 5, 2025

Tracking issue

flyteorg/flyte#6156

Why are the changes needed?

Please see issue. When FlyteRemote tries to execute a fetched task, it doesn't have any Python types, it only has the Flyte types. This PR attempts to try to use the type() of the python value first, before resorting to guessing. The python type is valid, if the Flyte type inferred by the transformer associated with that python type, matches the literal type of the flyte entity.

What changes were proposed in this pull request?

Add a better guess.

How was this patch tested?

Setup process

Screenshots

Check all the applicable boxes

  • I updated the documentation accordingly.
  • All new and existing tests passed.
  • All commits are signed-off.

Related PRs

Docs link

Summary by Bito

This PR enhances Flytekit's remote entity execution through type engine optimization and strict type checking. The implementation introduces type_match_checking.py and type_engine.py modules, consolidating type operations with improved error handling. Key changes include renaming 'better_guess_type_hint' to 'strict_type_hint_matching', adding detailed error logging, and implementing a fallback mechanism for type guessing. These modifications support various types (enums, unions, collections) while ensuring more precise type validation.

Unit tests added: True

Estimated effort to review (1-5, lower is better): 2

Signed-off-by: Yee Hing Tong <[email protected]>
@flyte-bot
Copy link
Contributor

Code Review Agent Run Status

  • Limitations and other issues: ❌ Failure - The AI Code Review Agent skipped reviewing this change because it is configured to exclude certain pull requests based on the source/target branch or the pull request status. You can change the settings here, or contact the agent instance creator at [email protected].

Copy link

codecov bot commented Feb 6, 2025

Codecov Report

Attention: Patch coverage is 74.35897% with 10 lines in your changes missing coverage. Please review.

Project coverage is 76.11%. Comparing base (22acc2c) to head (7e8fd7c).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
flytekit/core/type_match_checking.py 73.33% 5 Missing and 3 partials ⚠️
flytekit/core/type_engine.py 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3111      +/-   ##
==========================================
- Coverage   80.01%   76.11%   -3.90%     
==========================================
  Files         318      204     -114     
  Lines       27075    21636    -5439     
  Branches     2779     2792      +13     
==========================================
- Hits        21663    16469    -5194     
+ Misses       4647     4344     -303     
- Partials      765      823      +58     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Yee Hing Tong <[email protected]>
@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 7, 2025

Code Review Agent Run #e0c471

Actionable Suggestions - 1
  • flytekit/remote/remote.py - 1
    • Consider adding error handling for transformer · Line 255-265
Review Details
  • Files reviewed - 3 · Commit Range: ad7e95a..400a996
    • flytekit/core/type_match_checking.py
    • flytekit/remote/remote.py
    • tests/flytekit/unit/core/test_type_match_checking.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 7, 2025

Changelist by Bito

This pull request implements the following key changes.

Key Change Files Impacted
Feature Improvement - Enhanced Type Matching System

type_engine.py - Added strict type hint matching functionality for better type inference

type_match_checking.py - New module for literal type matching with support for collections, enums, and unions

remote.py - Updated remote execution to use strict type matching for better type resolution

Testing - Type Matching Test Coverage

test_type_engine.py - Added tests for strict type matching functionality

test_type_match_checking.py - New test suite for literal type matching across different data types

flytekit/remote/remote.py Outdated Show resolved Hide resolved
Signed-off-by: Yee Hing Tong <[email protected]>
Signed-off-by: Yee Hing Tong <[email protected]>
Signed-off-by: Yee Hing Tong <[email protected]>
@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 7, 2025

Code Review Agent Run #2f3b74

Actionable Suggestions - 2
  • tests/flytekit/unit/core/test_type_engine.py - 1
  • flytekit/core/type_engine.py - 1
Review Details
  • Files reviewed - 4 · Commit Range: 400a996..7e8fd7c
    • flytekit/core/type_engine.py
    • flytekit/remote/remote.py
    • tests/flytekit/unit/core/test_type_engine.py
    • tests/flytekit/unit/core/test_type_match_checking.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

Signed-off-by: Yee Hing Tong <[email protected]>
@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 7, 2025

Code Review Agent Run #e0383a

Actionable Suggestions - 1
  • tests/flytekit/unit/core/test_type_engine.py - 1
Review Details
  • Files reviewed - 2 · Commit Range: 7e8fd7c..2e78438
    • flytekit/core/type_engine.py
    • tests/flytekit/unit/core/test_type_engine.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

Copy link
Collaborator

@eapolinario eapolinario left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one tiny comment, otherwise LGTM. Thank you!

flytekit/core/type_engine.py Outdated Show resolved Hide resolved
Signed-off-by: Yee Hing Tong <[email protected]>
@wild-endeavor wild-endeavor enabled auto-merge (squash) February 10, 2025 18:15
@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 10, 2025

Code Review Agent Run #b17d18

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 2e78438..57c0e7e
    • flytekit/core/type_engine.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

Signed-off-by: Yee Hing Tong <[email protected]>
@flyte-bot
Copy link
Contributor

flyte-bot commented Feb 11, 2025

Code Review Agent Run #67094d

Actionable Suggestions - 2
  • flytekit/remote/remote.py - 1
  • flytekit/core/type_engine.py - 1
Additional Suggestions - 1
  • tests/flytekit/unit/core/test_type_engine.py - 1
Review Details
  • Files reviewed - 3 · Commit Range: 57c0e7e..db2c3ab
    • flytekit/core/type_engine.py
    • flytekit/remote/remote.py
    • tests/flytekit/unit/core/test_type_engine.py
  • Files skipped - 0
  • Tools
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful

AI Code Review powered by Bito Logo

Comment on lines +1517 to +1522
type_hints[k] = strict_type_hint_matching(v, input_flyte_type_map[k].type)
except ValueError:
logger.debug(f"Could not guess type for {input_flyte_type_map[k].type}, skipping...")
developer_logger.debug(
f"Could not guess type for {input_flyte_type_map[k].type}, skipping..."
)
type_hints[k] = TypeEngine.guess_python_type(input_flyte_type_map[k].type)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider more specific error handling

Consider handling the ValueError exception more gracefully. Currently, if strict_type_hint_matching() fails, we fall back to TypeEngine.guess_python_type() which may also raise a ValueError. This could lead to confusing error messages for users. Consider adding more specific error handling.

Code suggestion
Check the AI-generated fix before applying
 -                            type_hints[k] = strict_type_hint_matching(v, input_flyte_type_map[k].type)
 -                        except ValueError:
 -                            developer_logger.debug(
 -                                f"Could not guess type for {input_flyte_type_map[k].type}, skipping..."
 -                            )
 -                            type_hints[k] = TypeEngine.guess_python_type(input_flyte_type_map[k].type)
 +                            type_hints[k] = strict_type_hint_matching(v, input_flyte_type_map[k].type)
 +                        except ValueError as e:
 +                            developer_logger.debug(f"strict_type_hint_matching failed: {str(e)}")
 +                            try:
 +                                type_hints[k] = TypeEngine.guess_python_type(input_flyte_type_map[k].type)
 +                            except ValueError as e2:
 +                                developer_logger.error(
 +                                    f"Failed to guess type for {input_flyte_type_map[k].type}: {str(e2)}"
 +                                )
 +                                raise

Code Review Run #67094d


Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

@@ -2423,6 +2424,27 @@ def dataclass_from_dict(cls: type, src: typing.Dict[str, typing.Any]) -> typing.
return cls(**constructor_inputs)


def strict_type_hint_matching(input_val: typing.Any, target_literal_type: LiteralType) -> typing.Type:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider impact of strict type matching

The function name change from better_guess_type_hint to strict_type_hint_matching better reflects its behavior since it raises ValueError on type mismatches rather than falling back to guessing. However, this is a breaking change that could impact existing code relying on the fallback behavior.

Code suggestion
Check the AI-generated fix before applying
Suggested change
def strict_type_hint_matching(input_val: typing.Any, target_literal_type: LiteralType) -> typing.Type:
def better_guess_type_hint(input_val: typing.Any, target_literal_type: LiteralType) -> typing.Type:
native_type = type(input_val)
try:
return strict_type_hint_matching(input_val, target_literal_type)
except ValueError:
return TypeEngine.guess_python_type(target_literal_type)
def strict_type_hint_matching(input_val: typing.Any, target_literal_type: LiteralType) -> typing.Type:

Code Review Run #67094d


Is this a valid issue, or was it incorrectly flagged by the Agent?

  • it was incorrectly flagged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants