Skip to content

Fix Python DetachStream return nullability - #231

Merged
leileizhang (lei9444) merged 2 commits into
mainfrom
lei9444-detach-stream-nullability
Oct 30, 2026
Merged

leileizhang (lei9444) merged 2 commits into
mainfrom
lei9444-detach-stream-nullability

Conversation

@lei9444

@lei9444 leileizhang (lei9444) commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Correct generated Python DataWriter.detach_stream() and DataReader.detach_stream() return types to IOutputStream | None and IInputStream | None. An unbound writer, a buffer-backed reader, or a repeated detach can return None, but the generated stubs previously declared a non-null stream.

The fix adds exact reviewed facts for these classes and their IDataWriter/IDataReader declarations to the existing null-result table. Regression coverage checks class protocols, package and namespace facades, and independently resolved interface projections without widening unrelated results.

Reproduction

Prerequisites: Windows with the Windows SDK installed, the matching dynwinrt Python runtime, and mypy.

Steps:

  1. Generate and install the bindings:
cargo run -p dynwinrt-codegen -- generate --class-name Windows.Storage.Streams.DataWriter,Windows.Storage.Streams.DataReader --lang py --output .\generated
python -m pip install .\generated
  1. Save this as repro.py, then run python -m mypy --strict repro.py and python repro.py:
from dynwinrt import RoApartment, projected_lifetime_scope
from generated.windows.storage.streams import DataWriter

with RoApartment(), projected_lifetime_scope(), DataWriter() as writer:
    writer.detach_stream().flush_async()

Actual behavior: Strict checking accepts the unchecked access, but execution raises AttributeError: 'NoneType' object has no attribute 'flush_async'.

After the fix: Strict checking rejects the access because the result can be None. Saving the result and checking stream is not None before using it is accepted. Runtime behavior is unchanged.

Validation

Strict mypy and Pyright consumers accept guarded access and reject unchecked access through classes, protocols and declaring interfaces. Native calls verify absent streams, the original stream identity on the first attached detach, and None on repeated detach for both readers and writers. The generated src-layout package was also installed and exercised through its site-packages import rather than the source directory.

Compatibility and risks

Regenerate and reinstall existing bindings to receive the corrected declarations. Previously accepted unchecked calls may now require a None guard. Native null conversion and the broader inline Python annotations are unchanged.

Record exact nullable results for DataReader/DataWriter and their declaring interfaces. Cover generated stubs, guarded and unguarded strict consumers, and native stream detach states.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Mixed-language test coverage

Workflow status: ❌ Failed

Layer Lines Functions Branches/regions
Rust, including native .pyd/.node 69.98% 60.46% 69.41% regions

View workflow run and download full HTML/LCOV/XML reports

Preserve both Python CI checks and adapt the DetachStream regression to main's generated src layout and import helpers.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@lei9444
leileizhang (lei9444) merged commit 6a848e9 into main Oct 30, 2026
13 of 14 checks passed
@lei9444
leileizhang (lei9444) deleted the lei9444-detach-stream-nullability branch October 30, 2026 05:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants