bpo-41792: Add is_typeddict function to typing.py (GH-22254)

Closes issue41792.

Also closes https://github.com/python/typing/issues/751.
This commit is contained in:
Patrick Reader 2020-09-16 05:58:32 +01:00 committed by GitHub
parent 22415ad625
commit 0705ec8a14
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 0 deletions

View file

@ -103,6 +103,7 @@ __all__ = [
'get_args',
'get_origin',
'get_type_hints',
'is_typeddict',
'NewType',
'no_type_check',
'no_type_check_decorator',
@ -1479,6 +1480,20 @@ def get_args(tp):
return ()
def is_typeddict(tp):
"""Check if an annotation is a TypedDict class
For example::
class Film(TypedDict):
title: str
year: int
is_typeddict(Film) # => True
is_typeddict(Union[list, str]) # => False
"""
return isinstance(tp, _TypedDictMeta)
def no_type_check(arg):
"""Decorator to indicate that annotations are not type hints.