Skip to content

[mypyc] Use lower-case generic types such as "list[t]" in docs #18576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypyc/doc/differences_from_python.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ performance.
integer values. A side effect of this is that the exact runtime type of
``int`` values is lost. For example, consider this simple function::

def first_int(x: List[int]) -> int:
def first_int(x: list[int]) -> int:
return x[0]

print(first_int([True])) # Output is 1, instead of True!
Expand Down
2 changes: 1 addition & 1 deletion mypyc/doc/native_classes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ These non-native classes can be used as base classes of native
classes:

* ``object``
* ``dict`` (and ``Dict[k, v]``)
* ``dict`` (and ``dict[k, v]``)
* ``BaseException``
* ``Exception``
* ``ValueError``
Expand Down
5 changes: 2 additions & 3 deletions mypyc/doc/performance_tips_and_tricks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ here we call ``acme.get_items()``, but it has no type annotation. We
can use an explicit type annotation for the variable to which we
assign the result::

from typing import List, Tuple
import acme

def work() -> None:
# Annotate "items" to help mypyc
items: List[Tuple[int, str]] = acme.get_items()
items: list[tuple[int, str]] = acme.get_items()
for item in items:
... # Do some work here

Expand Down Expand Up @@ -140,7 +139,7 @@ Similarly, caching a frequently called method in a local variable can
help in CPython, but it can slow things down in compiled code, since
the code won't use :ref:`early binding <early-binding>`::

def squares(n: int) -> List[int]:
def squares(n: int) -> list[int]:
a = []
append = a.append # Not a good idea in compiled code!
for i in range(n):
Expand Down
16 changes: 8 additions & 8 deletions mypyc/doc/using_type_annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ implementations:
* ``float`` (:ref:`native operations <float-ops>`)
* ``bool`` (:ref:`native operations <bool-ops>`)
* ``str`` (:ref:`native operations <str-ops>`)
* ``List[T]`` (:ref:`native operations <list-ops>`)
* ``Dict[K, V]`` (:ref:`native operations <dict-ops>`)
* ``Set[T]`` (:ref:`native operations <set-ops>`)
* ``Tuple[T, ...]`` (variable-length tuple; :ref:`native operations <tuple-ops>`)
* ``list[T]`` (:ref:`native operations <list-ops>`)
* ``dict[K, V]`` (:ref:`native operations <dict-ops>`)
* ``set[T]`` (:ref:`native operations <set-ops>`)
* ``tuple[T, ...]`` (variable-length tuple; :ref:`native operations <tuple-ops>`)
* ``None``

The link after each type lists all supported native, optimized
Expand All @@ -61,10 +61,10 @@ variable. For example, here we have a runtime type error on the final
line of ``example`` (the ``Any`` type means an arbitrary, unchecked
value)::

from typing import List, Any
from typing import Any

def example(a: List[Any]) -> None:
b: List[int] = a # No error -- items are not checked
def example(a: list[Any]) -> None:
b: list[int] = a # No error -- items are not checked
print(b[0]) # Error here -- got str, but expected int

example(["x"])
Expand Down Expand Up @@ -126,7 +126,7 @@ Tuple types

Fixed-length
`tuple types <https://mypy.readthedocs.io/en/stable/kinds_of_types.html#tuple-types>`_
such as ``Tuple[int, str]`` are represented
such as ``tuple[int, str]`` are represented
as :ref:`value types <value-and-heap-types>` when stored in variables,
passed as arguments, or returned from functions. Value types are
allocated in the low-level machine stack or in CPU registers, as
Expand Down