mypy icon indicating copy to clipboard operation
mypy copied to clipboard

✨ Added new Error to TypedDict

Open JoaquimEsteves opened this issue 2 years ago • 7 comments

Fixes #4617

This allows the following code to trigger the error typeddict-unknown-key

A = T.TypedDict("A", {"x": int})

def f(x: A) -> None:
    ...

f({"x": 1, "y": "foo"})  # err: typeddict-unknown-key
f({"y": "foo"})  # err: typeddict-unknown-key & typeddict-item
f({"x": 'err', "y": "foo"})  # err: typeddict-unknown-key & typeddict-item

a: A = { 'x': 1 }

# You can set extra attributes
a['extra'] = 'extra' # err: typeddict-unknown-key
# Reading them produces the normal item error
err = a['does not exist'] # err: typeddict-item

The user can then safely ignore this specific error at their disgression.

JoaquimEsteves avatar Nov 30 '22 12:11 JoaquimEsteves

TODO:

  • [x] Add a small unit-test
  • [x] Ensure I don't trample over previous tests*

mypy used to only report extra keys and not missing keys. We now report both (I suspect the CI will yell at me and I'll try to correct those tests)

edit:

Having run the tests on my own terminal I can see a few that fail.

1 - as expected we now have two error codes instead of just the one. Will need to fix these. 2 - A few errors like: Expected TypedDict key "x" but found. There was an early return previously whenever we found extra keys. I'll have to add it back if there's missing or extra, or change the tests

~Will work on it on the weekend :)~ Done

JoaquimEsteves avatar Nov 30 '22 13:11 JoaquimEsteves

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Dec 02 '22 00:12 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Dec 04 '22 12:12 github-actions[bot]

Diff from mypy_primer, showing the effect of this PR on open source code:

discord.py (https://github.com/Rapptz/discord.py)
- discord/state.py:750: error: TypedDict "ButtonMessageComponentInteractionData" has no key "components"  [typeddict-item]
+ discord/state.py:750: error: TypedDict "ButtonMessageComponentInteractionData" has no key "components"  [typeddict-unknown-key]
- discord/state.py:750: error: TypedDict "SelectMessageComponentInteractionData" has no key "components"  [typeddict-item]
+ discord/state.py:750: error: TypedDict "SelectMessageComponentInteractionData" has no key "components"  [typeddict-unknown-key]

core (https://github.com/home-assistant/core)
+ homeassistant/components/energy/sensor.py:181: error: Unused "type: ignore" comment
+ homeassistant/components/energy/sensor.py:181: error: TypedDict "SolarSourceType" has no key "flow_from"  [typeddict-unknown-key]
+ homeassistant/components/energy/sensor.py:181: note: Error code "typeddict-unknown-key" not covered by "type: ignore" comment
+ homeassistant/components/energy/sensor.py:181: error: TypedDict "BatterySourceType" has no key "flow_from"  [typeddict-unknown-key]
+ homeassistant/components/energy/sensor.py:181: error: TypedDict "GasSourceType" has no key "flow_from"  [typeddict-unknown-key]
+ homeassistant/components/energy/sensor.py:181: error: TypedDict "WaterSourceType" has no key "flow_from"  [typeddict-unknown-key]

github-actions[bot] avatar Dec 05 '22 10:12 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Dec 08 '22 00:12 github-actions[bot]

Oh wait, there is one more thing, you need to add an entry in the docs to the list of error codes (otherwise it will be hard for people to find out the solution).

ilevkivskyi avatar Dec 24 '22 13:12 ilevkivskyi

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Dec 24 '22 13:12 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 23 '23 13:01 github-actions[bot]

@JoaquimEsteves could you please re-run black . locally, commit and push? It looks like black test is failing.

ilevkivskyi avatar Jan 23 '23 13:01 ilevkivskyi

Hello, apologies for the delay!

Went off on vacation and then real life™️ got in the way.

Hopefully this'll be OK to merge now.

A couple of three things:

Could you re-consider this comment? IMO it makes sense to keep the "Did you mean" for when the text is really similar.

I can't seem to get Black to collaborate with me. The CI/CD was complains that Black was returning an error on messages.py; however I don't have this problem on my local and my commit does not produce the changes shown.

Please review the text for the error_codes.rst. aspell didn't point out any spelling mistakes; but if you have any idea on how to improbe the text I'm all ears.

JoaquimEsteves avatar Jan 23 '23 13:01 JoaquimEsteves

The error message in CI says that black wants you to apply the following diff:

--- mypy/messages.py	2023-01-23 13:51:23.667146 +0000
+++ mypy/messages.py	2023-01-23 13:52:16.792518 +0000
@@ -1700,13 +1700,11 @@
                 f'TypedDict {format_type(typ)} has no key "{item_name}"', context, code=err_code
             )
             matches = best_matches(item_name, typ.items.keys(), n=3)
             if matches:
                 self.note(
-                    "Did you mean {}?".format(pretty_seq(matches, "or")),
-                    context,
-                    code=err_code,
+                    "Did you mean {}?".format(pretty_seq(matches, "or")), context, code=err_code
                 )
 
     def typeddict_context_ambiguous(self, types: list[TypedDictType], context: Context) -> None:
         formatted_types = ", ".join(list(format_type_distinctly(*types)))
         self.fail(

AlexWaygood avatar Jan 23 '23 14:01 AlexWaygood

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 23 '23 14:01 github-actions[bot]

Thanks @AlexWaygood; I was confused as it looked like my message.py was different from the one shown on the CD (turns out I needed to pull from the python:mypy).

The newest commit should have fixed all of the docs+black issues.

Do submit feedback about my writting on the docs; Otherwise I'll just press the commit button : )

JoaquimEsteves avatar Jan 23 '23 14:01 JoaquimEsteves

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 23 '23 14:01 github-actions[bot]

@AlexWaygood or @ilevkivskyi as a first-time contributor it seems I need one of you to request running the CD pipeline before approval (I foolishly merged master into this branch again, thinking that it would re-run the workflow)

JoaquimEsteves avatar Jan 23 '23 15:01 JoaquimEsteves

I'm afraid I'm not a maintainer, so I don't have the power to run the CI. I'm just a triager and contributor :)

AlexWaygood avatar Jan 23 '23 15:01 AlexWaygood

Sorry to bother you @AlexWaygood and @ilevkivskyi,

I seem to have hit a wall here with the run_primer; the action seems to run 4 times and fail twice, and at this stage I don't know what to do 😅

P.S. Apologies if pinging is rude - I don't think requesting another review was the appropriate action

JoaquimEsteves avatar Jan 25 '23 15:01 JoaquimEsteves

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 25 '23 15:01 github-actions[bot]

I seem to have hit a wall here with the run_primer; the action seems to run 4 times and fail twice, and at this stage I don't know what to do 😅

Looks like it was just a random fluke — closing and reopening the PR fixed it :)

AlexWaygood avatar Jan 25 '23 15:01 AlexWaygood

Could you re-consider https://github.com/python/mypy/pull/14225#discussion_r1043322392? IMO it makes sense to keep the "Did you mean" for when the text is really similar.

I would propose to keep the PR as is (i.e. with consistent use of error codes for error and note). We don't have a precedent for such inconsistency (e.g. IIRC # type: ignore[attr-defined] suppresses both error and the related suggestion). We can easily change this later if people will complain.

ilevkivskyi avatar Jan 25 '23 16:01 ilevkivskyi

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Jan 25 '23 16:01 github-actions[bot]