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

TypedDict has no key when checking for key #8887

Closed
Dreamsorcerer opened this issue May 25, 2020 · 5 comments
Closed

TypedDict has no key when checking for key #8887

Dreamsorcerer opened this issue May 25, 2020 · 5 comments
Labels
topic-type-narrowing Conditional type narrowing / binder topic-typed-dict

Comments

@Dreamsorcerer
Copy link
Contributor

Dreamsorcerer commented May 25, 2020

Minimal test case:

[case testTaggedUnionByKey]
from mypy_extensions import TypedDict
from typing import Union
class A(TypedDict):
    foo: int
class B(TypedDict):
    bar: str
def a(a: Union[A, B]):
    return a["foo"] if "foo" in a else None
[builtins fixtures/tuple.pyi]

The code is checking if the key is present, so this should pass correctly, but instead fails with error: TypedDict "B" has no key 'foo'.

@Dreamsorcerer
Copy link
Contributor Author

Dreamsorcerer commented Oct 19, 2020

Coming back to this, it looks like this should be supported by the tagged union pattern documented here:
https://mypy.readthedocs.io/en/stable/literal_types.html#tagged-unions

That documentation uses different Literal values on a key, but we should be able to achieve the same thing by testing whether a key exists or not.

@Dreamsorcerer
Copy link
Contributor Author

Dreamsorcerer commented Nov 13, 2020

Curiously, I can't even get the documented tagged union functionality to work in a test case, or find any code related to the feature. For example, this still fails:

[case testTaggedUnion]
from mypy_extensions import TypedDict
from typing import Union
from typing_extensions import Literal
class A(TypedDict):
    foo: Literal[2]
class B(TypedDict):
    foo: Literal[3]
    bar: str
def a(a: Union[A, B]):
    return a["bar"] if a["foo"] == 3 else None
[builtins fixtures/tuple.pyi]

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Nov 14, 2020

Your second example passes for me when I run mypy on it as a file (didn't try as a test); maybe you need a different fixture or something. You might be interested in #8151

@Dreamsorcerer
Copy link
Contributor Author

Thanks for digging that up, must just have been something wrong with the way I was running the test. I'm sure I've used the feature in my code before.

@AlexWaygood
Copy link
Member

I'm closing this issue, as the root cause of the mypy error here is discussed more precisely in #9953

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-type-narrowing Conditional type narrowing / binder topic-typed-dict
Projects
None yet
Development

No branches or pull requests

4 participants