Files
Alex Waygood 99d97bd72f [ty] Tighten up a few edge cases in Concatenate type-expression parsing (#24172)
## Summary

There were:
- a couple of places where we emitted two diagnostics where one would do
- a few places where we fell back to overly precise inferred types in
error cases (inferring types like `(Unknown, /) -> None` where `(...) ->
None` would arguably be better)
- a few places where we correctly emitted a diagnostic, but didn't have
an ideal error message

This PR just makes some minor cleanups in that area

## Test Plan

new mdtests
2026-04-09 09:43:50 +01:00

986 B

Optional

Annotation

typing.Optional is equivalent to using the type with a None in a Union.

from typing import Optional

a: Optional[int]
a1: Optional[bool]
a2: Optional[Optional[bool]]
a3: Optional[None]

def f():
    # revealed: int | None
    reveal_type(a)
    # revealed: bool | None
    reveal_type(a1)
    # revealed: bool | None
    reveal_type(a2)
    # revealed: None
    reveal_type(a3)

Assignment

from typing import Optional

a: Optional[int] = 1
a = None
# error: [invalid-assignment] "Object of type `Literal[""]` is not assignable to `int | None`"
a = ""

Typing Extensions

from typing_extensions import Optional

a: Optional[int]

def f():
    # revealed: int | None
    reveal_type(a)

Invalid

from typing import Optional

# error: [invalid-type-form] "`typing.Optional` requires exactly one argument when used in a parameter annotation"
def f(x: Optional) -> None:
    reveal_type(x)  # revealed: Unknown