Skip to content

Commit

Permalink
pyi parser: allow total=False for typing_extensions.TypedDict.
Browse files Browse the repository at this point in the history
My previous change did not actually fix the pytype bug I was trying to fix
because typeshed uses typing_extensions.TypedDict rather than typing.TypedDict.

For #613.

PiperOrigin-RevId: 321412207
  • Loading branch information
rchen152 committed Jul 20, 2020
1 parent 2432f59 commit c59a6a6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion pytype/pyi/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
# Typing members that represent sets of types.
_TYPING_SETS = ("typing.Intersection", "typing.Optional", "typing.Union")

_TYPED_DICT_ALIASES = (
"typing.TypedDict",
parser_constants.EXTERNAL_NAME_PREFIX + "typing_extensions.TypedDict")


_Params = collections.namedtuple("_", ["required",
"starargs", "starstarargs",
Expand Down Expand Up @@ -1113,7 +1117,7 @@ def new_class(self, decorators, class_name, parent_args, defs):
raise ParseError("Unexpected classdef kwarg %r" % keyword)
elif keyword == "total" and not any(
isinstance(parent, pytd.NamedType) and
parent.name == "typing.TypedDict" for parent in parents):
parent.name in _TYPED_DICT_ALIASES for parent in parents):
raise ParseError(
"'total' allowed as classdef kwarg only for TypedDict subclasses")
if keyword == "metaclass":
Expand Down
13 changes: 13 additions & 0 deletions pytype/pyi/parser_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,19 @@ class Foo(TypedDict): ...
class Foo(object, total=False): ...
""", 1, "'total' allowed as classdef kwarg only for TypedDict subclasses")

def test_typing_extensions_typed_dict(self):
self.check("""
from typing_extensions import TypedDict
class Foo(TypedDict, total=False): ...
""", """
import typing_extensions
from typing_extensions import TypedDict
class Foo(typing_extensions.TypedDict): ...
""")

def test_multiple_classdef_kwargs(self):
self.check("""
from typing import TypedDict
Expand Down

0 comments on commit c59a6a6

Please sign in to comment.