Skip to content

typing: add TypeGuard returns to type predicates - #332

Draft
BobTheBuidler wants to merge 1 commit into
ethereum:mainfrom
BobTheBuidler:type-hints/typeguard-predicate-returns
Draft

typing: add TypeGuard returns to type predicates#332
BobTheBuidler wants to merge 1 commit into
ethereum:mainfrom
BobTheBuidler:type-hints/typeguard-predicate-returns

Conversation

@BobTheBuidler

@BobTheBuidler BobTheBuidler commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Improve downstream type narrowing for existing predicate helpers without changing their runtime validation behavior.

What was wrong?

Predicate helpers returned plain bool, so downstream type checkers could not narrow values after successful runtime checks.

Related to Issue #
Closes #

How was it fixed?

Added safe TypeGuard return annotations to existing type, address, and hexadecimal predicates. Runtime bodies are unchanged, and a mypy fixture proves the expected narrowing.

Todo:

Cute Animal Picture

Cute kitten

Improve downstream type narrowing for existing predicate helpers without changing their runtime validation behavior.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e3c3a2b244

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread eth_utils/address.py


def is_binary_address(value: Any) -> bool:
def is_binary_address(value: Any) -> TypeGuard[Address]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve bytearray in binary-address narrowing

When value is a 20-byte bytearray, is_bytes() succeeds and this predicate returns True, but TypeGuard[Address] tells type checkers that the value is an Address backed by immutable bytes. Downstream operations such as hash(value) therefore type-check but raise TypeError at runtime. Include bytearray in the guarded type or stop accepting it; the same mismatch also affects is_canonical_address().

Useful? React with 👍 / 👎.

@BobTheBuidler
BobTheBuidler marked this pull request as draft July 29, 2026 10:19
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.

2 participants