Created
May 22, 2024 07:17
-
-
Save 2tony2/7ac91966bbabf88e36384e7a04c8e20d to your computer and use it in GitHub Desktop.
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
| from typing import Annotated | |
| from typing_extensions import Annotated | |
| # Define an annotated type with metadata | |
| Age = Annotated[int, "Must be a non-negative integer"] | |
| def check_age(age: Age): | |
| if age < 0: | |
| raise ValueError("Age must be a non-negative integer") | |
| print(f"Age is valid: {age}") | |
| # Usage examples | |
| try: | |
| check_age(25) # Valid | |
| check_age(-5) # Invalid, will raise ValueError | |
| except ValueError as e: | |
| print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment