lidatong / dataclasses-json

Easily serialize Data Classes to and from JSON
MIT License
1.34k stars 151 forks source link

fix mypy error when assigning to dataclass_json_config #486

Closed MickaelBergem closed 8 months ago

MickaelBergem commented 9 months ago

Given our code:

from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin, LetterCase, config

camelCaseConfig = config(letter_case=LetterCase.CAMEL)["dataclasses_json"]

@dataclass()
class MyDataStructure(DataClassJsonMixin):
    dataclass_json_config = camelCaseConfig

    name: str

Running mypy yields the following error:

error: Incompatible types in assignment (expression has type "dict[Any, Any]", base class "DataClassJsonMixin" defined the type as "None") [assignment]

The type is "defined as None" because no explicit type is given when initializing the variable.

This PR provides the correct explicit type for dataclass_json_config.

MickaelBergem commented 9 months ago

Hello there 👋

This is my first contribution to this project, let me know if some information is missing :)

george-zubrienko commented 8 months ago

Please sync with main and I'll merge :)

MickaelBergem commented 8 months ago

@george-zubrienko thanks! I've rebased the PR on the latest commit from master.

navimac commented 5 months ago

I don't know if this is related to this issue but when serializing object with dataclasses-json==0.6.2 I get new key "dataclass_json_config": null, which ruins my serialization and was not present in version 0.6.1 We didn't chage code for serialziation in any way and after using this version this issue appeared. Here is just example how it looks like

with _dataclasses-json==0.6.2_ -->
{
  "name": "DemoPro*****",
  "version": "v2_****",
  "poList": [
    {
.....
        {
          _**"dataclass_json_config": null,**_
          "name": "S_CAN*******",
 .....
        }
      ],
....
with _dataclasses-json==0.6.1_ -->
{
  "name": "DemoPro*****",
  "version": "v2_****",
  "poList": [
    {
.....
        {
          "name": "S_CAN*******",
 .....
        }
      ],
....

I reverted the change dataclass_json_config: Optional[dict] = None to dataclass_json_config = None and now I don't get this extra key.

george-zubrienko commented 5 months ago

I don't know if this is related to this issue but when serializing object with dataclasses-json==0.6.2 I get new key "dataclass_json_config": null, which ruins my serialization and was not present in version 0.6.1

We didn't chage code for serialziation in any way and after using this version this issue appeared. Here is just example how it looks like


with _dataclasses-json==0.6.2_ -->

{

  "name": "DemoPro*****",

  "version": "v2_****",

  "poList": [

    {

.....

        {

          _**"dataclass_json_config": null,**_

          "name": "S_CAN*******",

 .....

        }

      ],

....

with _dataclasses-json==0.6.1_ -->

{

  "name": "DemoPro*****",

  "version": "v2_****",

  "poList": [

    {

.....

        {

          "name": "S_CAN*******",

 .....

        }

      ],

....

I reverted the change

dataclass_json_config: Optional[dict] = None to dataclass_json_config = None

and now I don't get this extra key.

@navimac can you raise an issue? Looks like a regression to me, will take a look