Skip to content

Incorrect "redundant cast" between total and partial dicts #12200

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
alicederyn opened this issue Feb 17, 2022 · 5 comments
Closed

Incorrect "redundant cast" between total and partial dicts #12200

alicederyn opened this issue Feb 17, 2022 · 5 comments
Labels

Comments

@alicederyn
Copy link

alicederyn commented Feb 17, 2022

Bug Report

Casting a total dict to an equivalent non-total dict incorrectly warns that the cast is redundant.

To Reproduce

class Permissive(TypedDict, total=False):
  id: str

class Restrictive(TypedDict):
  id: str

def convert(d: Restrictive) -> Permissive:
  return cast(Permissive, d)

Expected Behavior

No mypy errors/warnings

Actual Behavior

error: Redundant cast to "Permissive"

The mypy docs state "a total TypedDict is not valid when a partial one is expected", so this seems incorrect, and indeed if I remove the cast, I get:

Incompatible return value type (got "Restrictive", expected "Permissive")

Your Environment

  • Mypy version used: Checked with all versions between 0.812 and 0.931 inclusive
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
python_version = "3.9"
namespace_packages = true
explicit_package_bases = true
strict = true
  • Python version used: 3.9
  • Operating system and version: docker, looks like RHEL 7.9, running within Ubuntu 10 on WSL 2
@alicederyn alicederyn added the bug mypy got something wrong label Feb 17, 2022
@alicederyn
Copy link
Author

Apologies if this is a duplicate! I searched but the existing issues I found seemed like they were about other redundant cast bugs.

@alicederyn alicederyn changed the title Incorrect "redundant cast" Incorrect "redundant cast" between total and non-total dicts Feb 17, 2022
@alicederyn alicederyn changed the title Incorrect "redundant cast" between total and non-total dicts Incorrect "redundant cast" between total and partial dicts Feb 17, 2022
@A5rocks
Copy link
Collaborator

A5rocks commented Feb 17, 2022

Note that it should be a redundant cast under #12142... I'm not sure if it will be accepted though.

@sodul
Copy link

sodul commented Aug 1, 2023

We recently ran in a similar issue with boto stubs where strict output data from boto calls could not be re-used as input data for other boto calls where missing keys are allowed.

youtype/mypy_boto3_builder#208

The fix was apparently to remove the Output types and only use the types with Total=False, but this is a bit of a lie since the output types are supposed to contain all the keys.

Is there are better alternative than using cast() or always use total=False even if untrue?

@erictraut
Copy link

It appears this was fixed in mypy 0.981.

@hauntsaninja
Copy link
Collaborator

Fixed in #13311

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants