pythonuser-defined-types

Python equivalent for typedef


What is the python way to define a (non-class) type like:

typedef Dict[Union[int, str], Set[str]] RecordType

Solution

  • This would simply do it?

    from typing import Dict, Union, Set
    
    RecordType = Dict[Union[int, str], Set[str]]
    
    
    def my_func(rec: RecordType):
        pass
    
    
    my_func({1: {'2'}})
    my_func({1: {2}})
    

    This code will generate a warning from your IDE on the second call to my_func, but not on the first. As @sahasrara62 indicated, more here https://docs.python.org/3/library/stdtypes.html#types-genericalias

    Since Python 3.9, the preferred syntax would be:

    from typing import Union
    
    RecordType = dict[Union[int, str], set[str]]
    

    The built-in types can be used directly for type hints and the added imports are no longer required.

    Since Python 3.10, the preferred syntax is

    RecordType = dict[int | str, set[str]]
    

    The | operator is a simpler way to create a union of types, and the import of Union is no longer required.

    Since Python 3.12, the preferred syntax is

    type RecordType = dict[int | str, set[str]]
    

    The type keyword explicitly indicates that this is a type alias.