Skip to content

Fix some ty errors in collections. #664

Draft
kmontemayor2-sc wants to merge 4 commits into
mainfrom
kmontemayor/ty_pre_snapshot
Draft

Fix some ty errors in collections. #664
kmontemayor2-sc wants to merge 4 commits into
mainfrom
kmontemayor/ty_pre_snapshot

Conversation

@kmontemayor2-sc
Copy link
Copy Markdown
Collaborator

@kmontemayor2-sc kmontemayor2-sc commented Jun 2, 2026

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

kmonte and others added 4 commits June 1, 2026 18:27
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>
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