Skip to content

Instantly share code, notes, and snippets.

@MartinThoma
Created October 2, 2021 10:06
Show Gist options
  • Save MartinThoma/b903dd4aefc634d2839dafb65de23645 to your computer and use it in GitHub Desktop.
Save MartinThoma/b903dd4aefc634d2839dafb65de23645 to your computer and use it in GitHub Desktop.
from typing import TypedDict, TypeGuard
class User(TypedDict):
name: str
email: str
def is_user(data: dict) -> TypeGuard[User]:
return "name" in data and "email" in data
if __name__ == "__main__":
user = {"name": "Martin", "email": "[email protected]"}
user = {}
if is_user(user):
print(user["x"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment