mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 07:31:38 +00:00 
			
		
		
		
	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:
		
							parent
							
								
									22415ad625
								
							
						
					
					
						commit
						0705ec8a14
					
				
					 4 changed files with 42 additions and 0 deletions
				
			
		| 
						 | 
					@ -1658,6 +1658,20 @@ Introspection helpers
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   .. versionadded:: 3.8
 | 
					   .. versionadded:: 3.8
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.. function:: 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
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					   .. versionadded:: 3.10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
.. class:: ForwardRef
 | 
					.. class:: ForwardRef
 | 
				
			||||||
 | 
					
 | 
				
			||||||
   A class used for internal typing representation of string forward references.
 | 
					   A class used for internal typing representation of string forward references.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,7 @@
 | 
				
			||||||
from typing import cast, runtime_checkable
 | 
					from typing import cast, runtime_checkable
 | 
				
			||||||
from typing import get_type_hints
 | 
					from typing import get_type_hints
 | 
				
			||||||
from typing import get_origin, get_args
 | 
					from typing import get_origin, get_args
 | 
				
			||||||
 | 
					from typing import is_typeddict
 | 
				
			||||||
from typing import no_type_check, no_type_check_decorator
 | 
					from typing import no_type_check, no_type_check_decorator
 | 
				
			||||||
from typing import Type
 | 
					from typing import Type
 | 
				
			||||||
from typing import NewType
 | 
					from typing import NewType
 | 
				
			||||||
| 
						 | 
					@ -3900,6 +3901,12 @@ class Cat(Animal):
 | 
				
			||||||
            'voice': str,
 | 
					            'voice': str,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def test_is_typeddict(self):
 | 
				
			||||||
 | 
					        assert is_typeddict(Point2D) is True
 | 
				
			||||||
 | 
					        assert is_typeddict(Union[str, int]) is False
 | 
				
			||||||
 | 
					        # classes, not instances
 | 
				
			||||||
 | 
					        assert is_typeddict(Point2D()) is False
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class IOTests(BaseTestCase):
 | 
					class IOTests(BaseTestCase):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -103,6 +103,7 @@
 | 
				
			||||||
    'get_args',
 | 
					    'get_args',
 | 
				
			||||||
    'get_origin',
 | 
					    'get_origin',
 | 
				
			||||||
    'get_type_hints',
 | 
					    'get_type_hints',
 | 
				
			||||||
 | 
					    'is_typeddict',
 | 
				
			||||||
    'NewType',
 | 
					    'NewType',
 | 
				
			||||||
    'no_type_check',
 | 
					    'no_type_check',
 | 
				
			||||||
    'no_type_check_decorator',
 | 
					    'no_type_check_decorator',
 | 
				
			||||||
| 
						 | 
					@ -1479,6 +1480,20 @@ def get_args(tp):
 | 
				
			||||||
    return ()
 | 
					    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):
 | 
					def no_type_check(arg):
 | 
				
			||||||
    """Decorator to indicate that annotations are not type hints.
 | 
					    """Decorator to indicate that annotations are not type hints.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					Add is_typeddict function to typing.py to check if a type is a TypedDict
 | 
				
			||||||
 | 
					class
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Previously there was no way to check that without using private API. See the
 | 
				
			||||||
 | 
					`relevant issue in python/typing
 | 
				
			||||||
 | 
					<https://github.com/python/typing/issues/751>`
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue