Skip to content

TypedDict with total=False does not allow clear #12732

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
haroldmeerwaldt opened this issue May 5, 2022 · 6 comments
Closed

TypedDict with total=False does not allow clear #12732

haroldmeerwaldt opened this issue May 5, 2022 · 6 comments
Labels
bug mypy got something wrong topic-typed-dict

Comments

@haroldmeerwaldt
Copy link

haroldmeerwaldt commented May 5, 2022

Bug Report

I made a TypedDict with total=False . Then I wanted to clear it, but I got an [attr-defined] error. I can do pop, so I would expect to be able to do clear as well.

To Reproduce

from typing import TypedDict


class MyTypedDict(TypedDict, total=False):
    param1: int
    param2: str

td: MyTypedDict = {"param1": 2, "param2": "foo"}
td.pop("param1")
td.clear()

Actual Behavior

I got

scratch_37.py:10: error: "MyTypedDict" has no attribute "clear"  [attr-defined]

Your Environment

  • Mypy version used: 0.950
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files):
# The following options are set by the --strict command line flag
warn_unused_configs=True
disallow_any_generics=True
disallow_subclassing_any=True
disallow_untyped_calls=True
disallow_untyped_defs=True
disallow_incomplete_defs=True
check_untyped_defs=True
disallow_untyped_decorators=True
no_implicit_optional=True
warn_redundant_casts=True
warn_unused_ignores=True
warn_return_any=True
no_implicit_reexport=True
strict_equality=True
# End of --strict options

ignore_missing_imports=True
show_error_codes=True

plugins=pydantic.mypy, numpy.typing.mypy_plugin
  • Python version used: 3.9.9
  • Operating system and version: Windows 10
@haroldmeerwaldt haroldmeerwaldt added the bug mypy got something wrong label May 5, 2022
@alicederyn
Copy link

This behaviour is correct. According to https://peps.python.org/pep-0589/

clear() is not safe since it could remove required keys, some of which may not be directly visible because of structural subtyping. popitem() is similarly unsafe, even if all known keys are not required (total=False).

@AlexWaygood AlexWaygood closed this as not planned Won't fix, can't repro, duplicate, stale Mar 17, 2023
@JukkaL
Copy link
Collaborator

JukkaL commented Mar 17, 2023

If somebody still really wants to allow this, the best bet would be to define a specific error code for these errors which could be disabled globally (I'm not promising that we'd accept a PR, but we might).

@alicederyn
Copy link

alicederyn commented Mar 17, 2023

I believe the correct approach would be to declare the type @final, disallowing the structural subtyping that is making the clear illegal. However, mypy doesn't handle that correctly. I opened #12732 #14914 to track.

@AlexWaygood
Copy link
Member

I opened #12732 to track.

I think you got the issue number wrong — #12732 points to this issue :)

@alicederyn
Copy link

That's a good point lol, #14914

@JukkaL
Copy link
Collaborator

JukkaL commented Mar 17, 2023

A great point, supporting @final would be a better way to deal with this.

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