{"id":6932,"library":"types-tensorflow","title":"TensorFlow Type Stubs","description":"types-tensorflow is a type stub package providing static type annotations for the TensorFlow library. Maintained as part of the `typeshed` project, it enables type checkers like MyPy and Pyright to analyze code using TensorFlow, improving code quality and developer tooling. This package aims to provide accurate annotations for specific TensorFlow minor versions (e.g., `~=2.18.0`). It follows a frequent release cadence, with minor and patch versions released regularly to keep pace with TensorFlow updates.","status":"active","version":"2.18.0.20260408","language":"en","source_language":"en","source_url":"https://github.com/python/typeshed","tags":["typing","stubs","tensorflow","typeshed","type checking"],"install":[{"cmd":"pip install types-tensorflow","lang":"bash","label":"Install types-tensorflow"},{"cmd":"pip install tensorflow","lang":"bash","label":"Install TensorFlow (required runtime dependency)"}],"dependencies":[{"reason":"Provides the runtime library for which these are type stubs. Specific minor versions are targeted (e.g., ~=2.18.0).","package":"tensorflow"},{"reason":"Requires Python version >=3.10.","package":"python"}],"imports":[{"note":"types-tensorflow provides stubs for the `tensorflow` library; users directly import `tensorflow`, and type checkers then utilize the installed `types-tensorflow` package for static analysis. There is no direct import for `types-tensorflow` itself.","symbol":"tf","correct":"import tensorflow as tf"}],"quickstart":{"code":"import tensorflow as tf\n\ndef create_and_add_constants(val1: int, val2: int) -> tf.Tensor:\n    \"\"\"Creates two constant tensors from ints and adds them.\"\"\"\n    tensor1 = tf.constant(val1, dtype=tf.int32)\n    tensor2 = tf.constant(val2, dtype=tf.int32)\n    return tf.add(tensor1, tensor2)\n\ndef concatenate_tensors(tensors: list[tf.Tensor]) -> tf.Tensor:\n    \"\"\"Concatenates a list of tensors along axis 0.\"\"\"\n    return tf.concat(tensors, axis=0)\n\nif __name__ == \"__main__\":\n    # Example 1: Basic addition with type hints\n    sum_result = create_and_add_constants(10, 20)\n    print(f\"Sum result (numpy value): {sum_result.numpy()}\")\n\n    # Example 2: Concatenation with explicit type hints\n    tensor_a = tf.constant([1.0, 2.0], dtype=tf.float32)\n    tensor_b = tf.constant([3.0, 4.0], dtype=tf.float32)\n    combined_tensors = concatenate_tensors([tensor_a, tensor_b])\n    print(f\"Concatenated tensors (numpy value): {combined_tensors.numpy()}\")\n\n    # To run type checking:\n    # 1. Save this code as `tf_example.py`.\n    # 2. Install a type checker (e.g., `pip install mypy`).\n    # 3. Run `mypy tf_example.py` in your terminal.\n    #    Expected output: Success (no errors) if code is correctly typed.","lang":"python","description":"This quickstart demonstrates how to write type-hinted TensorFlow code that `types-tensorflow` can then be used to check with a static type checker like `mypy` or `pyright`. It shows basic tensor creation and manipulation with Python type annotations."},"warnings":[{"fix":"Consult TensorFlow's official documentation for un-stubbed parts. Contribute to `typeshed` to help complete the stubs.","message":"The `types-tensorflow` package is marked as 'partial'. Type checkers, such as Pyright, often assume that if type stubs are present, they are authoritative for that module. This can lead to a lack of autocompletion or type information for parts of the TensorFlow API that are not yet covered by the stubs, rather than falling back to runtime introspection.","severity":"gotcha","affected_versions":"All versions, as it's an inherent characteristic of partial stubs."},{"fix":"Always install `types-tensorflow` matching the minor version of your `tensorflow` installation, e.g., if you have `tensorflow==2.18.x`, install `types-tensorflow==2.18.x.YYYYMMDD`.","message":"The `types-tensorflow` package aims to provide accurate annotations for specific TensorFlow minor versions (e.g., `tensorflow~=2.18.0`). Mismatches between the installed `types-tensorflow` version and your actual `tensorflow` runtime version can lead to incorrect or misleading type checking results, including false positives or missed errors.","severity":"breaking","affected_versions":"All versions"},{"fix":"Be aware that some advanced or dynamically generated TensorFlow patterns might still require `type: ignore` comments or might not be perfectly covered by stubs. Consult TensorFlow documentation if types are unclear.","message":"TensorFlow's API relies heavily on dynamic method definitions, decorator magic, and internal re-exports (e.g., classes defined internally but exposed publicly via different paths). This complexity can make generating perfect type stubs challenging, potentially leading to situations where type checkers might produce false positives or fail to catch actual errors due to the discrepancies between the stub's static view and TensorFlow's runtime behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always verify Python compatibility with both the `types-tensorflow` package and your `tensorflow` installation. Upgrade your Python version if necessary.","message":"While `types-tensorflow` explicitly requires Python `>=3.10`, the underlying `tensorflow` library also has its own Python version compatibility requirements. For instance, TensorFlow 2.21 removed support for Python 3.9. Ensure that your Python environment satisfies the requirements of *both* `types-tensorflow` and the specific version of `tensorflow` you intend to use.","severity":"breaking","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-15T00:00:00.000Z","next_check":"2026-07-14T00:00:00.000Z","problems":[]}