Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add str/Iterable[str] to unsafe-subtype checks
Co-authored-by: m-aciek <9288014+m-aciek@users.noreply.github.com>
  • Loading branch information
Copilot and m-aciek committed Dec 20, 2025
commit 2b3e1b36cf522a860c0a55a641b9f3d631fd8513
29 changes: 29 additions & 0 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,8 @@ If enabled with :option:`--enable-error-code unsafe-subtype <mypy --enable-error
mypy will block certain subtype relationships that are unsafe at runtime despite
being valid in Python's type system.

**datetime/date incompatibility**

The primary use case is blocking the ``datetime.datetime`` to ``datetime.date``
inheritance relationship. While ``datetime`` is a subclass of ``date`` at runtime,
comparing a ``datetime`` with a ``date`` raises a ``TypeError``. When this error
Expand Down Expand Up @@ -751,3 +753,30 @@ preventing the runtime error.

**Note:** Equality comparisons (``==`` and ``!=``) still work between these types,
as ``__eq__`` accepts ``object`` as its parameter.

**str/Iterable[str] incompatibility**

Another common issue is using a ``str`` where ``Iterable[str]`` is expected. While
``str`` is iterable and yields strings (characters), this often leads to bugs because
developers expect iteration over a collection of strings, not individual characters.

Example:

.. code-block:: python

# mypy: enable-error-code="unsafe-subtype"
from typing import Iterable

def process_items(items: Iterable[str]) -> None:
for item in items:
print(f"Item: {item}")

# Error: Argument 1 to "process_items" has incompatible type "str"; expected "Iterable[str]"
process_items("hello") # Would iterate over: 'h', 'e', 'l', 'l', 'o'

# OK: Pass a list of strings instead
process_items(["hello"]) # Iterates over: "hello"

This prevents a common source of bugs where a single string is accidentally treated
as a collection of strings, with iteration yielding individual characters instead of
the full string.
2 changes: 2 additions & 0 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
# as a subtype can cause runtime errors.
UNSAFE_SUBTYPING_PAIRS: Final = [
("datetime.datetime", "datetime.date"),
("builtins.str", "typing.Iterable"),
("builtins.str", "collections.abc.Iterable"),
]

TypeParameterChecker: _TypeAlias = Callable[[Type, Type, int, bool, "SubtypeContext"], bool]
Expand Down
26 changes: 26 additions & 0 deletions test-data/unit/check-unsafe-subtype.test
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,29 @@ class datetime(date):
def __le__(self, other: datetime) -> bool: ... # type: ignore[override]
def __gt__(self, other: datetime) -> bool: ... # type: ignore[override]
def __ge__(self, other: datetime) -> bool: ... # type: ignore[override]

[case testStrIterableBlocking]
# flags: --enable-error-code unsafe-subtype
from typing import Iterable
from collections.abc import Iterable as ABCIterable

def process_typing(items: Iterable[str]) -> None:
pass

def process_abc(items: ABCIterable[str]) -> None:
pass

# str to Iterable[str] - blocked
s: str = "hello"
process_typing(s) # E: Argument 1 to "process_typing" has incompatible type "str"; expected "Iterable[str]"

# str to collections.abc.Iterable[str] - blocked
process_abc(s) # E: Argument 1 to "process_abc" has incompatible type "str"; expected "Iterable[str]"

# Assignment - blocked
items: Iterable[str] = "hello" # E: Incompatible types in assignment (expression has type "str", variable has type "Iterable[str]")

# list[str] to Iterable[str] - OK
lst: list[str] = ["hello", "world"]
process_typing(lst) # OK
[builtins fixtures/list.pyi]