Skip to content

error_code_list2.rst: typo + edit #11052

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 3, 2021
Merged
Changes from all commits
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
13 changes: 7 additions & 6 deletions docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,25 +234,26 @@ since unless implemented by a sub-type, the expression will always evaluate to t
...


This check might falsely imply an error. For example, ``typing.Iterable`` does not implement
This check might falsely imply an error. For example, ``Iterable`` does not implement
``__len__`` and so this code will be flagged:

.. code-block:: python

# mypy: enable-error-code truthy-bool
from typing import Iterable

def transform(items: Iterable[int]) -> List[int]:
# Error: "items" has type "typing.Iterable[int]" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
def transform(items: Iterable[int]) -> Iterable[int]:
# Error: "items" has type "Iterable[int]" which does not implement __bool__ or __len__ so it could always be true in boolean context [truthy-bool]
if not items:
return [42]
return [x + 1 for x in items]



If called as ``transform((int(s) for s in []))``, this function would not return ``[42]]`` unlike what the author
If called as ``transform((int(s) for s in []))``, this function would not return ``[42]`` unlike what the author
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops [42]]

might have intended. Of course it's possible that ``transform`` is only passed ``list`` objects, and so there is
no error in practice. In such case, it might be prudent to annotate ``items: typing.Sequence[int]``.
no error in practice. In such case, it might be prudent to annotate ``items: Sequence[int]``.

This is similar in concept to ensuring that an expression's type implements an expected interface (e.g. ``typing.Sized``),
This is similar in concept to ensuring that an expression's type implements an expected interface (e.g. ``Sized``),
except that attempting to invoke an undefined method (e.g. ``__len__``) results in an error,
while attempting to evaluate an object in boolean context without a concrete implementation results in a truthy value.