crates/ty_python_semantic/resources/mdtest/declaration/error.md
x = 1
x: str # error: [invalid-declaration] "Cannot declare type `str` for inferred type `Literal[1]`"
def _(flag: bool):
if flag:
x: str
else:
x: int
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: `str` and `int`"
def _(flag1: bool, flag2: bool):
if flag1:
x: str
elif flag2:
x: int
# Here, the declared type for `x` is `int | str | Unknown`.
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: `str` and `int`"
def _(flag1: bool, flag2: bool, flag3: bool, flag4: bool):
if flag1:
x: str
elif flag2:
x: int
elif flag3:
x: int
elif flag4:
x: str
else:
x: bytes
x = "a" # error: [conflicting-declarations] "Conflicting declared types for `x`: `str`, `int` and `bytes`"
def _(flag: bool):
if flag:
x: str
else:
x: int
# error: [conflicting-declarations]
# error: [invalid-assignment]
x = b"foo"
Currently, we avoid raising the conflicting-declarations for the following cases:
def _(flag: bool):
if flag:
x: int
x = 1
Refer to https://github.com/astral-sh/ruff/issues/13966
def _():
try:
x: int = 1
except:
x = 2
x = 3