Skip to content

TypedDict.values() always returns object. #10759

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

Closed
Dreamsorcerer opened this issue Jul 2, 2021 · 4 comments
Closed

TypedDict.values() always returns object. #10759

Dreamsorcerer opened this issue Jul 2, 2021 · 4 comments
Labels
bug mypy got something wrong topic-typed-dict

Comments

@Dreamsorcerer
Copy link
Contributor

from typing import TypedDict

class A(TypedDict):
    a: str

def foo(a: A):
    for v in reveal_type(a.values()):
        v.lower()

This produces:

test.py:7: note: Revealed type is 'typing.ValuesView[builtins.object]'
test.py:8: error: "object" has no attribute "lower"
Found 1 error in 1 file (checked 1 source file)

Expected Behavior

TypedDict.values() to return a different type if all values of a TypedDict are of the same type.

Your Environment

  • Mypy version used: 0.910
@Dreamsorcerer Dreamsorcerer added the bug mypy got something wrong label Jul 2, 2021
@JukkaL
Copy link
Collaborator

JukkaL commented Feb 28, 2022

TypedDict types support structural subtyping, so it's always possible that there is a subtype of A that has a value with a non-str type:

class B(TypedDict):
    a: str
    b: int

b: B = {'b': 1, 'a': 'y'}
foo(b)

One way to make this work as proposed could be to support final TypedDict types that don't allow additional keys in subtypes.

@JukkaL JukkaL closed this as completed Feb 28, 2022
@Dreamsorcerer
Copy link
Contributor Author

I think when I use TypedDict, I'm always thinking about it as not allowing additional keys. So, I guess a final type is probably what I'm missing (though, generally, I'd still like it to be subclassable, just the opposite of total=False).

@JelleZijlstra
Copy link
Member

Pyright supports @Final TypedDicts; it would be a useful addition for mypy too. Not sure if there's already an issue about it.

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 1, 2022

Created #12266.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-typed-dict
Projects
None yet
Development

No branches or pull requests

4 participants