Skip to content

Commit

Permalink
Fix crash involving explicit any flag and Required (#12039)
Browse files Browse the repository at this point in the history
Co-authored-by: Mehdi Drissi <[email protected]>
  • Loading branch information
hmc-cs-mdrissi and Mehdi Drissi authored Jan 26, 2022
1 parent 99f4d5a commit d5d077b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ def __repr__(self) -> str:
else:
return "NotRequired[{}]".format(self.item)

def accept(self, visitor: 'TypeVisitor[T]') -> T:
return self.item.accept(visitor)


class ProperType(Type):
"""Not a type alias.
Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -2208,6 +2208,12 @@ class Movie(TypedDict, total=False):
year: int
[typing fixtures/typing-typeddict.pyi]

[case testRequiredExplicitAny]
# flags: --disallow-any-explicit
from typing import TypedDict
from typing import Required
Foo = TypedDict("Foo", {"a.x": Required[int]})
[typing fixtures/typing-typeddict.pyi]

-- NotRequired[]

Expand Down Expand Up @@ -2271,6 +2277,13 @@ class Movie(TypedDict):
year: int
[typing fixtures/typing-typeddict.pyi]

[case testNotRequiredExplicitAny]
# flags: --disallow-any-explicit
from typing import TypedDict
from typing import NotRequired
Foo = TypedDict("Foo", {"a.x": NotRequired[int]})
[typing fixtures/typing-typeddict.pyi]

-- Union dunders

[case testTypedDictUnionGetItem]
Expand Down
3 changes: 2 additions & 1 deletion test-data/unit/fixtures/typing-typeddict.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ class Iterator(Iterable[T_co], Protocol):
def __next__(self) -> T_co: pass

class Sequence(Iterable[T_co]):
def __getitem__(self, n: Any) -> T_co: pass
# misc is for explicit Any.
def __getitem__(self, n: Any) -> T_co: pass # type: ignore[misc]

class Mapping(Iterable[T], Generic[T, T_co], metaclass=ABCMeta):
def __getitem__(self, key: T) -> T_co: pass
Expand Down

0 comments on commit d5d077b

Please sign in to comment.