
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
45 lines
1014 B
Plaintext
45 lines
1014 B
Plaintext
== Why is this an issue?
|
|
|
|
Dictionary unpacking requires an object with methods ``++__getitem__++`` and ``++keys++`` or ``++__getitem__++`` and ``++__getattr__++``. This is the case for any https://docs.python.org/3/glossary.html#term-mapping[mapping] object such as ``++dict++``. Using an object which doesn't have these methods will raise a ``++TypeError++``.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,python]
|
|
----
|
|
class A:
|
|
pass
|
|
|
|
dict(**A()) # Noncompliant
|
|
{'a': 10, 'b': 20, **A()} # Noncompliant
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,python]
|
|
----
|
|
class A:
|
|
def __getitem__(self, key):
|
|
return 2
|
|
|
|
def keys(self):
|
|
return ['1','2','3']
|
|
|
|
dict(**A()) # => {'1': 2, '2': 2, '3': 2}
|
|
{'a': 10, 'b': 20, **A()} # => {'a': 10, 'b': 20, '1': 2, '2': 2, '3': 2}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
X is of type Y and cannot be unpacked with "**". Use a "mapping" object instead.
|
|
|
|
|
|
endif::env-github,rspecator-view[]
|