mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
![]() ## Summary Part of [#117](https://github.com/astral-sh/ty/issues/117). `TypeIs[]` is a special form that allows users to define their own narrowing functions. Despite the syntax, `TypeIs` is not a generic and, on its own, it is meaningless as a type. [Officially](https://typing.python.org/en/latest/spec/narrowing.html#typeis), a function annotated as returning a `TypeIs[T]` is a <i>type narrowing function</i>, where `T` is called the <i>`TypeIs` return type</i>. A `TypeIs[T]` may or may not be bound to a symbol. Only bound types have narrowing effect: ```python def f(v: object = object()) -> TypeIs[int]: ... a: str = returns_str() if reveal_type(f()): # Unbound: TypeIs[int] reveal_type(a) # str if reveal_type(f(a)): # Bound: TypeIs[a, int] reveal_type(a) # str & int ``` Delayed usages of a bound type has no effect, however: ```python b = f(a) if b: reveal_type(a) # str ``` A `TypeIs[T]` type: * Is fully static when `T` is fully static. * Is a singleton/single-valued when it is bound. * Has exactly two runtime inhabitants when it is unbound: `True` and `False`. In other words, an unbound type have ambiguous truthiness. It is possible to infer more precise truthiness for bound types; however, that is not part of this change. `TypeIs[T]` is a subtype of or otherwise assignable to `bool`. `TypeIs` is invariant with respect to the `TypeIs` return type: `TypeIs[int]` is neither a subtype nor a supertype of `TypeIs[bool]`. When ty sees a function marked as returning `TypeIs[T]`, its `return`s will be checked against `bool` instead. ty will also report such functions if they don't accept a positional argument. Addtionally, a type narrowing function call with no positional arguments (e.g., `f()` in the example above) will be considered invalid. ## Test Plan Markdown tests. --------- Co-authored-by: Carl Meyer <carl@astral.sh> |
||
---|---|---|
.. | ||
annotations | ||
assignment | ||
binary | ||
boolean | ||
boundness_declaredness | ||
call | ||
class | ||
comparison | ||
comprehensions | ||
conditional | ||
declaration | ||
diagnostics | ||
directives | ||
doc | ||
exception | ||
expression | ||
function | ||
generics | ||
ide_support | ||
import | ||
literal | ||
loops | ||
narrow | ||
regression | ||
scopes | ||
shadowing | ||
snapshots | ||
stubs | ||
subscript | ||
suppressions | ||
type_compendium | ||
type_of | ||
type_properties | ||
type_qualifiers | ||
unary | ||
with | ||
.mdformat.toml | ||
attributes.md | ||
cycle.md | ||
dataclass_transform.md | ||
dataclasses.md | ||
decorators.md | ||
del.md | ||
descriptor_protocol.md | ||
final.md | ||
intersection_types.md | ||
invalid_syntax.md | ||
known_constants.md | ||
mdtest_config.md | ||
mdtest_custom_typeshed.md | ||
metaclass.md | ||
mro.md | ||
named_tuple.md | ||
overloads.md | ||
pep695_type_aliases.md | ||
properties.md | ||
protocols.md | ||
slots.md | ||
statically_known_branches.md | ||
sys_platform.md | ||
sys_version_info.md | ||
terminal_statements.md | ||
type_api.md | ||
typed_dict.md | ||
union_types.md | ||
unpacking.md | ||
unreachable.md |