Skip to content

Instantly share code, notes, and snippets.

@KaoruNishikawa
Created August 30, 2024 18:01
Show Gist options
  • Select an option

  • Save KaoruNishikawa/c89a7f4fe3fb2f71a09c6eb09f93de69 to your computer and use it in GitHub Desktop.

Select an option

Save KaoruNishikawa/c89a7f4fe3fb2f71a09c6eb09f93de69 to your computer and use it in GitHub Desktop.
Shape annotation in Python
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"from typing import TypeVar, Generic, Protocol, runtime_checkable\n",
"\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import torch\n",
"\n",
"DType = TypeVar('DType')\n",
"Shape = TypeVar('Shape')\n",
"\n",
"@runtime_checkable\n",
"class NDArray(Generic[DType, Shape], Protocol):\n",
" def __len__(self) -> int: ...\n",
" def __abs__(self) -> \"NDArray[DType, Shape]\": ...\n",
"\n",
"a: NDArray[float, (3, 2)] = np.ndarray((3, 2), dtype=float)\n",
"b: NDArray[float, (3, 2)] = tf.constant([[1., 2.], [3., 4.], [5., 6.]])\n",
"c: NDArray[float, (3, 2)] = torch.tensor([[1., 2.], [3., 4.], [5., 6.]])\n",
"d: NDArray[float, (3, 2)] = [[1., 2.], [3., 4.], [5., 6.]]\n",
"\n",
"assert isinstance(a, np.ndarray)\n",
"assert isinstance(a, NDArray)\n",
"\n",
"assert isinstance(b, tf.Tensor)\n",
"assert isinstance(b, NDArray)\n",
"\n",
"assert isinstance(c, torch.Tensor)\n",
"assert isinstance(c, NDArray)\n",
"\n",
"assert isinstance(d, list)\n",
"assert not isinstance(d, NDArray)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from typing import TypeVar, TypeVarTuple, Generic, Protocol, runtime_checkable\n",
"\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import torch\n",
"\n",
"DType = TypeVar('DType')\n",
"Shape = TypeVarTuple('Shape')\n",
"\n",
"@runtime_checkable\n",
"class NDArray(Generic[DType, *Shape], Protocol):\n",
" def __len__(self) -> int: ...\n",
" def __abs__(self) -> \"NDArray[DType, *Shape]\": ...\n",
"\n",
"a: NDArray[float, 3, 2] = np.ndarray((3, 2), dtype=float)\n",
"b: NDArray[float, 3, 2] = tf.constant([[1., 2.], [3., 4.], [5., 6.]])\n",
"c: NDArray[float, 3, 2] = torch.tensor([[1., 2.], [3., 4.], [5., 6.]])\n",
"d: NDArray[float, 3, 2] = [[1., 2.], [3., 4.], [5., 6.]]\n",
"\n",
"assert isinstance(a, np.ndarray)\n",
"assert isinstance(a, NDArray)\n",
"\n",
"assert isinstance(b, tf.Tensor)\n",
"assert isinstance(b, NDArray)\n",
"\n",
"assert isinstance(c, torch.Tensor)\n",
"assert isinstance(c, NDArray)\n",
"\n",
"assert isinstance(d, list)\n",
"assert not isinstance(d, NDArray)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment