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

"Arguments missing" for optional arguments in dataclass #893

Closed
talarari opened this issue Jan 28, 2021 · 4 comments
Closed

"Arguments missing" for optional arguments in dataclass #893

talarari opened this issue Jan 28, 2021 · 4 comments
Labels
waiting for upstream Waiting for upstream to release a fix

Comments

@talarari
Copy link

Environment data

  • Language Server version: Pylance language server 2021.1.3
  • OS and version: Mac os catalina
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7.8

Expected behaviour

Creating an instance of a dataclass without arguments for optional fields will show no error

Actual behaviour

creating an instacnce of a dataclass without arguments for optional fields shows and error "arguments missing"

image
image

@notatallshaw
Copy link

notatallshaw commented Jan 28, 2021

Have you tried running this code?

When I run this:

from dataclasses import dataclass
from typing import Optional

@dataclass
class User:
    name: str
    age: Optional[int]

user = User(name="Jon")

I get this output:

Traceback (most recent call last):
  File "data_class_test.py", line 9, in <module>    
    user = User(name="Jon")
TypeError: __init__() missing 1 required positional argument: 'age'

Optional from typing means the value can by the given type (int in this case) or it can be the None type.

For a field to be actually optional in the dataclass constructor you must give it a default value, in this case probably None:

@dataclass
class User:
    name: str
    age: Optional[int] = None

Which Pylance correctly handles (actually if this was a bug I assume it would be with Pyright not Pylance).

@erictraut
Copy link
Contributor

The notation Optional[int] means that the value must be of type None or int. It does not mean that it can be omitted from the dataclass. There is currently no way to specify that individual elements are not required. There is an ongoing discussion in the typing-sig about adding such a facility in Python 3.10, but no decision has been finalized yet.

Pylance is doing the right thing here based on PEP 557.

@erictraut
Copy link
Contributor

If you're interested in following the discussion in the typing-sig, here's a link.

@judej judej added the waiting for upstream Waiting for upstream to release a fix label Jan 28, 2021
@github-actions github-actions bot removed the triage label Jan 28, 2021
@talarari
Copy link
Author

talarari commented Feb 1, 2021

thanks for the info, using a default value makes sense.

@talarari talarari closed this as completed Feb 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for upstream Waiting for upstream to release a fix
Projects
None yet
Development

No branches or pull requests

4 participants