Skip to content

chore(py): bump ty to 0.0.82 - #134

Closed
huangjeff5 wants to merge 3 commits into
mainfrom
chore/py-bump-ty
Closed

huangjeff5 wants to merge 3 commits into
mainfrom
chore/py-bump-ty

Conversation

@huangjeff5

Copy link
Copy Markdown
Collaborator

Bumps ty from 0.0.17 to 0.0.82 and fixes what the new version reports. pyright and pyrefly results are unchanged.

Why

ty 0.0.17 picks an overload differently depending on keyword-argument order. When two overloads both accept a call and a TypeVar is solved from only one argument, passing arguments in a different order than the parameters gives Unknown:

ai.define_prompt(name='c', model='bistro/chef', prompt='x', config=BistroConfig(), output_schema=Dish)
# ty 0.0.17: Unknown
# ty 0.0.82, pyright, pyrefly: Prompt[Any, Dish]

It's fixed upstream as of 0.0.33 (likely astral-sh/ty#3139). I picked 0.0.82 because it had the fewest diagnostics on main of the versions I tried from 0.0.33 to 0.0.104. Latest (0.0.104) reports 176.

What changed in ty that matters

ty no longer honors # type: ignore[<code>] with a code. A bare # type: ignore and # ty: ignore[rule] still work. Most of the 143 new diagnostics were on lines that already carried a coded # type: ignore[...]. Those comments stay, since pyright still reads them, and the line gets a # ty: ignore[<rule>] where the code is wrong on purpose.

How each category was handled

143 diagnostics before, 0 after.

  • Tests that do something wrong on purpose (95 sites): a flow that returns a dict where Receipt is annotated to test return validation, temprature= to test that it raises, positional args to a keyword-only plugin constructor, writes to a frozen ref, monkeypatched methods. Each line gets a targeted # ty: ignore[<rule>].
    • invalid-return-type 31
    • invalid-assignment 21
    • too-many-positional-arguments 17
    • unknown-argument 18
    • unresolved-attribute 8
    • and a few missing-argument / invalid-type-form / invalid-attribute-override / unresolved-import
  • Real fixes in src (no signature or runtime change):
    • StoppableStream is now covariant in its chunk type. It only hands chunks out, and StreamResponse's chunk type is already covariant, so StreamResponse.stream / __aiter__ were a variance error.
    • _core/_channel.py: the stop callback was a lambda returning bool | None where () -> None is expected. It's a small def now.
    • _ai/_aio.py: anyio.abc.TaskGroup didn't resolve through the module attribute. It's imported from anyio.abc now.
    • _ai/_tools.py: define_interrupt built its metadata through a callable() check that ty can't narrow. It checks None / dict first now; same behavior for every input the signature allows.
    • _ai/_agents/_transports/_http.py: header resolution checks dict before calling the provider, for the same narrowing reason.
    • _ai/_agents/_runtime.py: a detached turn always has a store. That's now a cast instead of a type: ignore.
    • genkit-google-cloud: _GOOGLE_EXC_STATUS is typed dict[str, StatusName], and the exporter's temporality map is annotated dict[type, AggregationTemporality].
    • genkit-openai: the dict branch of tool-call parsing casts to dict[str, Any] once.
    • genkit-anthropic: dropped a cast that in narrowing already covers.
  • Real fixes in tests and samples: cast / assert narrowing where the test already checked the type (middleware config, prompt chunks, otel attributes, firestore mutator, log exporter records), a typed next callback, schema.model_validate({...}) instead of kwargs on a type[BaseModel]. In samples/agents/basic/09_detach.py, the SessionSnapshot[JobState] annotation named the non-generic public class; it's inferred now.
  • Unused ty: ignore removed: 15 (fastapi handler, reflection middleware, Action input-stream cast, mcp tool _meta, tool-response contract).

Ignores added in src, with reasons

  • _core/_channel.py: asyncio.Queue._queue is a private deque typeshed doesn't declare.
  • _core/_error.py ×2: http_code / retry_after_ms_from_headers take any object on purpose; int() raising, or a non-mapping raising AttributeError, is the check.
  • genkit-mcp: the exceptiongroup backport is only installed on 3.10.
  • genkit-ollama ×2: ollama's chat overloads type format narrower than the schema dict it accepts. pyright already had the same ignore.
  • genkit-openai: reasoning_content is read inside try/except AttributeError on purpose.
  • _ai/_prompt.py: GenerateActionOptions.use is typed for wire refs, but middleware instances pass through. pyright already had the same ignore.

Follow-up

The define_prompt ty ignore in ref_config_contract_test.py on #46 (ty < 0.0.33 bug ...) can come out once #46 and this PR are both on main.

Not changed here

These would change a public type, so they're left for a separate decision:

  • DetachedTask.poll() / wait() return the generic SessionSnapshot[StateT] from _ai/_agents/_client.py, but genkit.exp.agent.SessionSnapshot is the non-generic core class. Users can't spell the return type.
  • Prompt.__call__(on_chunk=...) is typed Callable[[object], None], so callers cast to read .text.

Breaking changes

None. Dev-dependency bump. Runtime and public signatures are unchanged, apart from StoppableStream widening to covariant.

ty 0.0.17 picked overloads differently depending on keyword-argument
order, typing define_prompt(..., prompt=, config=, output_schema=) as
Unknown. Fixed upstream in 0.0.33; 0.0.82 has the fewest new
diagnostics of the versions tried.

ty no longer honors coded `# type: ignore[...]`, so intentional
wrong-on-purpose test lines get a targeted `ty: ignore[rule]`. Real
issues are fixed: StoppableStream is covariant in its chunk type, a
bool-returning stop callback becomes a def, anyio TaskGroup is imported
directly, define_interrupt and HTTP header resolution narrow without
callable(), and plugin maps get precise types. 15 unused ty ignores
are removed. pyright and pyrefly results are unchanged.
Resolves conflict in tool_response_contract_test.py and updates ty 0.0.82
diagnostics for newer main changes:
- BidiConnection is now contravariant in StreamInT (via _CloseableSink)
- Tool.from_action uses method-local TypeVars (_I, _O, _C)
- Removes now-unused ty ignores in _action.py, action_test.py, and
  ref_config_contract_test.py (the ty < 0.0.33 workaround)
- Drops now-redundant ModelResponseChunk cast in prompt_test.py
- Adds targeted ty ignores for intentional invalid calls in tests
# Conflicts:
#	pyproject.toml
#	uv.lock
@huangjeff5

Copy link
Copy Markdown
Collaborator Author

Closing — we'll jump straight to latest ty in a single PR instead of churning type ignores twice.

@huangjeff5 huangjeff5 closed this Dec 1, 2026
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.

1 participant