Skip to content

Commit 2eb7e16

Browse files
Google Python teamgpshead
authored andcommitted
Project import generated by Copybara.
PiperOrigin-RevId: 442743475
1 parent 629edc1 commit 2eb7e16

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

pyguide.md

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,12 +1486,6 @@ You can also declare the type of a variable using similar
14861486
a: SomeType = some_func()
14871487
```
14881488
1489-
Or by using a type comment in code that must support legacy Python versions.
1490-
1491-
```python
1492-
a = some_func() # type: SomeType
1493-
```
1494-
14951489
<a id="s2.21.2-pros"></a>
14961490
<a id="2212-pros"></a>
14971491
@@ -3182,25 +3176,26 @@ ignore`.
31823176
<a id="typing-variables"></a>
31833177
#### 3.19.8 Typing Variables
31843178
3185-
If an internal variable has a type that is hard or impossible to infer, you can
3186-
specify its type in a couple ways.
3187-
3188-
<a id="type-comments"></a>
3189-
[*Type Comments:*](#type-comments)
3190-
: Use a `# type:` comment on the end of the line
3191-
3192-
```python
3193-
a = SomeUndecoratedFunction() # type: Foo
3194-
```
3195-
31963179
<a id="annotated-assignments"></a>
31973180
[*Annotated Assignments*](#annotated-assignments)
3198-
: Use a colon and type between the variable name and value, as with function
3199-
arguments.
3181+
: If an internal variable has a type that is hard or impossible to infer,
3182+
specify its type with an annotated assignment - use a colon and type between
3183+
the variable name and value (the same as is done with function arguments
3184+
that have a default value):
32003185
3201-
```python
3202-
a: Foo = SomeUndecoratedFunction()
3203-
```
3186+
```python
3187+
a: Foo = SomeUndecoratedFunction()
3188+
```
3189+
3190+
<a id="type-comments"></a>
3191+
[*Type Comments*](#type-comments)
3192+
: Though you may see them remaining in the codebase (they were necessary
3193+
before Python 3.6), do not add any more uses of a `# type: <type name>`
3194+
comment on the end of the line:
3195+
3196+
```python
3197+
a = SomeUndecoratedFunction() # type: Foo
3198+
```
32043199
32053200
<a id="s3.19.9-tuples-vs-lists"></a>
32063201
<a id="s3.19.9-tuples"></a>
@@ -3214,9 +3209,9 @@ have a single repeated type or a set number of elements with different types.
32143209
The latter is commonly used as the return type from a function.
32153210
32163211
```python
3217-
a = [1, 2, 3] # type: list[int]
3218-
b = (1, 2, 3) # type: tuple[int, ...]
3219-
c = (1, "2", 3.5) # type: tuple[int, str, float]
3212+
a: list[int] = [1, 2, 3]
3213+
b: tuple[int, ...] = (1, 2, 3)
3214+
c: tuple[int, str, float] = (1, "2", 3.5)
32203215
```
32213216
32223217
<a id="s3.19.10-typevars"></a>

0 commit comments

Comments
 (0)