You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/source/development/contributing_codebase.rst
+36-6
Original file line number
Diff line number
Diff line change
@@ -303,7 +303,7 @@ pandas strongly encourages the use of :pep:`484` style type hints. New developme
303
303
Style guidelines
304
304
~~~~~~~~~~~~~~~~
305
305
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
307
307
308
308
.. code-block:: python
309
309
@@ -315,21 +315,31 @@ You should write
315
315
316
316
.. code-block:: python
317
317
318
-
from typing import List, Optional, Union
318
+
primes: list[int] = []
319
319
320
-
primes: List[int] = []
320
+
``Optional`` should be avoided in favor of the shorter ``| None``, so instead of
321
321
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
323
329
324
330
.. code-block:: python
325
331
326
-
maybe_primes: List[Union[int, None]] = []
332
+
from typing import Optional
333
+
334
+
maybe_primes: list[Optional[int]] = []
327
335
328
336
You should write
329
337
330
338
.. code-block:: python
331
339
332
-
maybe_primes: List[Optional[int]] = []
340
+
from__future__import annotations # noqa: F404
341
+
342
+
maybe_primes: list[int|None] = []
333
343
334
344
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
335
345
@@ -410,6 +420,26 @@ A recent version of ``numpy`` (>=1.21.0) is required for type validation.
410
420
411
421
.. _contributing.ci:
412
422
423
+
Testing type hints in code using pandas
424
+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
425
+
426
+
.. warning::
427
+
428
+
* Pandas is not yet a py.typed library (:pep:`561`)!
429
+
The primary purpose of locally declaring pandas as a py.typed library is to test and
430
+
improve the pandas-builtin type annotations.
431
+
432
+
Until pandas becomes a py.typed library, it is possible to easily experiment with the type
433
+
annotations shipped with pandas by creating an empty file named "py.typed" in the pandas
0 commit comments