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

Wrong type checking on TypedDict with optional parameters #84

Closed
NixBiks opened this issue Jul 9, 2020 · 4 comments
Closed

Wrong type checking on TypedDict with optional parameters #84

NixBiks opened this issue Jul 9, 2020 · 4 comments

Comments

@NixBiks
Copy link

NixBiks commented Jul 9, 2020

Environment data

  • Language Server version: 2020.6.1
  • OS and version: Debian GNU/Linux 10 (buster)
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.3

Expected behaviour

I'd expect pylance to not flag any issues with the following code that passes just fine.

from typing import Optional, List, TypedDict


class SubOrder(TypedDict):
    name: str


class Order(TypedDict):
    name: str
    sub_orders: Optional[List[SubOrder]]


assert Order(name="Pizza") == {"name": "Pizza"}
assert Order(name="Pizza", sub_orders=[SubOrder(name="Pasta")]) == {
    "name": "Pizza",
    "sub_orders": [{"name": "Pasta"}],
}

Actual behaviour

However I get the following message regarding the line with Order(name="Pizza").

Argument missing for parameter "sub_orders"

Another issue is that the IntelliSense doesn't seem to work either. It just shows Order(*kwargs, **kwargs) when I start typing Order(

@FrozenBob
Copy link

This behavior is correct. "Runs fine" doesn't say anything about whether your static types are correct, and your static types are not in fact correct. Your Order is defined as requiring an entry for 'sub_orders'. That entry may be set to None, but it must not be entirely absent.

@NixBiks
Copy link
Author

NixBiks commented Jul 9, 2020

Hmm okay. Is it possible to achieve what I want then? I've tried fiddling with Union of two Order types, one with sub_orders and one without. No luck though.

@erictraut
Copy link
Contributor

If you want to specify that some of the keys within the TypedDict can be completely absent, you can use total=False in its definition, like this:

class Order(TypedDict, total=False):
    name: str
    sub_orders: Optional[List[SubOrder]]

For more details about TypedDict, see PEP 589 documentation.

@NixBiks
Copy link
Author

NixBiks commented Jul 9, 2020

Alright thanks for your answers and a great product

@NixBiks NixBiks closed this as completed Jul 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants