[red-knot] Narrowing on in tuple[...] and in str#17059
[red-knot] Narrowing on in tuple[...] and in str#17059carljm merged 12 commits intoastral-sh:mainfrom
in tuple[...] and in str#17059Conversation
|
in tuple[...]in tuple[...] and in str
|
Okay, i realise this is not right from typing import Literal
def _(x: Literal["a", "b", "c", "d"]):
if x in "abc":
reveal_type(x) # revealed: Literal["a", "b", "c"]
else:
reveal_type(x) # revealed: Literal["d"]The true type is Literal["a", "b", "c", "ab", "ac", "bc", "abc"]. So i should just infer as str right? Edit: In fact im wrong in this instance, but if the argument x was str then i think this is the case? |
|
Im a bit unsure about if we should support other types in str. For example we have these right now: def _(x: Literal["a"]) -> None:
if x in "abc":
reveal_type(x) # revealed: Literal["a"]def _(x: Literal["a", "b"]) -> None:
if x in "abc":
reveal_type(x) # revealed: Literal["a", "b"]def _(x: str) -> None:
if x in "abc":
reveal_type(x) # revealed: strbut we also have: def _(x: object) -> None:
if x in "abc":
reveal_type(x) # revealed: Literal["a", "b", "c"]*we also get [unsupported-operator] error here My thinking for all StringLiteral and Union containing only StringLiteral, we can narrow the type, but otherwise we can only infer as str? How does this sounds. |
carljm
left a comment
There was a problem hiding this comment.
Haven't reviewed the code yet, just the tests, but there are some things we'll have to be a bit more conservative about here.
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals/in.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals/in.md
Outdated
Show resolved
Hide resolved
crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals/in.md
Outdated
Show resolved
Hide resolved
|
Okay thank you, I'll look over this again |
carljm
left a comment
There was a problem hiding this comment.
The behavior here looks good! I think we can simplify the implementation and make it a bit more general.
|
Thank you |
* main: [red-knot] Add property tests for callable types (#17006) [red-knot] Disjointness for callable types (#17094) [red-knot] Flatten `Type::Callable` into four `Type` variants (#17126) mdtest.py: do a full mdtest run immediately when the script is executed (#17128) [red-knot] Fix callable subtyping for standard parameters (#17125) [red-knot] Fix more `redundant-cast` false positives (#17119) Sync vendored typeshed stubs (#17106) [red-knot] support Any as a class in typeshed (#17107) Visit `Identifier` node as part of the `SourceOrderVisitor` (#17110) [red-knot] Don't infer Todo for quite so many tuple type expressions (#17116) CI: Run pre-commit on depot machine (#17120) Error instead of `panic!` when running Ruff from a deleted directory (#16903) (#17054) Control flow graph: setup (#17064) [red-knot] Playground improvements (#17109) [red-knot] IDE crate (#17045) Update dependency vite to v6.2.4 (#17104) [red-knot] Add redundant-cast error (#17100) [red-knot] Narrowing on `in tuple[...]` and `in str` (#17059)
Summary
Part of #13694
Seems there a bit more to cover regarding
inand other types, but i can cover them in different PRsTest Plan
Add
in.mdfile in narrowing conditionals folder