typing: add TypeGuard returns to type predicates - #332
Conversation
Improve downstream type narrowing for existing predicate helpers without changing their runtime validation behavior.
There was a problem hiding this comment.
💡 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".
|
|
||
|
|
||
| def is_binary_address(value: Any) -> bool: | ||
| def is_binary_address(value: Any) -> TypeGuard[Address]: |
There was a problem hiding this comment.
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 👍 / 👎.
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
TypeGuardreturn annotations to existing type, address, and hexadecimal predicates. Runtime bodies are unchanged, and a mypy fixture proves the expected narrowing.Todo:
Cute Animal Picture