question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

AttributeError: type object 'dict' has no attribute '__args__'

See original GitHub issue

Note: This issue is somewhat related to: #13, #12, but it wasn’t resolved in #16. I get the same error, but for different input.

My code: @dataclass_json() @dataclass class A: b: dict

@dataclass_json() @dataclass class X: x: List[A]

output = {“x”: [{ "a: {“b”: {“False”: 0.1, “True”: 0.3}}}]}

response = x.from_json(output)

The Error:

when type=_dict, value={“b”: {“False”: 0.1, “True”: 0.3}}

` def decode_generic(type, value, infer_missing): if value is None: res = value elif issubclass_safe(type, Enum): # Convert to an Enum using the type as a constructor. Assumes a direct match is found. res = type_(value) # FIXME this is a hack to fix a deeper underlying issue. A refactor is due. elif is_collection(type): if is_mapping(type):

          k_type, v_type = type_.__args__

E AttributeError: type object ‘dict’ has no attribute ‘args’`

My very hacky solution: To solve my very specific issue, I changed the code of _decode_generic

            try:
                k_type, v_type = type_.__args__
            except:
                k_type, v_type = None, None

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:3
  • Comments:10

github_iconTop GitHub Comments

6reactions
rothncommented, Feb 11, 2021

I really hope someone fixes this

2reactions
YiweiJiang2015commented, Aug 12, 2022

I just come across this due to a similar issue in another library. From there, I learn the solution is to specify the element type inside the container type e.g. list[int]. For @kodekracker’s issue

from dataclasses_json import DataClassJsonMixin
from dataclasses import dataclass

@dataclass
class SimpleExample(DataClassJsonMixin):
    int_field: int
    list_field: list[int] # specify the element type

simple_example = SimpleExample(1,[1,2,3])
d = simple_example.to_dict()
print(d)  # no error
>>> {'int_field': 1, 'list_field': [1, 2, 3]}

se = SimpleExample.from_dict(d)
print(se) # no error
>>> SimpleExample(int_field=1, list_field=[1, 2, 3])

For OP’s nested structure, I haven’t found good solutions. The code below does not work.

from dataclasses_json import dataclass_json
@dataclass_json()
@dataclass
class A:
    b: dict[str, int] #  specify the key-value type

@dataclass_json()
@dataclass
class X:
    x: list[A]

output = '{"x": [{ "a": {"b": {"False": 0.1, "True": 0.3}}}]}' # without the quotes, an error will be thrown
print(X.from_json(output)  # raise KeyError in recognizing "b"
Read more comments on GitHub >

github_iconTop Results From Across the Web

python - AttributeError: 'dict' object has no attribute 'predictors'
The AttributeError is an exception thrown when an object does not have the attribute you tried to access. The class dict does not...
Read more >
AttributeError: 'dict' object has no attribute in Python | bobbyhadz
The "AttributeError: 'dict' object has no attribute" means that we are trying to access an attribute or call a method on a dictionary...
Read more >
Python AttributeError: 'dict' object has no attribute 'append'
In this tutorial, you'll learn how to solve the Python AttributeError: 'dict' object has no attribute 'append' error.
Read more >
Issue 45572: urllib.request:AttributeError: 'dict' object has no ...
msg404767 ‑ (view) Author: tongxiaoge (sxt1001) Date: 2021‑10‑22 13:35 msg406813 ‑ (view) Author: Andrei Kulakov (andrei.avk) * Date: 2021‑11‑23 02:45 msg411318 ‑ (view) Author: Andrei...
Read more >
'dict' object has no attribute 'get_debug_tool_name' - Debugging
AttributeError : 'dict' object has no attribute 'get_debug_tool_name' ... line 1128, in __call__ return self.main(*args, **kwargs) File ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found