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.

Author AlexWaygood
Recipients AlexWaygood, JelleZijlstra, docs@python, gvanrossum, kj, sobolevn
Date 2022-02-16.17:01:18
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1645030878.3.0.822530505617.issue46769@roundup.psfhosted.org>
In-reply-to
Content
There are three variants of `TypeVar`s:

(1) TypeVars that are neither constrained nor bound: `T = TypeVar("T")`
(2) TypeVars that are bound: `U = TypeVar("U", bound=str)`
(3) TypeVars that are constrained: `V = TypeVar("V", str, bytes)`

The third variant is important for annotating certain functions, such as those in the `re` module. However, it has a number of issues (see https://github.com/python/typing/discussions/1080 for further discussion):

(1) It has somewhat surprising semantics in many situations.
(2) It is difficult for type checkers to deal with, leading to a number of bugs in mypy, for example.
(3) Many users (especially people relatively inexperienced with Python typing) reach for constrained TypeVars in situations where using bound TypeVars or the @overload decorator would be more appropriate.

Both PEP 484 and the documentation for the typing module, however:

(1) Give examples for variants (1) and (3), but not for variant (2), which is treated as something of an afterthought.
(2) Do not mention that TypeVars can be bound to a union of types, which is an important point: `T = TypeVar("T", str, bytes)` has different semantics to `T = TypeVar("T", bound=str|bytes)`, and often the latter is more appropriate.
History
Date User Action Args
2022-02-16 17:01:18AlexWaygoodsetrecipients: + AlexWaygood, gvanrossum, docs@python, JelleZijlstra, sobolevn, kj
2022-02-16 17:01:18AlexWaygoodsetmessageid: <1645030878.3.0.822530505617.issue46769@roundup.psfhosted.org>
2022-02-16 17:01:18AlexWaygoodlinkissue46769 messages
2022-02-16 17:01:18AlexWaygoodcreate