Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UserWarning occurs when using None | str as type in dataclass and using scheam.load(). #407

Closed
yuji38kwmt opened this issue May 29, 2023 · 3 comments · Fixed by #419
Closed
Assignees
Labels
bug Something isn't working

Comments

@yuji38kwmt
Copy link

yuji38kwmt commented May 29, 2023

Environment

  • Python 3.11.2
  • dataclasses-json 0.5.7

What's happen?

When I exected the following python file, UserWarning occurred.

from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin


@dataclass(frozen=True)
class Project(DataClassJsonMixin):
    project_id: str
    project_name: None | str = None


print("Use `Project.from_dict()`")
print(Project.from_dict({"project_id": "1"}))
print("Use `Project.schema().load()`")
print(Project.schema().load([{"project_id": "2"}], many=True))
$ python sample1.py 
Use `Project.from_dict()`
Project(project_id='1', project_name=None)
Use `Project.schema().load()`
/home/vagrant/.pyenv/versions/3.11.2/lib/python3.11/site-packages/dataclasses_json/mm.py:271: UserWarning: Unknown type <class 'NoneType'> at Project.project_name: None | str It's advised to pass the correct marshmallow type to `mm_field`.
  warnings.warn(
[Project(project_id='2', project_name=None)]

However I changed project_name's type from None | str to Optional[str], UserWarning did not occurre.

from dataclasses import dataclass
from dataclasses_json import DataClassJsonMixin
from typing import Optional


@dataclass(frozen=True)
class Project(DataClassJsonMixin):
    project_id: str
    project_name: Optional[str] = None


print("Use `Project.from_dict()`")
print(Project.from_dict({"project_id": "1"}))
print("Use `Project.schema().load()`")
print(Project.schema().load([{"project_id": "2"}], many=True))

What do I expect ?

I expect that even if I use None | str instead of Optional[str], the UserWarning should not occur. Both None | str and Optional[str] have the same meaning.

Supplementary

Related to https://qiita.com/yuji38kwmt/items/85a8bd836f06f3df8321 .

@george-zubrienko
Copy link
Collaborator

I see according to PEP they are indeed equivalent, I'll take a look at this

https://peps.python.org/pep-0604/

@george-zubrienko george-zubrienko self-assigned this Jun 1, 2023
@george-zubrienko george-zubrienko added the bug Something isn't working label Jun 1, 2023
@george-zubrienko
Copy link
Collaborator

Alright I think problem is with typing-inspect lib, which we should really just replace as this one is really aimed at typing module, while the pipe overload is not coming from there.

I'll prepare a PR this week and create a follow-up to replace that lib.

@george-zubrienko
Copy link
Collaborator

Alright partially correcting myself here, issue is really much more simple than that. Linking a PR that should fix it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants