Repository navigation
Python: Fix Windows Pyright errors for POSIX-only os flags in file_access sample - #9161
Conversation
Replace literal os.O_DIRECTORY/os.O_NOFOLLOW/os.O_NONBLOCK references in the foundry-hosted-agents responses file_access sample with a small getattr-based _posix_flag() helper. These flags are absent from the Windows type stubs, so Pyright failed with reportAttributeAccessIssue on Windows even though the sample only ever executes the POSIX code path. The helper still fails closed at runtime (raises RuntimeError when a flag is missing), preserving the existing no-follow/descriptor-relative security guarantees. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🟢 Approval recommended
The implementation preserves secure behavior, with only a minor diagnostic wording issue noted.
1 open finding
What changed in this PR
Replaces direct POSIX-only os flag access with runtime lookup to keep Windows Pyright checks clean while preserving fail-closed file access.
Changes:
- Adds
_posix_flagfor guarded flag lookup. - Updates directory, read, and write flags to use the helper.
| File | Description |
|---|---|
python/samples/04-hosting/foundry-hosted-agents/responses/files/file_access.py |
Resolves POSIX flags dynamically while retaining security checks. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
There was a problem hiding this comment.
MAF Automated Review — Iteration 1
Result: No findings
Scope: full PR (1 commit(s)): f12bf2fa2052
Model: gpt-5.6-sol
Overview
The PR replaces direct references to POSIX-only flags with a private dynamic lookup so Windows Pyright can analyze the sample. Missing flags still raise before file I/O, while descriptor-relative opens, no-follow behavior, non-blocking opens, file-type checks, and descriptor cleanup remain intact. No new publishable Critical, High, or Medium issue was established.
Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

Motivation & Context
Windows Pyright reports six
reportAttributeAccessIssueerrors inpython/samples/04-hosting/foundry-hosted-agents/responses/files/file_access.pybecause
os.O_DIRECTORY,os.O_NOFOLLOW, andos.O_NONBLOCKare absentfrom the Windows type stubs, even though the sample only ever executes its
POSIX descriptor-relative code path (it already fails closed with a
RuntimeErroron platforms that lack these flags). This blocksuv run poe pyright -Son Windows contributor/CI machines.Description & Review Guide
os.O_NOFOLLOW/os.O_DIRECTORY/os.O_NONBLOCKattribute references with a small_posix_flag(name)helper that looks the flag up viagetattr(os, name, None)and raises the same
RuntimeErroras before when it is missing. The priorhasattrguard in_directory_flagsis replaced by this helper, and the twoother call sites (
_read_file,write_local_upload) now go through it as well.behavior is unchanged. No-follow, descriptor-relative opens, and the existing
fail-closed guard are fully preserved — a missing flag still raises before any
file I/O is attempted.
getattr-basedlookup doesn't weaken the POSIX security checks (no-follow / dir_fd requirements)
and still fails closed identically to the previous
hasattrguard.Related Issue
Fixes #
Contribution Checklist
breaking changelabel (or add "[BREAKING]" to the title prefix, before or after any language prefix) — a workflow keeps the label and title prefix in sync automatically.