Skip to content

Commit 18b3312

Browse files
JelleZijlstraambv
authored andcommitted
PEP 484: Clarify definition of AnyStr (#744)
The definition of AnyStr as given makes sense only in Python 3, as observed by @srittau. To prevent confusion, this PR changes the definition to use Text instead of str. I also changed the two places where AnyStr was defined as an example, and made a few other related adjustments: - Remove mention of None as modifying the type, which is not true as of #689. - Move Text up in the list of convenience definition because we now need it for the definition of AnyStr. - Modify definition of Optional to use `None` instead of `type(None)`, because the latter is not a valid static type.
1 parent b991d19 commit 18b3312

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

pep-0484.txt

+8-9
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,9 @@ example, we can define a type variable that ranges over just ``str`` and
312312
``bytes``. By default, a type variable ranges over all possible types.
313313
Example of constraining a type variable::
314314

315-
from typing import TypeVar
315+
from typing import TypeVar, Text
316316

317-
AnyStr = TypeVar('AnyStr', str, bytes)
317+
AnyStr = TypeVar('AnyStr', Text, bytes)
318318

319319
def concat(x: AnyStr, y: AnyStr) -> AnyStr:
320320
return x + y
@@ -1357,8 +1357,7 @@ without specifying the actual default value. For example::
13571357
def foo(x: AnyStr, y: AnyStr = ...) -> AnyStr: ...
13581358

13591359
What should the default value look like? Any of the options ``""``,
1360-
``b""`` or ``None`` fails to satisfy the type constraint (actually,
1361-
``None`` will *modify* the type to become ``Optional[AnyStr]``).
1360+
``b""`` or ``None`` fails to satisfy the type constraint.
13621361

13631362
In such cases the default value may be specified as a literal
13641363
ellipsis, i.e. the above example is literally what you would write.
@@ -1785,9 +1784,9 @@ A constrained ``TypeVar`` type can often be used instead of using the
17851784
``@overload`` decorator. For example, the definitions of ``concat1``
17861785
and ``concat2`` in this stub file are equivalent::
17871786

1788-
from typing import TypeVar
1787+
from typing import TypeVar, Text
17891788

1790-
AnyStr = TypeVar('AnyStr', str, bytes)
1789+
AnyStr = TypeVar('AnyStr', Text, bytes)
17911790

17921791
def concat1(x: AnyStr, y: AnyStr) -> AnyStr: ...
17931792

@@ -2024,12 +2023,12 @@ A few one-off types are defined that test for single special methods
20242023

20252024
Convenience definitions:
20262025

2027-
* Optional, defined by ``Optional[t] == Union[t, type(None)]``
2028-
2029-
* AnyStr, defined as ``TypeVar('AnyStr', str, bytes)``
2026+
* Optional, defined by ``Optional[t] == Union[t, None]``
20302027

20312028
* Text, a simple alias for ``str`` in Python 3, for ``unicode`` in Python 2
20322029

2030+
* AnyStr, defined as ``TypeVar('AnyStr', Text, bytes)``
2031+
20332032
* NamedTuple, used as
20342033
``NamedTuple(type_name, [(field_name, field_type), ...])``
20352034
and equivalent to

0 commit comments

Comments
 (0)