Skip to content

Instantly share code, notes, and snippets.

@2tony2
Created May 22, 2024 07:17
Show Gist options
  • Select an option

  • Save 2tony2/7ac91966bbabf88e36384e7a04c8e20d to your computer and use it in GitHub Desktop.

Select an option

Save 2tony2/7ac91966bbabf88e36384e7a04c8e20d to your computer and use it in GitHub Desktop.
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