@@ -2452,9 +2452,9 @@ f(memoryview(b"asdf")) # E: Argument 1 to "f" has incompatible type "memoryview
24522452[builtins fixtures/primitives.pyi]
24532453
24542454[case testDisallowStrIteration]
2455- # flags: --disallow-str-iteration
2455+ # flags: --disallow-str-iteration --warn-unreachable
24562456from abc import abstractmethod
2457- from typing import Collection, Container, Iterable, Mapping, Protocol, Sequence, SupportsIndex, TypeVar, Union
2457+ from typing import Collection, Container, Hashable, Iterable, Mapping, Protocol, Sequence, SupportsIndex, TypeGuard, TypeIs, TypeVar, Union, runtime_checkable
24582458
24592459def takes_str(x: str):
24602460 for ch in x: # E: Iterating over "str" is disallowed # N: This is because --disallow-str-iteration is enabled
@@ -2476,7 +2476,7 @@ seq: Sequence[str] = s # E: Incompatible types in assignment (expression has ty
24762476iterable: Iterable[str] = s # E: Incompatible types in assignment (expression has type "str", variable has type "Iterable[str]")
24772477collection: Collection[str] = s # E: Incompatible types in assignment (expression has type "str", variable has type "Collection[str]")
24782478
2479- def takes_maybe_seq(x: " str | Sequence[int]" ) -> None:
2479+ def takes_maybe_seq(x: Union[ str, Sequence[int]] ) -> None:
24802480 for ch in x: # E: Iterating over "str | Sequence[int]" is disallowed # N: This is because --disallow-str-iteration is enabled
24812481 reveal_type(ch) # N: Revealed type is "builtins.str | builtins.int"
24822482
@@ -2495,6 +2495,7 @@ def takes_str_subclass(x: StrSubclass):
24952495 for ch in x: # E: Iterating over "StrSubclass" is disallowed # N: This is because --disallow-str-iteration is enabled
24962496 reveal_type(ch) # N: Revealed type is "builtins.str"
24972497
2498+ @runtime_checkable
24982499class CollectionSubclass(Collection[_T_co], Protocol[_T_co]):
24992500 @abstractmethod
25002501 def __missing_impl__(self): ...
@@ -2512,12 +2513,52 @@ takes_collection_subclass(StrSubclass()) # E: Argument 1 to "takes_collection_s
25122513def dict_unpacking_unaffected_by_union_simplification(x: Mapping[str, Union[str, Sequence[str]]]) -> None:
25132514 x = {**x}
25142515
2515- def narrowing (x: " str | Sequence[str]" ):
2516+ def narrowing_str (x: Union[ str, Sequence[str]] ):
25162517 if isinstance(x, str):
25172518 reveal_type(x) # N: Revealed type is "builtins.str"
25182519 else:
25192520 reveal_type(x) # N: Revealed type is "typing.Sequence[builtins.str]"
25202521
2522+ def narrowing_iterable(x: Union[str, Iterable[str], Iterable[str]]):
2523+ if isinstance(x, Iterable):
2524+ reveal_type(x) # N: Revealed type is "builtins.str | typing.Iterable[builtins.str]"
2525+ else:
2526+ reveal_type(x) # E: Statement is unreachable
2527+
2528+ def is_iterable_guard(x: object) -> TypeGuard[Iterable[str]]: ...
2529+
2530+ def is_iterable_is(x: object) -> TypeIs[Iterable[str]]: ...
2531+
2532+ def narrowing_typeguard_iterable(x: Union[str, Iterable[str]]):
2533+ if is_iterable_guard(x):
2534+ reveal_type(x) # N: Revealed type is "builtins.str | typing.Iterable[builtins.str]"
2535+ else:
2536+ reveal_type(x) # N: Revealed type is "builtins.str | typing.Iterable[builtins.str]"
2537+
2538+ def narrowing_typeis_iterable(x: Union[str, Iterable[str]]):
2539+ if is_iterable_is(x):
2540+ reveal_type(x) # N: Revealed type is "builtins.str | typing.Iterable[builtins.str]"
2541+ else:
2542+ reveal_type(x) # E: Statement is unreachable
2543+
2544+ def narrowing_incompatible_collection_subclass(x: Union[str, CollectionSubclass[str]]):
2545+ if isinstance(x, CollectionSubclass):
2546+ reveal_type(x) # N: Revealed type is "__main__.CollectionSubclass[builtins.str]"
2547+ else:
2548+ reveal_type(x) # N: Revealed type is "builtins.str"
2549+
2550+ def narrowing_object(x: object):
2551+ if isinstance(x, Iterable):
2552+ reveal_type(x) # N: Revealed type is "typing.Iterable[Any] | builtins.str"
2553+ else:
2554+ reveal_type(x) # N: Revealed type is "builtins.object"
2555+
2556+ def narrowing_type(x: Union[type[str], type[int]]):
2557+ if issubclass(x, Iterable):
2558+ reveal_type(x) # N: Revealed type is "type[builtins.str]"
2559+ else:
2560+ reveal_type(x) # N: Revealed type is "type[builtins.int]"
2561+
25212562Item = TypeVar("Item")
25222563def takes_generic_list(x: list[Item]) -> None: ...
25232564takes_generic_list(reveal_type([s, seq])) # N: Revealed type is "builtins.list[builtins.object]"
0 commit comments