Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/status/PYTHON_CHECKLIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ of a dynamic projection.
- [x] Derive Python symbols, implementation modules, facade modules, and stable
path hashes from a namespace- and kind-preserving semantic type identity,
including recursively closed generic arguments.
- [x] Derive closed generic interface Protocol markers from that full semantic
identity, not local projection aliases, so independently generated packages
agree only on the same closure even when methods do not use its arguments.
- [x] Keep same-short-name types available through their namespace facades,
omit ambiguous root exports, and use deterministic qualified aliases in
consumer modules instead of selecting a short-name ABI implicitly.
Expand Down
5 changes: 5 additions & 0 deletions tools/dynwinrt-codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,11 @@ After `--no-pyi`, include the earlier types when regenerating with stubs enabled
(or fully regenerate the package). A typed append fails rather than publishing
imports of missing retained declarations or dropping interfaces from the union.

Closed generic Python interface markers also require a one-time
[stub migration](python/README.md#closed-generic-stub-migration): regenerate
complete affected packages together rather than mixing old projection-name
markers with new semantic-identity markers, even for the same closed interface.

The npm wrapper accepts the legacy `--source-map`, `--declaration`, and
`--no-declaration` flags as no-ops. The Rust command accepts only `js` and `py`
for `--lang`.
Expand Down
20 changes: 20 additions & 0 deletions tools/dynwinrt-codegen/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,26 @@ The output directory belongs to codegen; do not store handwritten files in it.
After changing metadata files, SDK versions, or reference inputs, regenerate the
complete output.

### Closed-generic stub migration

Python stubs now identify closed generic interfaces by their complete semantic
identity instead of a local projection name. This prevents different type
arguments from becoming interchangeable when their short names collide.

Stubs generated before this fix and regenerated stubs are not type-compatible
for the same closed interface in either direction. This is a typing migration:
the generated `.py` runtime code and native interface IIDs are unchanged.

Regenerate the **complete output of every generated Python package exchanging
these interfaces** with the fixed generator, including runtime classes that
implement or require them, then rebuild/reinstall the affected packages.
Use a fresh codegen-owned output directory with all original type selections
and metadata/reference inputs. An incremental append that retains old `.pyi`
declarations is not sufficient. Do not copy individual marker declarations or
add legacy fallbacks: they can restore the incorrect cross-interface acceptance.
The new markers agree across independently regenerated packages for the same
closed identity, even when their local projection names differ.

## Platform and limitations

- The standalone generator has `py3-none-win_amd64` and
Expand Down
23 changes: 18 additions & 5 deletions tools/dynwinrt-codegen/src/codegen/winrt/python/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

use std::collections::HashSet;

use sha2::{Digest, Sha256};

use crate::meta::{ClassMeta, InterfaceMeta, MethodMeta};
use crate::types::{TypeKind, TypeMeta};

Expand Down Expand Up @@ -113,7 +115,16 @@ fn identity_marker(prefix: &str, namespace: &str, name: &str) -> String {
}

fn interface_marker(interface: &InterfaceMeta) -> String {
identity_marker("iid", &interface.namespace, &interface.name)
if interface.generic_piid.is_some() {
// Projection aliases depend on which other types a package contains.
// Hash the full, case-sensitive semantic identity, including nested arguments.
format!(
"_dynwinrt_iid_g{:x}",
Sha256::digest(interface.type_identity().canonical_key().as_bytes())
)
} else {
identity_marker("iid", &interface.namespace, &interface.name)
}
}

fn interface_symbol(context: &PythonProjectionContext, interface: &InterfaceMeta) -> String {
Expand Down Expand Up @@ -227,6 +238,11 @@ pub fn generate_interface_stub(context: &PythonProjectionContext, iface: &Interf
let context = context.as_ref();
let mut projected_iface = iface.clone();
projected_iface.name = context.projected_name_for_interface(iface);
let marker = interface_marker(if iface.generic_piid.is_some() {
iface
} else {
&projected_iface
});
let iface = &projected_iface;
let is_delegate = iface.is_delegate();
if is_delegate {
Expand Down Expand Up @@ -421,10 +437,7 @@ pub fn generate_interface_stub(context: &PythonProjectionContext, iface: &Interf
});
let identity_name = format!("_{}Identity", iface.name);
out.push_str(&format!("\nclass {identity_name}(Protocol):\n"));
out.push_str(&format!(
" def {}(self) -> None: ...\n",
interface_marker(iface)
));
out.push_str(&format!(" def {marker}(self) -> None: ...\n"));

let mut bases = vec![identity_name];
if is_protocol {
Expand Down
Loading
Loading