|
| 1 | +# Pandas Copilot Instructions |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +`pandas` is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language. |
| 5 | + |
| 6 | +<!-- TODO: Add Sections for ## Root Folders and ## Core Architecture (pandas/ dir ) --> |
| 7 | + |
| 8 | +## Type Hints |
| 9 | + |
| 10 | +pandas strongly encourages the use of PEP 484 style type hints. New development should contain type hints and pull requests to annotate existing code are accepted as well! |
| 11 | + |
| 12 | +### Style Guidelines |
| 13 | + |
| 14 | +Type imports should follow the from `typing import ...` convention. Your code may be automatically re-written to use some modern constructs (e.g. using the built-in `list` instead of `typing.List`) by the pre-commit checks. |
| 15 | + |
| 16 | +In some cases in the code base classes may define class variables that shadow builtins. This causes an issue as described in Mypy 1775. 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 |
| 17 | + |
| 18 | +``` |
| 19 | +class SomeClass1: |
| 20 | + str = None |
| 21 | +``` |
| 22 | + |
| 23 | +The appropriate way to annotate this would be as follows |
| 24 | + |
| 25 | +``` |
| 26 | +str_type = str |
| 27 | +
|
| 28 | +class SomeClass2: |
| 29 | + str: str_type = None |
| 30 | +``` |
| 31 | +In some cases you may be tempted to use `cast` from the typing module when you know better than the analyzer. This occurs particularly when using custom inference functions. For example |
| 32 | + |
| 33 | +``` |
| 34 | +from typing import cast |
| 35 | +
|
| 36 | +from pandas.core.dtypes.common import is_number |
| 37 | +
|
| 38 | +def cannot_infer_bad(obj: Union[str, int, float]): |
| 39 | +
|
| 40 | + if is_number(obj): |
| 41 | + ... |
| 42 | + else: # Reasonably only str objects would reach this but... |
| 43 | + obj = cast(str, obj) # Mypy complains without this! |
| 44 | + return obj.upper() |
| 45 | +``` |
| 46 | +The limitation here is that while a human can reasonably understand that `is_number` would catch the `int` and `float` types mypy cannot make that same inference just yet (see mypy #5206. While the above works, the use of `cast` is strongly discouraged. Where applicable a refactor of the code to appease static analysis is preferable.) |
| 47 | + |
| 48 | +``` |
| 49 | +def cannot_infer_good(obj: Union[str, int, float]): |
| 50 | +
|
| 51 | + if isinstance(obj, str): |
| 52 | + return obj.upper() |
| 53 | + else: |
| 54 | + ... |
| 55 | +``` |
| 56 | +With custom types and inference this is not always possible so exceptions are made, but every effort should be exhausted to avoid `cast` before going down such paths. |
| 57 | + |
| 58 | +### pandas-specific types |
| 59 | + |
| 60 | +Commonly used types specific to pandas will appear in pandas._typing and you should use these where applicable. This module is private for now but ultimately this should be exposed to third party libraries who want to implement type checking against pandas. |
| 61 | + |
| 62 | +For example, quite a few functions in pandas accept a `dtype` argument. This can be expressed as a string like `"object"`, a `numpy.dtype` like `np.int64` or even a pandas `ExtensionDtype` like `pd.CategoricalDtype`. Rather than burden the user with having to constantly annotate all of those options, this can simply be imported and reused from the pandas._typing module |
| 63 | + |
| 64 | +``` |
| 65 | +from pandas._typing import Dtype |
| 66 | +
|
| 67 | +def as_type(dtype: Dtype) -> ...: |
| 68 | + ... |
| 69 | +``` |
| 70 | + |
| 71 | +This module will ultimately house types for repeatedly used concepts like “path-like”, “array-like”, “numeric”, etc… and can also hold aliases for commonly appearing parameters like `axis`. Development of this module is active so be sure to refer to the source for the most up to date list of available types. |
0 commit comments