Skip to content

Type checking not catching invalid keys inside of loops #197

@2018kguo

Description

@2018kguo

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions