Skip to content

Instantly share code, notes, and snippets.

@thanakijwanavit
Last active March 17, 2021 15:53
Show Gist options
  • Save thanakijwanavit/8e21d168f81671005a752b9070929d57 to your computer and use it in GitHub Desktop.
Save thanakijwanavit/8e21d168f81671005a752b9070929d57 to your computer and use it in GitHub Desktop.
type checking example
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "type checking example",
"private_outputs": true,
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyOMO867/GR6aoN2FM9fhvcr",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/thanakijwanavit/8e21d168f81671005a752b9070929d57/type-checking-example.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "sjUclrH83Jgr"
},
"source": [
"!pip install beartype"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "x1S-o2Wz205T"
},
"source": [
"from beartype import beartype\n",
"from typing import Optional, List\n",
"from enum import Enum, auto\n",
"\n",
"class Animal(Enum):\n",
" fish=1\n",
" cat=2\n",
" dog = auto()\n",
" \n",
"@beartype\n",
"def testBeartype(i:int, s:str, a:Optional[Animal]=None)-> str:\n",
" print(s,i, a)\n",
" return a.name\n",
"testBeartype(5,'4', Animal['fish'])\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "lY7gt2PS4fKI"
},
"source": [
"print(Animal['dog'])\n",
"print(Animal(3))\n"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "t_l26wEg3ZMq"
},
"source": [
"## success Example"
]
},
{
"cell_type": "code",
"metadata": {
"id": "N2Orr3gY3rD6"
},
"source": [
"testBeartype(5,'4', Animal['fish'])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "cRMhfpDO3CZ5"
},
"source": [
"testBeartype('5','4', Animal['fish'])"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "e1MiXJ-j3r_g"
},
"source": [
""
],
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment