Skip to content

Promote mixed-type list literals like NumPy (#539) - #543

Merged
ciaranra merged 2 commits into
devfrom
list-dtype-promotion
Aug 17, 2026
Merged

Promote mixed-type list literals like NumPy (#539)#543
ciaranra merged 2 commits into
devfrom
list-dtype-promotion

Conversation

@ciaranra

Copy link
Copy Markdown
Member

What

Fixes #539. pecos.array([0, 1.5, True]) produced a bool array, silently destroying the 1.5. Root cause: the generic inference path had a correct promotion lattice for int -> float -> complex, but bool was wired backwards -- promote_type_to_bool DEMOTED accumulated integers and floats into != 0 bools whenever a Python bool appeared. In NumPy's lattice, bool is the bottom: it never drags a numeric type down.

The fix inverts the flow and deletes the demoter:

  • A bool claims the dtype only while nothing has been accumulated (so [True, False] is still bool); otherwise it is absorbed into the already-promoted type as 0/1.
  • Integers, floats, and u64-range values arriving after bools lift the accumulated bools upward (promote_bools_to_i64 / promote_bools_to_f64; the complex direction already existed and was correct).
  • Mixing bools with Pauli/PauliString literals now raises a clear TypeError (previously an internal shape error).

The #540 homogeneous fast paths are untouched -- they bail to this generic path on any mixed list, which is exactly where the defect lived.

Verification

  • New oracle tests: 11 mixed-literal cases asserting dtype and values against the installed NumPy, both element orders, including [True, 2**63] -> uint64, complex both directions, and nested [[1, True], [2.5, False]]; plus the Pauli-mixing TypeError.
  • Full pecos-rslib suite: 2,124 pass. Downstream quantum-pecos suite: 4,764 pass.
  • Cold cargo clippy -p pecos-rslib --all-targets -- -D warnings, cargo fmt --check, unshielded pre-commit run --all-files: clean.

@ciaranra
ciaranra merged commit 803eacb into dev Aug 17, 2026
42 checks passed
@ciaranra
ciaranra deleted the list-dtype-promotion branch August 17, 2026 14:35
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.

Mixed-type list construction infers dtype from the first element instead of promoting like NumPy

1 participant