-
Notifications
You must be signed in to change notification settings - Fork 222
Open
Description
Type checking is not catching invalid key lookups within a for loop
Repro (Monty)
# repro_monty.py
from pydantic_monty import Monty, MontyTypingError
stubs = """
from typing import TypedDict
class SearchTag(TypedDict):
id: str
"""
loop_code = """
def main() -> None:
arr: list[SearchTag] = [{'id': 'u1'}]
for result in arr:
_x = result['uuid'] # expected invalid-key
main()
"""
direct_code = """
def main() -> None:
arr: list[SearchTag] = [{'id': 'u1'}]
_x = arr[0]['uuid'] # does raise invalid-key
main()
"""
for name, code in [('loop', loop_code), ('direct', direct_code)]:
try:
Monty(code, type_check=True, type_check_stubs=stubs)
print(name, 'NO_TYPE_ERROR')
except MontyTypingError as e:
print(name, 'TYPE_ERROR')
print(str(e).splitlines()[0])Observed:
- loop NO_TYPE_ERROR
- direct TYPE_ERROR (error[invalid-key]: Unknown key "uuid" ...)
Repro (ty CLI)
# repro_ty.py
from typing import TypedDict
class SearchTag(TypedDict):
id: str
def main() -> None:
arr: list[SearchTag] = [{'id': 'u1'}]
for result in arr:
_x = result['uuid'] # should error
main()Run:
ty check --python-version 3.14 repro_ty.py
Observed:
- error[invalid-key] is correctly reported.
Notes
- Isolated this with ty commit 6ded4bed1651e30b34dd04cdaa50c763036abb0d: it reports invalid-key with ty’s own/default typeshed, but shows the same missing loop check when --typeshed points to Monty’s vendored typesheds.
Version
pydantic-monty==0.0.7, macOS ARM64, Python 3.14
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels