Skip to content

Commit 7886503

Browse files
hauntsaninjajhance
authored andcommitted
Fix crash when indexing TypedDict with empty key (#15392)
1 parent 5e6e488 commit 7886503

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

mypy/messages.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2989,8 +2989,9 @@ def _real_quick_ratio(a: str, b: str) -> float:
29892989

29902990

29912991
def best_matches(current: str, options: Collection[str], n: int) -> list[str]:
2992+
if not current:
2993+
return []
29922994
# narrow down options cheaply
2993-
assert current
29942995
options = [o for o in options if _real_quick_ratio(current, o) > 0.75]
29952996
if len(options) >= 50:
29962997
options = [o for o in options if abs(len(o) - len(current)) <= 1]

test-data/unit/check-typeddict.test

+12
Original file line numberDiff line numberDiff line change
@@ -2873,3 +2873,15 @@ foo({"foo": {"e": "foo"}}) # E: Type of TypedDict is ambiguous, none of ("A", "
28732873
# E: Argument 1 to "foo" has incompatible type "Dict[str, Dict[str, str]]"; expected "Union[A, B]"
28742874
[builtins fixtures/dict.pyi]
28752875
[typing fixtures/typing-typeddict.pyi]
2876+
2877+
[case testTypedDictMissingEmptyKey]
2878+
from typing_extensions import TypedDict
2879+
2880+
class A(TypedDict):
2881+
my_attr_1: str
2882+
my_attr_2: int
2883+
2884+
d: A
2885+
d[''] # E: TypedDict "A" has no key ""
2886+
[builtins fixtures/dict.pyi]
2887+
[typing fixtures/typing-typeddict.pyi]

0 commit comments

Comments
 (0)