Skip to content

Commit

Permalink
Fix crash when indexing TypedDict with empty key (#15392)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored and jhance committed Jun 12, 2023
1 parent 5e6e488 commit 7886503
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -2989,8 +2989,9 @@ def _real_quick_ratio(a: str, b: str) -> float:


def best_matches(current: str, options: Collection[str], n: int) -> list[str]:
if not current:
return []
# narrow down options cheaply
assert current
options = [o for o in options if _real_quick_ratio(current, o) > 0.75]
if len(options) >= 50:
options = [o for o in options if abs(len(o) - len(current)) <= 1]
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -2873,3 +2873,15 @@ foo({"foo": {"e": "foo"}}) # E: Type of TypedDict is ambiguous, none of ("A", "
# E: Argument 1 to "foo" has incompatible type "Dict[str, Dict[str, str]]"; expected "Union[A, B]"
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

[case testTypedDictMissingEmptyKey]
from typing_extensions import TypedDict

class A(TypedDict):
my_attr_1: str
my_attr_2: int

d: A
d[''] # E: TypedDict "A" has no key ""
[builtins fixtures/dict.pyi]
[typing fixtures/typing-typeddict.pyi]

0 comments on commit 7886503

Please sign in to comment.