Skip to content

Commit 5a6906c

Browse files
committed
Merge remote-tracking branch 'upstream/main' into pandas-arrays
2 parents 9cb0923 + 82f8994 commit 5a6906c

File tree

150 files changed

+9131
-3406
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

150 files changed

+9131
-3406
lines changed

.github/workflows/optional.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: 'Optional'
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
workflow_dispatch:
8+
9+
jobs:
10+
nightly:
11+
runs-on: ubuntu-latest
12+
timeout-minutes: 10
13+
14+
steps:
15+
- uses: actions/checkout@v3
16+
17+
- name: Install project dependencies
18+
uses: ./.github/setup
19+
with:
20+
os: ubuntu-latest
21+
python-version: '3.11'
22+
23+
- name: Run pytest (against pandas nightly)
24+
run: poetry run poe pytest --nightly
25+
26+
mypy_nightly:
27+
runs-on: ubuntu-latest
28+
timeout-minutes: 10
29+
30+
steps:
31+
- uses: actions/checkout@v3
32+
33+
- name: Install project dependencies
34+
uses: ./.github/setup
35+
with:
36+
os: ubuntu-latest
37+
python-version: '3.11'
38+
39+
- name: Run mypy tests with mypy nightly
40+
run: poetry run poe mypy --mypy_nightly

.github/workflows/test.yml

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
name: 'Test'
22

33
on:
4-
[push, pull_request, workflow_dispatch]
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
59

610
env:
711
MPLBACKEND: 'Agg'
@@ -14,7 +18,7 @@ jobs:
1418
fail-fast: false
1519
matrix:
1620
os: [ubuntu-latest, windows-latest, macos-latest]
17-
python-version: ['3.8', '3.9', '3.10.6', '3.11']
21+
python-version: ['3.8', '3.9', '3.10', '3.11']
1822

1923
steps:
2024
- uses: actions/checkout@v3
@@ -25,11 +29,6 @@ jobs:
2529
os: ${{ matrix.os }}
2630
python-version: ${{ matrix.python-version }}
2731

28-
- name: Show poetry python location (Windows)
29-
shell: pwsh
30-
run: poetry run where python
31-
if: matrix.os == 'windows-latest'
32-
3332
- name: Run mypy on 'tests' (using the local stubs) and on the local stubs
3433
run: poetry run poe mypy
3534

@@ -42,22 +41,6 @@ jobs:
4241
- name: Install pandas-stubs and run tests on the installed stubs
4342
run: poetry run poe test_dist
4443

45-
nightly:
46-
runs-on: ubuntu-latest
47-
timeout-minutes: 10
48-
49-
steps:
50-
- uses: actions/checkout@v3
51-
52-
- name: Install project dependencies
53-
uses: ./.github/setup
54-
with:
55-
os: ubuntu-latest
56-
python-version: '3.10'
57-
58-
- name: Run pytest (against pandas nightly)
59-
run: poetry run poe pytest --nightly
60-
6144
precommit:
6245
runs-on: ubuntu-latest
6346
timeout-minutes: 10

.pre-commit-config.yaml

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,27 @@ ci:
33
autofix_prs: false
44
repos:
55
- repo: https://github.com/python/black
6-
rev: 22.8.0
6+
rev: 23.7.0
77
hooks:
88
- id: black
99
- repo: https://github.com/PyCQA/isort
10-
rev: 5.10.1
10+
rev: 5.12.0
1111
hooks:
1212
- id: isort
13-
- repo: https://github.com/asottile/pyupgrade
14-
rev: v3.2.0
13+
- repo: https://github.com/astral-sh/ruff-pre-commit
14+
rev: v0.0.283
1515
hooks:
16-
- id: pyupgrade
17-
types_or: [python, pyi]
18-
types: [text] # overwrite types: [python]
19-
args: [--py38-plus]
20-
- repo: https://github.com/PyCQA/flake8
21-
rev: 5.0.4
22-
hooks:
23-
- id: flake8
24-
name: flake8 (py)
25-
types: [python]
26-
args: [--ignore=E501 F841]
27-
- id: flake8
28-
name: flake8 (pyi)
29-
additional_dependencies:
30-
- flake8-pyi==22.10.0
31-
types: [pyi]
16+
- id: ruff
3217
args: [
33-
--ignore=E301 E302 E305 E402 E501 E701 E704 F401 F811 W503 Y019 Y027 Y034 Y037 Y041 Y042,
34-
# TypeVars in private files are already private
35-
--per-file-ignores=_*.pyi:Y001
18+
--exit-non-zero-on-fix,
19+
--target-version, py38,
20+
--extend-select, "PYI,UP,RUF100",
21+
--ignore, "E501,E731,F841,PYI042",
22+
--per-file-ignores, "_*.pyi:PYI001",
23+
--fix
3624
]
25+
- repo: https://github.com/codespell-project/codespell
26+
rev: v2.2.5
27+
hooks:
28+
- id: codespell
29+
additional_dependencies: [ tomli ]

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ convention of providing stubs in a separate package, as specified in [PEP 561](h
1717
pandas. In general, these stubs are narrower than what is possibly allowed by pandas,
1818
but follow a convention of suggesting best recommended practices for using pandas.
1919

20-
The stubs are likely incomplete in terms of covering the published API of pandas.
20+
The stubs are likely incomplete in terms of covering the published API of pandas. NOTE: The current 2.0.x releases of pandas-stubs do not support all of the new features of pandas 2.0. See this [tracker](https://github.com/pandas-dev/pandas-stubs/issues/624) to understand the current compatibility with version 2.0.
2121

2222
The stubs are tested with [mypy](http://mypy-lang.org/) and [pyright](https://github.com/microsoft/pyright#readme) and are currently shipped with the Visual Studio Code extension
2323
[pylance](https://github.com/microsoft/pylance-release#readme).
@@ -137,20 +137,19 @@ Documentation is a work-in-progress.
137137

138138
## Background
139139

140-
These stubs are the result of a strategic effort lead by the core pandas team to integrate [Microsoft type stub repository](https://github.com/microsoft/python-type-stubs)
141-
together with the [VirtusLabs pandas_stubs repository](https://github.com/VirtusLab/pandas-stubs).
140+
These stubs are the result of a strategic effort led by the core pandas team to integrate [Microsoft type stub repository](https://github.com/microsoft/python-type-stubs) with the [VirtusLabs pandas_stubs repository](https://github.com/VirtusLab/pandas-stubs).
142141

143-
These stubs were initially forked from the Microsoft project <https://github.com/microsoft/python-type-stubs> as of [this commit](https://github.com/microsoft/python-type-stubs/tree/6b800063bde687cd1846122431e2a729a9de625a).
142+
These stubs were initially forked from the Microsoft project at <https://github.com/microsoft/python-type-stubs> as of [this commit](https://github.com/microsoft/python-type-stubs/tree/6b800063bde687cd1846122431e2a729a9de625a).
144143

145-
We are indebted to Microsoft and that project for the initial set of public type stubs. We are also grateful for the original pandas-stubs project at <https://github.com/VirtusLab/pandas-stubs> that created the framework for testing the stubs.
144+
We are indebted to Microsoft and that project for providing the initial set of public type stubs. We are also grateful for the original pandas-stubs project at <https://github.com/VirtusLab/pandas-stubs>, which created the framework for testing the stubs.
146145

147146
## Differences between type declarations in pandas and pandas-stubs
148147

149148
The <https://github.com/pandas-dev/pandas/> project has type declarations for some parts of pandas, both for the internal and public API's. Those type declarations are used to make sure that the pandas code is _internally_ consistent.
150149

151-
The <https://github.com/pandas-dev/pandas-stubs/> project provides type declarations for the pandas _public_ API. The philsophy of these stubs can be found at <https://github.com/pandas-dev/pandas-stubs/blob/main/docs/philosophy.md/> While it would be ideal if the `pyi` files in this project would be part of the `pandas` distribution, this would require consistency between the internal type declarations and the public declarations, and the scope of a project to create that consistency is quite large. That is a long term goal. Finally, another goal is to do more frequent releases of the pandas-stubs than is done for pandas, in order to make the stubs more useful.
150+
The <https://github.com/pandas-dev/pandas-stubs/> project provides type declarations for the pandas _public_ API. The philosophy of these stubs can be found at <https://github.com/pandas-dev/pandas-stubs/blob/main/docs/philosophy.md/>. While it would be ideal if the `pyi` files in this project would be part of the `pandas` distribution, this would require consistency between the internal type declarations and the public declarations, and the scope of a project to create that consistency is quite large. That is a long term goal. Finally, another goal is to do more frequent releases of the pandas-stubs than is done for pandas, in order to make the stubs more useful.
152151

153-
If issues are found with the public stubs, pull requests to correct those issues are welcome. In addition, pull requests on the pandas repository to fix the same issue are welcome there as well. However, since the goals of typing in the two projects are different (internal consistency vs. public usage), it may be a challenge to create consistent type declarations across both projects. See <https://pandas.pydata.org/docs/development/contributing_codebase.html#type-hints/> for a discussion of typing standards used within the pandas code.
152+
If issues are found with the public stubs, pull requests to correct those issues are welcome. In addition, pull requests on the pandas repository to fix the same issue are welcome there as well. However, since the goals of typing in the two projects are different (internal consistency vs. public usage), it may be a challenge to create consistent type declarations across both projects. See <https://pandas.pydata.org/docs/development/contributing_codebase.html#type-hints> for a discussion of typing standards used within the pandas code.
154153

155154
## Getting help
156155

docs/philosophy.md

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# pandas-stubs Type Checking Philosophy
22

33
The goal of the pandas-stubs project is to provide type stubs for the public API
4-
that represent the recommended ways of using pandas. This is opposed to the
4+
that represent the recommended ways of using pandas. This is opposed to the
55
philosophy within the pandas source, as described [here](https://pandas.pydata.org/docs/development/contributing_codebase.html?highlight=typing#type-hints), which
66
is to assist with the development of the pandas source code to ensure type safety within
77
that source.
88

9-
Due to the methodology used by Microsoft to develop the original stubs, there are internal
9+
Due to the methodology used by Microsoft to develop the original stubs, there are internal
1010
classes, methods and functions that are annotated within the pandas-stubs project
1111
that are incorrect with respect to the pandas source, but that have no effect on type
1212
checking user code that calls the public API.
@@ -27,12 +27,12 @@ s = pd.Series([1, 2, 3])
2727
lt = s < 3
2828
```
2929

30-
In the pandas source, `lt` is a `Series` with a `dtype` of `bool`. In the pandas-stubs,
31-
the type of `lt` is `Series[bool]`. This allows further type checking to occur in other
30+
In the pandas source, `lt` is a `Series` with a `dtype` of `bool`. In the pandas-stubs,
31+
the type of `lt` is `Series[bool]`. This allows further type checking to occur in other
3232
pandas methods. Note that in the above example, `s` is typed as `Series[Any]` because
3333
its type cannot be statically inferred.
3434

35-
This also allows type checking for operations on series that contain date/time data. Consider
35+
This also allows type checking for operations on series that contain date/time data. Consider
3636
the following example that creates two series of datetimes with corresponding arithmetic.
3737

3838
```python
@@ -74,22 +74,22 @@ interval of `Timestamp`s.
7474
A set of (most likely incomplete) tests for testing the type stubs is in the pandas-stubs
7575
repository in the `tests` directory. The tests are used with `mypy` and `pyright` to
7676
validate correct typing, and also with `pytest` to validate that the provided code
77-
actually executes. The recent decision for Python 3.11 to include `assert_type()`,
77+
actually executes. The recent decision for Python 3.11 to include `assert_type()`,
7878
which is supported by `typing_extensions` version 4.2 and beyond makes it easier
79-
to test to validate the return types of functions and methods. Future work
79+
to test to validate the return types of functions and methods. Future work
8080
is intended to expand the use of `assert_type()` in the test code.
8181

8282
## Narrow vs. Wide Arguments
8383

84-
A consideration in creating stubs is too make the set of type annotations for
84+
A consideration in creating stubs is to make the set of type annotations for
8585
function arguments "just right", i.e.,
8686
not too narrow and not too wide. A type annotation to an argument to a function or
8787
method is too narrow if it disallows valid arguments. A type annotation to
8888
an argument to a function or method is too wide if
8989
it allows invalid arguments. Testing for type annotations that are too narrow is rather
90-
straightforward. It is easy to create an example for which the type checker indicates
90+
straightforward. It is easy to create an example for which the type checker indicates
9191
the argument is incorrect, and add it to the set of tests in the pandas-stubs
92-
repository after fixing the appropriate stub. However, testing for when type annotations
92+
repository after fixing the appropriate stub. However, testing for when type annotations
9393
are too wide is a bit more complicated.
9494
In this case, the test will fail when using `pytest`, but it is also desirable to
9595
have type checkers report errors for code that is expected to fail type checking.
@@ -100,14 +100,36 @@ Here is an example that illustrates this concept, from `tests/test_interval.py`:
100100
i1 = pd.Interval(
101101
pd.Timestamp("2000-01-01"), pd.Timestamp("2000-01-03"), closed="both"
102102
)
103-
if TYPE_CHECKING:
104-
i1 + pd.Timestamp("2000-03-03") # type: ignore
103+
if TYPE_CHECKING_INVALID_USAGE:
104+
i1 + pd.Timestamp("2000-03-03") # type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues]
105105

106106
```
107107

108108
In this particular example, the stubs consider that `i1` will have the type
109109
`pd.Interval[pd.Timestamp]`. It is incorrect code to add a `Timestamp` to a
110-
time-based interval. Without the `if TYPE_CHECKING` construct, the code would fail.
111-
However, it is also desirable to have the type checker pick up this failure, and by
112-
placing the `# type: ignore` on the line, an indication is made to the type checker
113-
that we expect this line to not pass the type checker.
110+
time-based interval. Without the `if TYPE_CHECKING_INVALID_USAGE` construct, the
111+
code would fail at runtime. Further, type checkers should report an error for this
112+
incorrect code. By placing the `# type: ignore[operator] # pyright: ignore[reportGeneralTypeIssues]`
113+
on the line, type checkers are told to ignore the type error. To ensure that the
114+
pandas-stubs annotations are not too wide (allow adding a `Timestamp` to a
115+
time-based interval), mypy and pyright are configured to report unused ignore
116+
statements.
117+
118+
## Using ignore comments
119+
120+
Type checkers report errors
121+
122+
- when writing negative tests to reject invalid behavior (inside a
123+
`TYPE_CHECKING_INVALID_USAGE` block),
124+
- when writing `overload`s that return incompatible return values, or
125+
- when type checkers have bugs themselves.
126+
127+
Since mypy and pyright behave slightly differently, we use separate ignore comments
128+
for them.
129+
130+
- If mypy reports an error, please use `# type: ignore[<error code>]`
131+
- If pyright reports an error, please use `# pyright: ignore[<error code>]`
132+
133+
If mypy and pyright report errors, for example, inside a `TYPE_CHECKING_INVALID_USAGE`
134+
block, please ensure that the comment for mypy comes first:
135+
`# type: ignore[<error code>] # pyright: ignore[<error code>]`.

docs/release_procedure.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
```shell
99
rm dist/*
10-
poetry pubish --build # you will get prompted for your pypi username/password
10+
poetry publish --build # you will get prompted for your pypi username/password
1111
git commit -a -m "Version a.b.c.yymmdd"
1212
git push upstream main
1313
git tag va.b.c.yymmdd

docs/tests.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Here are the most important options. Fore more details, please use `poe --help`.
1111
- Run only pytest: `poe pytest`
1212
- Run only pre-commit: `poe style`
1313
- Run tests against the installed stubs (this will install and uninstall the stubs): `poe test_dist`
14-
- Optional: run pytest against pandas nightly (this might fail): `poe pytest --nightly`
15-
- Optional: Run stubtest to compare the installed pandas-stubs against pandas (this will fail): `poe stubtest`. If you have created an allowlist to ignore certain errors: `poe stubtest path_to_the_allow_list`
1614

1715
These tests originally came from https://github.com/VirtusLab/pandas-stubs.
16+
17+
The following tests are **optional**. Some of them are run by the CI but it is okay if they fail.
18+
19+
- Run pytest against pandas nightly: `poe pytest --nightly`
20+
- Use mypy nightly to validate the annotations: `poe mypy --mypy_nightly`
21+
- Run stubtest to compare the installed pandas-stubs against pandas (this will fail): `poe stubtest`. If you have created an allowlist to ignore certain errors: `poe stubtest path_to_the_allow_list`

0 commit comments

Comments
 (0)