This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Sequence example in typing module documentation does not typecheck
Type: Stage:
Components: Documentation Versions: Python 3.6, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: docs@python, gvanrossum, michael0x2a, python-dev
Priority: normal Keywords:

Created on 2016-06-27 17:09 by michael0x2a, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg269389 - (view) Author: Michael Lee (michael0x2a) * Date: 2016-06-27 17:09
In the documentation for the `typing` module, the [entry for the `List` type][0] uses the following example to help demonstrate when to use `Sequence` vs `List`:

    T = TypeVar('T', int, float)

    def vec2(x: T, y: T) -> List[T]:
        return [x, y]

    def slice__to_4(vector: Sequence[T]) -> List[T]:
        return vector[0:4]

However, the `slice__to_4` function does not actually typecheck since there's no guarantee that a slice of a sequence will return a `List`. For example the vector could be a numpy array or a custom subclass of `collections.abc.Sequence` with an unusual `__getitem__`. (Mypy correctly catches this error and complains about an "Incompatible return value type").

The documentation should probably be updated to use an example that _does_ typecheck, though I'm not sure what exactly that example might look like? Maybe replace `slice__to_4` with something like this?

    def keep_positives(vector: Sequence[T]) -> List[T]:
        return [item for item in vector if item > 0]
msg269390 - (view) Author: Michael Lee (michael0x2a) * Date: 2016-06-27 17:12
Whoops, forgot the link to the error in the docs: 

https://docs.python.org/3/library/typing.html#typing.List
msg269445 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-06-28 17:13
New changeset 0aec17c8f434 by Guido van Rossum in branch '3.5':
Fix issue #27402: example for typing did not type-check.
https://hg.python.org/cpython/rev/0aec17c8f434

New changeset 57f3514564f6 by Guido van Rossum in branch 'default':
Fix issue #27402: example for typing did not type-check. (Merge 3.5->3.6)
https://hg.python.org/cpython/rev/57f3514564f6
msg269446 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2016-06-28 17:32
Thanks!
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71589
2016-06-28 17:32:44gvanrossumsetstatus: open -> closed

nosy: + gvanrossum
messages: + msg269446

resolution: fixed
2016-06-28 17:13:24python-devsetnosy: + python-dev
messages: + msg269445
2016-06-27 17:12:11michael0x2asetmessages: + msg269390
2016-06-27 17:09:40michael0x2acreate