Content · Programming · Python

Python typing.TypeIs: Precise Type Narrowing

TypeIs narrows values on both true and false branches when its predicate accurately describes a subtype.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

TypeIs narrows values on both true and false branches when its predicate accurately describes a subtype.

What It Does and Why It Matters


TypeIs narrows values on both true and false branches when its predicate accurately describes a subtype. A dishonest or poorly implemented predicate can make static assumptions unsafe.

Version requirement: TypeIs was added in Python 3.13 and requires a compatible type checker for useful narrowing.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

from typing import TypeIs

def is_text(value: object) -> TypeIs[str]:
    return isinstance(value, str)

def describe(value: str | int) -> str:
    if is_text(value):
        return value.upper()
    return str(value + 1)
print(describe('ready'))

How the Code Works


  • The imports make these standard-library tools available: typing.
  • The definition of is_text, describe creates reusable behavior; its body runs only when called or instantiated as appropriate.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • The predicate returns a runtime Boolean. The annotation tells a static checker what type it may assume within the checked branch.

Expected output:

READY

For the demonstrated inputs, Python displays 'READY'. This result follows from the operation described above; change one input at a time to understand which rule matters.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This helps maintain larger projects where functions and data shapes cross module boundaries. Check the code with a static analyzer, and still validate data received from files, users or remote APIs. Annotation constructs do not normally enforce types automatically at runtime.

Practice Exercise


Use a TypeIs predicate to distinguish strings from other objects.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Claiming TypeIs[str] while returning True for non-string values.
  • Assuming a type checker has checked values received dynamically from an untrusted source.

Key Takeaways


  • TypeIs narrows values on both true and false branches when its predicate accurately describes a subtype.
  • Use a TypeIs predicate to distinguish strings from other objects.
  • Version requirement: TypeIs was added in Python 3.13 and requires a compatible type checker for useful narrowing.
Share this resourceSend this page to someone who may find it useful.