Preserve element order when initializing Python dictionary #57
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pandas DataFrames were rendering incorrectly in IPython. This bug was reported in philipturner/swift-colab#22, and seems to have been around in the swift-jupyter era. The culprit is how a dictionary literal currently converts into a Python object.
PythonKit/PythonKit/Python.swift
Lines 1466 to 1468 in f12defa
The new implementation preserves element order in the final Python object, unlike
Dictionary.pythonObject
. When keys are duplicated, throw the same runtime error asSwift.Dictionary.init(dictionaryLiteral:)
. This differs from Python's key uniquing semantics, which silently override an existing key with the next one it encounters.The previous implementation used
Dictionary.init(uniquingKeysWith:)
and retained only the first duplicate key's value. Changing the rule introduces API breakage, but is the most semantically correct. We want to create a Swift-first API, and duplicating keys in a literal is bad/unsafe practice.Additional changes
prefix func -
.Members
initializer ofPythonClass
to follow the uniqueness semantics above. The alternative initializer usesDictionary
, which up until now had different behavior upon key duplication.