Skip to content

Commit 46868f7

Browse files
authored
DOC: update documentation of type annotation (PEP 585) (#44948)
1 parent b15ef37 commit 46868f7

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

doc/source/development/contributing_codebase.rst

+16-6
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ pandas strongly encourages the use of :pep:`484` style type hints. New developme
303303
Style guidelines
304304
~~~~~~~~~~~~~~~~
305305

306-
Types imports should follow the ``from typing import ...`` convention. So rather than
306+
Type imports should follow the ``from typing import ...`` convention. Some types do not need to be imported since :pep:`585` some builtin constructs, such as ``list`` and ``tuple``, can directly be used for type annotations. So rather than
307307

308308
.. code-block:: python
309309
@@ -315,21 +315,31 @@ You should write
315315

316316
.. code-block:: python
317317
318-
from typing import List, Optional, Union
318+
primes: list[int] = []
319319
320-
primes: List[int] = []
320+
``Optional`` should be avoided in favor of the shorter ``| None``, so instead of
321321

322-
``Optional`` should be used where applicable, so instead of
322+
.. code-block:: python
323+
324+
from typing import Union
325+
326+
maybe_primes: list[Union[int, None]] = []
327+
328+
or
323329

324330
.. code-block:: python
325331
326-
maybe_primes: List[Union[int, None]] = []
332+
from typing import Optional
333+
334+
maybe_primes: list[Optional[int]] = []
327335
328336
You should write
329337

330338
.. code-block:: python
331339
332-
maybe_primes: List[Optional[int]] = []
340+
from __future__ import annotations # noqa: F404
341+
342+
maybe_primes: list[int | None] = []
333343
334344
In some cases in the code base classes may define class variables that shadow builtins. This causes an issue as described in `Mypy 1775 <https://github.com/python/mypy/issues/1775#issuecomment-310969854>`_. The defensive solution here is to create an unambiguous alias of the builtin and use that without your annotation. For example, if you come across a definition like
335345

0 commit comments

Comments
 (0)