Fix some ty errors in collections. #664
Draft
kmontemayor2-sc wants to merge 4 commits into
Draft
Conversation
Resolves 4 production-code ignores via typed-local snapshots: - frozen_dict.py: __eq__ snapshots Mapping arg to typed dict - sorted_dict.py: __init__ uses typed-empty + .update(); __eq__ snapshots like frozen_dict - gcs.py: extract __storage_client.project to typed self.__project once at construction with assert isinstance check - tf_value_encoder.py: typed value_list local escapes Any-taint Patches gcs_test.py to set mock_client.project before instantiating GcsUtils (required by the new assert). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replaces `assert isinstance(...)` with an explicit `raise TypeError` so the runtime check survives `python -O`. Per CLAUDE.md's "Fail Fast on Invalid State" guidance, asserts are not appropriate for narrowing load-bearing runtime invariants. Also tightens `value_list: list` to `value_list: list[Any]` in tf_value_encoder.py. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replace `dict(other.items())` with `cast(Mapping[Any, Any], other)` in FrozenDict.__eq__ and SortedDict.__eq__. `isinstance(other, Mapping)` loses generic params (ty narrows to Mapping[Unknown, Unknown]) so indexing `other[self_key]` fails ty. The previous fix cloned the input to relabel it as `dict[Any, Any]` — that's O(n) memory per __eq__ call. The cast is zero-runtime-cost and preserves self.__dict's dict[KT, VT] precision for the rest of the class. Verified against ty 0.0.31: no annotation-only or iteration-only alternative clears the diagnostic without weakening self.__dict to Any. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
The gcs.py __project extraction is conceptually distinct from this PR's typed-local snapshot pattern — it addresses an upstream stub gap in google-cloud-storage where Client.project is typed Optional[str] but is always str at runtime after Client.__init__. Moving it to a focused PR (kmontemayor/ty_gcs_project_narrowing) so PR E stays scoped to our own collection/encoder code. The `# ty: ignore[invalid-argument-type]` at gcs.py:136 returns; the focused PR clears it. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Scope of work done
More cleanup from ty migration, this time related to collections.
Where is the documentation for this feature?: N/A
Did you add automated tests or write a test plan?
Updated Changelog.md? NO
Ready for code review?: NO