-
Notifications
You must be signed in to change notification settings - Fork 281
Labels
help wantedLarger than "good first issue", but still well-defined and ready for someone to pick upLarger than "good first issue", but still well-defined and ready for someone to pick uptypechecking
Description
Describe the Bug
I just ran into python/mypy#20602 and noticed that pyrefly also doesn't type narrow in some of these cases.
from typing import Literal, reveal_type
LitA = Literal["a"]
lit_dict: dict[LitA, int] = {"a": 1}
lit_tuple: tuple[LitA] = ("a",)
lit_list: list[LitA] = ["a"]
lit_set: set[LitA] = {"a"}
lit_frozenset: frozenset[LitA] = frozenset(("a",))
def no_narrowing(key: str):
if key in lit_dict:
reveal_type(key) # str
lit_dict[key] # Argument `str` is not assignable to parameter `key` with type `Literal['a']`
if key in lit_tuple:
reveal_type(key) # str
if key in lit_list:
reveal_type(key) # str
if key in lit_set:
reveal_type(key) # str
if key in lit_frozenset:
reveal_type(key) # str
if key in frozenset(("a",)):
reveal_type(key) # str
def narrowing(key: str):
if key in ("a",):
reveal_type(key) # Literal['a']
if key in ["a"]:
reveal_type(key) # Literal['a']
if key in {"a"}:
reveal_type(key) # Literal['a']
if key in {"a": 1}:
reveal_type(key) # Literal['a']So it seems to narrow when checking against inline collections, but not if we check against typed variables.
Sandbox Link
No response
(Only applicable for extension issues) IDE Information
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
help wantedLarger than "good first issue", but still well-defined and ready for someone to pick upLarger than "good first issue", but still well-defined and ready for someone to pick uptypechecking