Skip to content

Binary operators narrow only their left operands #2718

@dscorbett

Description

@dscorbett

Summary

Binary operators narrow only their left operands. Example:

$ cat >bug.py <<'# EOF'
from typing import Literal
def _(t: Literal[True], tn: Literal[True] | None):
    if tn is t:
        +tn
    if t is tn:
        +tn
# EOF

$ ty check bug.py                 
error[unsupported-operator]: Unary operator `+` is not supported for object of type `Literal[True] | None`
 --> bug.py:6:9
  |
4 |         +tn
5 |     if t is tn:
6 |         +tn
  |         ^^^
  |
info: rule `unsupported-operator` is enabled by default

Found 1 diagnostic

Version

ty 0.0.15 (eb43dff 2026-02-04)

Metadata

Metadata

Assignees

Labels

narrowingrelated to flow-sensitive type narrowing

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions