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 marked as @final not assignable to Mapping #14441

Closed
DetachHead opened this issue Jan 13, 2023 · 3 comments
Closed

TypedDict marked as @final not assignable to Mapping #14441

DetachHead opened this issue Jan 13, 2023 · 3 comments

Comments

@DetachHead
Copy link
Contributor

DetachHead commented Jan 13, 2023

from typing import Mapping, TypedDict, final

@final
class Foo(TypedDict):
    foo: int
    
foo: Foo

bar: Mapping[str, int] = foo # error: Incompatible types in assignment (expression has type "Foo", variable has type "Mapping[str, int]")  [assignment]

playground

@DetachHead DetachHead added the bug mypy got something wrong label Jan 13, 2023
@erictraut
Copy link

Mypy is correct in generating an error here. A TypedDict may have subtypes that include additional keys that are not specified in the parent class, so it's unsafe to assume that foo is compatible with Mapping[str, int].

If a TypedDict were marked as @final, it would be safe to assume that it is compatible with Mapping[str, <union or join of all value types>], but it's rare to see @final TypedDicts, and no Python type checkers currently implement this special-case logic. That would be an enhancement request rather than a bug report.

@DetachHead DetachHead changed the title TypedDict not assignable to Mapping TypedDict marked as @final not assignable to Mapping Jan 13, 2023
@DetachHead
Copy link
Contributor Author

oops i don't know how i missed that

@ichard26 ichard26 added feature topic-typed-dict topic-final PEP 591 and removed bug mypy got something wrong labels Jan 13, 2023
@AlexWaygood
Copy link
Member

Closing, since the basic feature request here is "support TypedDicts decorated with @final", which is a duplicate of #7981.

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Jan 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants