-
Notifications
You must be signed in to change notification settings - Fork 167
typing: improve applicator overloads #333
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
Draft
BobTheBuidler
wants to merge
1
commit into
ethereum:main
Choose a base branch
from
BobTheBuidler:type-hints/applicator-overloads
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # no-unused-vars | ||
| from collections.abc import ( | ||
| Callable, | ||
| ) | ||
| from typing import ( | ||
| Any, | ||
| TypeGuard, | ||
| ) | ||
|
|
||
| from eth_utils import ( | ||
| apply_formatter_at_index, | ||
| apply_formatter_if, | ||
| apply_formatter_to_array, | ||
| apply_formatters_to_sequence, | ||
| apply_one_of_formatters, | ||
| combine_argument_formatters, | ||
| ) | ||
| from eth_utils.curried import ( | ||
| apply_formatter_if as curried_apply_formatter_if, | ||
| apply_one_of_formatters as curried_apply_one_of_formatters, | ||
| ) | ||
|
|
||
|
|
||
| def is_text_value(value: str | int) -> TypeGuard[str]: | ||
| return isinstance(value, str) | ||
|
|
||
|
|
||
| def is_text_condition(value: str) -> bool: | ||
| return isinstance(value, str) | ||
|
|
||
|
|
||
| def uppercase(value: str) -> str: | ||
| return value.upper() | ||
|
|
||
|
|
||
| def test_sequence_formatter_inputs() -> None: | ||
| array_result: tuple[int, ...] = apply_formatter_to_array( # noqa: F841 | ||
| int, ("1", "2") | ||
| ) | ||
| sequence_result: tuple[int, ...] = apply_formatters_to_sequence( # noqa: F841 | ||
| (int, int), ("1", "2") | ||
| ) | ||
| index_result: tuple[int | str, ...] = apply_formatter_at_index( # noqa: F841 | ||
| str, 1, (1, 2, 3) | ||
| ) | ||
| combined: Callable[ # noqa: F841 | ||
| [list[Any]], list[Any] | ||
| ] = combine_argument_formatters(str, int) | ||
|
|
||
|
|
||
| def test_condition_formatter_inputs() -> None: | ||
| value: str | int = "abc" | ||
| guarded_result: str | int = apply_formatter_if( # noqa: F841 | ||
| is_text_value, uppercase, value | ||
| ) | ||
| bool_result: str | int = apply_formatter_if( # noqa: F841 | ||
| is_text_condition, len, "abc" | ||
| ) | ||
| curried_result: Callable[ # noqa: F841 | ||
| [str | int], str | int | ||
| ] = curried_apply_formatter_if(is_text_value, uppercase) | ||
| condition_first = curried_apply_formatter_if(is_text_value) | ||
| formatter_first: Callable[[str | int], str | int] = condition_first( # noqa: F841 | ||
| uppercase | ||
| ) | ||
|
|
||
|
|
||
| def test_one_of_formatter_inputs() -> None: | ||
| formatter_pairs: tuple[tuple[Callable[[str], bool], Callable[[str], str]], ...] = ( | ||
| (is_text_condition, uppercase), | ||
| ) | ||
| direct_result: str = apply_one_of_formatters(formatter_pairs, "abc") # noqa: F841 | ||
| curried_result: Callable[ # noqa: F841 | ||
| [str], str | ||
| ] = curried_apply_one_of_formatters(formatter_pairs) | ||
| curried_value: str = curried_result("abc") # noqa: F841 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When branches use
TypeGuards for different alternatives—such as anobject -> TypeGuard[str]condition paired with astr -> strformatter and anobject -> TypeGuard[int]condition paired with anint -> intformatter—this sharedTArgumentrequires every formatter to accept the originalobject. Mypy therefore rejects this type-safe and natural use ofapply_one_of_formatters; the same constraint is duplicated in the curried overloads. Model each condition/formatter pair's narrowed input independently so heterogeneous guarded branches remain usable.Useful? React with 👍 / 👎.