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
11 changes: 8 additions & 3 deletions numba/core/typeinfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
new_error_context, termcolor, UnsupportedError,
ForceLiteralArg, CompilerError)
from numba.core.funcdesc import qualifying_prefix
from numba.core.typeconv import Conversion

_logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -337,12 +338,16 @@ def __call__(self, typeinfer):
strkey = all([isinstance(x, types.StringLiteral) for x in ktys])
literalvty = all([isinstance(x, types.Literal) for x in vtys])
vt0 = types.unliteral(vtys[0])
# homogeneous values comes in the form of being able to cast
# all the other values in the ctor to the type of the first

# homogeneous values comes in the form of being able to cast
# all the other values in the ctor to the type of the first.
# The order is important as `typed.Dict` takes it's type from
# the first element.
def check(other):
return typeinfer.context.can_convert(other, vt0) is not None
conv = typeinfer.context.can_convert(other, vt0)
return conv is not None and conv < Conversion.unsafe
homogeneous = all([check(types.unliteral(x)) for x in vtys])

# Special cases:
# Single key:value in ctor, key is str, value is an otherwise
# illegal container type, e.g. LiteralStrKeyDict or
Expand Down
14 changes: 9 additions & 5 deletions numba/tests/test_dictobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2145,18 +2145,22 @@ def value_type_mismatch(x):
def test_dict_value_coercion(self):
# checks that things coerce or not!

# safe convertible: TypedDict
p = {(np.int32, np.int32): types.DictType,
# unsafe but convertible: TypedDict
(np.int8, np.int32): types.DictType,
p = {# safe and no conversion: TypedDict
(np.int32, np.int32): types.DictType,
# safe and convertible: TypedDict
(np.int32, np.int8): types.DictType,
# safe convertible: TypedDict
(np.complex128, np.int32): types.DictType,
# unsafe not convertible: LiteralStrKey
(np.int32, np.complex128): types.LiteralStrKeyDict,
# unsafe not convertible: LiteralStrKey
(np.int32, np.array): types.LiteralStrKeyDict,
# unsafe not convertible: LiteralStrKey
(np.array, np.int32): types.LiteralStrKeyDict,}
(np.array, np.int32): types.LiteralStrKeyDict,
# unsafe not convertible: LiteralStrKey
(np.int8, np.int32): types.LiteralStrKeyDict,
# unsafe not convertible: LiteralStrKey (issue #6420 case)
(np.int64, np.float64): types.LiteralStrKeyDict,}

def bar(x):
pass
Expand Down