I want to propose adding a keyword module argument to TypeAliasType, which allows one to override the __module__ otherwise inferred by the constructor via the current interpreter frame. This is analogous to the module argument already supported by collections.namedtuple and enum.Enum.
The motivation is likewise getting __module__ right in cases like exec, constructing TypeAliasType objects from the C API, etc. While I’d be surprised if many people were depending on TypeAliasType objects’ __module__ for unpickling, it’s still helpful for use-cases like runtime-introspection-based stub generation or docgen.
Concretely, I’d propose making the interface
TypeAliasType(name, value, *, type_params=(), qualname=None, module=None)
With module omitted or explicitly passed as None, we’d infer __module__ exactly as today. With any other object passed, we’d pass that through directly to __module__ (regardless of whether it’s str, matching namedtuple and Enum behavior).