Skip to content

Commit 3460717

Browse files
authored
Remove most mentions of type comments from docs (#12683)
1 parent ee78afe commit 3460717

9 files changed

+14
-88
lines changed

docs/source/cheat_sheet_py3.rst

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ and we use it in most examples.
2525
# This is how you declare the type of a variable type in Python 3.6
2626
age: int = 1
2727
28-
# In Python 3.5 and earlier you can use a type comment instead
29-
# (equivalent to the previous definition)
30-
age = 1 # type: int
31-
3228
# You don't need to initialize a variable to annotate it
3329
a: int # Ok (no value at runtime until assigned)
3430
@@ -45,7 +41,7 @@ Built-in types
4541

4642
.. code-block:: python
4743
48-
44+
4945
from typing import List, Set, Dict, Tuple, Optional
5046
5147
# For simple built-in types, just use the name of the type
@@ -65,9 +61,6 @@ Built-in types
6561
x: List[int] = [1]
6662
x: Set[int] = {6, 7}
6763
68-
# Same as above, but with type comment syntax (Python 3.5 and earlier)
69-
x = [1] # type: List[int]
70-
7164
# For mappings, we need the types of both keys and values
7265
x: dict[str, float] = {"field": 2.0} # Python 3.9+
7366
x: Dict[str, float] = {"field": 2.0}

docs/source/class_basics.rst

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,6 @@ As in Python generally, a variable defined in the class body can be used
4242
as a class or an instance variable. (As discussed in the next section, you
4343
can override this with a :py:data:`~typing.ClassVar` annotation.)
4444

45-
Type comments work as well, if you need to support Python versions earlier
46-
than 3.6:
47-
48-
.. code-block:: python
49-
50-
class A:
51-
x = None # type: list[int] # Declare attribute 'x' of type list[int]
52-
53-
Note that attribute definitions in the class body that use a type comment
54-
are special: a ``None`` value is valid as the initializer, even though
55-
the declared type is not optional. This should be used sparingly, as this can
56-
result in ``None``-related runtime errors that mypy can't detect.
57-
5845
Similarly, you can give explicit types to instance variables defined
5946
in a method:
6047

docs/source/command_line.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -548,11 +548,11 @@ of the above sections.
548548
from typing import Optional
549549
550550
a = None # Need type annotation here if using --local-partial-types
551-
b = None # type: Optional[int]
551+
b: Optional[int] = None
552552
553553
class Foo:
554554
bar = None # Need type annotation here if using --local-partial-types
555-
baz = None # type: Optional[int]
555+
baz: Optional[int] = None
556556
557557
def __init__(self) -> None:
558558
self.bar = 1

docs/source/dynamic_typing.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ operations:
7777
o.foo() # Error!
7878
o + 2 # Error!
7979
open(o) # Error!
80-
n = 1 # type: int
80+
n: int = 1
8181
n = o # Error!
8282
8383
You can use different :ref:`type narrowing <type-narrowing>`

docs/source/generics.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ In this way, for example, you can typecheck chaining of setter methods:
295295
self.width = w
296296
return self
297297
298-
circle = Circle().set_scale(0.5).set_radius(2.7) # type: Circle
299-
square = Square().set_scale(0.5).set_width(3.2) # type: Square
298+
circle: Circle = Circle().set_scale(0.5).set_radius(2.7)
299+
square: Square = Square().set_scale(0.5).set_width(3.2)
300300
301301
Without using generic ``self``, the last two lines could not be type-checked properly.
302302

@@ -310,7 +310,7 @@ For class methods, you can also define generic ``cls``, using :py:class:`Type[T]
310310
T = TypeVar('T', bound='Friend')
311311
312312
class Friend:
313-
other = None # type: Friend
313+
other: "Friend" = None
314314
315315
@classmethod
316316
def make_pair(cls: Type[T]) -> tuple[T, T]:

docs/source/getting_started.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,9 +317,6 @@ syntax like so:
317317
# If you're using Python 3.6+
318318
my_global_dict: Dict[int, float] = {}
319319
320-
# If you want compatibility with even older versions of Python
321-
my_global_dict = {} # type: Dict[int, float]
322-
323320
.. _stubs-intro:
324321

325322
Library stubs and typeshed

docs/source/kinds_of_types.rst

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -347,23 +347,13 @@ This also works for attributes defined within methods:
347347
def __init__(self) -> None:
348348
self.count: Optional[int] = None
349349
350-
As a special case, you can use a non-optional type when initializing an
351-
attribute to ``None`` inside a class body *and* using a type comment,
352-
since when using a type comment, an initializer is syntactically required,
353-
and ``None`` is used as a dummy, placeholder initializer:
350+
This is not a problem when using variable annotations, since no initial
351+
value is needed:
354352

355353
.. code-block:: python
356354
357355
class Container:
358-
items = None # type: list[str] # OK (only with type comment)
359-
360-
This is not a problem when using variable annotations, since no initializer
361-
is needed:
362-
363-
.. code-block:: python
364-
365-
class Container:
366-
items: list[str] # No initializer
356+
items: list[str] # No initial value
367357
368358
Mypy generally uses the first assignment to a variable to
369359
infer the type of the variable. However, if you assign both a ``None``
@@ -421,9 +411,6 @@ the runtime with some limitations (see :ref:`runtime_troubles`).
421411
422412
t2: int | None # equivalent to Optional[int]
423413
424-
# Usable in type comments
425-
t3 = 42 # type: int | str
426-
427414
.. _no_strict_optional:
428415

429416
Disabling strict optional checking

docs/source/more_types.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ implicitly casting from ``UserId`` where ``int`` is expected. Examples:
120120
name_by_id(42) # Fails type check
121121
name_by_id(UserId(42)) # OK
122122
123-
num = UserId(5) + 1 # type: int
123+
num: int = UserId(5) + 1
124124
125125
:py:func:`NewType <typing.NewType>` accepts exactly two arguments. The first argument must be a string literal
126126
containing the name of the new type and must equal the name of the variable to which the new
@@ -985,7 +985,7 @@ dictionary value depends on the key:
985985
986986
Movie = TypedDict('Movie', {'name': str, 'year': int})
987987
988-
movie = {'name': 'Blade Runner', 'year': 1982} # type: Movie
988+
movie: Movie = {'name': 'Blade Runner', 'year': 1982}
989989
990990
``Movie`` is a ``TypedDict`` type with two items: ``'name'`` (with type ``str``)
991991
and ``'year'`` (with type ``int``). Note that we used an explicit type
@@ -1080,7 +1080,7 @@ keys. This will be flagged as an error:
10801080
.. code-block:: python
10811081
10821082
# Error: 'year' missing
1083-
toy_story = {'name': 'Toy Story'} # type: Movie
1083+
toy_story: Movie = {'name': 'Toy Story'}
10841084
10851085
Sometimes you want to allow keys to be left out when creating a
10861086
``TypedDict`` object. You can provide the ``total=False`` argument to
@@ -1090,7 +1090,7 @@ Sometimes you want to allow keys to be left out when creating a
10901090
10911091
GuiOptions = TypedDict(
10921092
'GuiOptions', {'language': str, 'color': str}, total=False)
1093-
options = {} # type: GuiOptions # Okay
1093+
options: GuiOptions = {} # Okay
10941094
options['language'] = 'en'
10951095
10961096
You may need to use :py:meth:`~dict.get` to access items of a partial (non-total)

docs/source/type_inference_and_annotations.rst

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,6 @@ type:
4444
4545
x: Union[int, str] = 1.1 # Error!
4646
47-
The variable annotation syntax is available starting from Python 3.6.
48-
In earlier Python versions, you can use a special comment after an
49-
assignment statement to declare the type of a variable:
50-
51-
.. code-block:: python
52-
53-
x = 1 # type: Union[int, str]
54-
55-
We'll use both syntax variants in examples. The syntax variants are
56-
mostly interchangeable, but the variable annotation syntax allows
57-
defining the type of a variable without initialization, which is not
58-
possible with the comment syntax:
59-
60-
.. code-block:: python
61-
62-
x: str # Declare type of 'x' without initialization
63-
6447
.. note::
6548

6649
The best way to think about this is that the type annotation sets the
@@ -182,27 +165,6 @@ Working around the issue is easy by adding a type annotation:
182165
a: list[int] = [] # OK
183166
foo(a)
184167
185-
Declaring multiple variable types at a time
186-
*******************************************
187-
188-
You can declare more than a single variable at a time, but only with
189-
a type comment. In order to nicely work with multiple assignment, you
190-
must give each variable a type separately:
191-
192-
.. code-block:: python
193-
194-
i, found = 0, False # type: int, bool
195-
196-
You can optionally use parentheses around the types, assignment targets
197-
and assigned expression:
198-
199-
.. code-block:: python
200-
201-
i, found = 0, False # type: (int, bool) # OK
202-
(i, found) = 0, False # type: int, bool # OK
203-
i, found = (0, False) # type: int, bool # OK
204-
(i, found) = (0, False) # type: (int, bool) # OK
205-
206168
Starred expressions
207169
*******************
208170

0 commit comments

Comments
 (0)