Type checker rejects the supported open() builtin
Author: DanielTemesgenCreated Sep 26, 2026Updated Sep 27, 2026
Description
Monty supports open() at runtime, but its bundled type information does not declare the builtin. As a result, valid mounted-filesystem programs fail when type checking is enabled.
I reproduced on main at 15753b35.
Reproduction
from pathlib import Path
from tempfile import TemporaryDirectory
import pydantic_monty
source = """\
with open('/work/example.txt', 'w') as output:
output.write('hello')
"""
with TemporaryDirectory() as host_dir:
mount = pydantic_monty.MountDir('/work', host_dir, mode='read-write')
pydantic_monty.Monty(source, type_check=True).run(mount=mount)
assert Path(host_dir, 'example.txt').read_text() == 'hello'Actual behavior
main.py:1:6: error[unresolved-reference] Name `open` used when not definedChanging only type_check=True to type_check=False allows the program to run and the mounted file contains hello.
Expected behavior
The program should pass type checking and write the file. Invalid calls, such as open() without a file argument or open('/work/x', encoding=1), should still produce type-checking errors.
Happy to contribute a fix for this.
Source: pydantic/monty