Created
February 15, 2026 13:13
-
-
Save mypy-play/268435dc36b943e43b5700e79b39babc to your computer and use it in GitHub Desktop.
Shared via mypy Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import typing | |
| import types | |
| from typing import Any | |
| def _validate_type_hint(parameter_type: type[Any]) -> tuple[str, Any] | None: | |
| generic_args: tuple[type[Any], ...] = typing.get_args(parameter_type) | |
| match generic_args: | |
| case () | (_,): | |
| # This is just misuse of this function. A Union type cannot have less than two type args. | |
| raise ValueError(f'UnionType cannot have only {len(generic_args)} type argument(s).') | |
| case (_, types.NoneType) | (types.NoneType, _): | |
| # Proper path | |
| pass | |
| case (first, second): | |
| return 'Multi-type parameter must have only one non-None type', f'"{first.__name__}" and "{second.__name__}""' | |
| case _: | |
| return 'Must be either "<type>", "Sequence[<type>], or <type> | None', f'"{parameter_type}" is not valid' | |
| return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment