Question
Feature request: include inferred variable type in invalid-key diagnostic for union TypedDicts
Summary
When accessing a key on a variable whose inferred type is a union of TypedDicts (e.g. TypedDictA | TypedDictB), ty emits separate invalid-key errors for each member where the key doesn't exist. The error message only mentions the specific TypedDict where the key is invalid, not the full inferred type of the variable.
This makes it impossible for programmatic consumers of ty output to distinguish:
- Key invalid on one branch of a union — may be intentional (e.g. user probing for a key that only exists on some branches)
- Key truly invalid on a single-typed variable — likely a bug
If this cannot be implement, could you also share some ideas how I can parse the output distinguish the two cases? Thanks!
Reproduction
from typing import TypedDict
class DictA(TypedDict):
foo: str
class DictB(TypedDict):
bar: int
def get_a() -> DictA:
return {"foo": "hello"}
def get_b() -> DictB:
return {"bar": 42}
# Case 1: union variable after if/else
if condition:
resp = get_a()
else:
resp = get_b()
# resp is DictA | DictB — accessing "foo" is valid on DictA branch
val = resp["foo"] # ty reports: Unknown key "foo" for TypedDict `DictB`
# Case 2: single-typed variable
x = get_a()
bad = x["bar"] # ty reports: Unknown key "bar" for TypedDict `DictA`
Current output (concise)
file.py:19:14: error[invalid-key] Unknown key "foo" for TypedDict `DictB`
file.py:22:14: error[invalid-key] Unknown key "bar" for TypedDict `DictA`
Both errors look identical in structure — there is no way to tell from the output that line 19 involves a union type while line 22 does not.
Requested enhancement
Include the full inferred type of the variable in the invalid-key diagnostic. For example:
file.py:19:14: error[invalid-key] Unknown key "foo" for TypedDict `DictB` (variable type: `DictA | DictB`)
file.py:22:14: error[invalid-key] Unknown key "bar" for TypedDict `DictA` (variable type: `DictA`)
Or alternatively, emit a note/info line with the inferred type, which is already done for other diagnostics like invalid-assignment.
Motivation
We are building a code validator that uses ty to check TypedDict field access on some API response typeddict. We want to programmatically parse the ty output to know that the error is invalid on a union type. So that we can then programmatically check if the key exists in any of the union members. If the key exists, we don't block the code from execution.
Version
ty 0.0.29
Question
Feature request: include inferred variable type in
invalid-keydiagnostic for union TypedDictsSummary
When accessing a key on a variable whose inferred type is a union of TypedDicts (e.g.
TypedDictA | TypedDictB),tyemits separateinvalid-keyerrors for each member where the key doesn't exist. The error message only mentions the specific TypedDict where the key is invalid, not the full inferred type of the variable.This makes it impossible for programmatic consumers of
tyoutput to distinguish:If this cannot be implement, could you also share some ideas how I can parse the output distinguish the two cases? Thanks!
Reproduction
Current output (concise)
Both errors look identical in structure — there is no way to tell from the output that line 19 involves a union type while line 22 does not.
Requested enhancement
Include the full inferred type of the variable in the
invalid-keydiagnostic. For example:Or alternatively, emit a note/info line with the inferred type, which is already done for other diagnostics like
invalid-assignment.Motivation
We are building a code validator that uses
tyto check TypedDict field access on some API response typeddict. We want to programmatically parse the ty output to know that the error is invalid on a union type. So that we can then programmatically check if the key exists in any of the union members. If the key exists, we don't block the code from execution.Version
ty 0.0.29