Skip to content

Commit c426dbd

Browse files
authored
fix: test setter on df.columns with pyright, not mypy (#484)
1 parent 04cc404 commit c426dbd

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

pyproject.toml

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ types-pytz = ">= 2022.1.1"
3838
mypy = "0.991"
3939
pyarrow = ">=10.0.1"
4040
pytest = ">=7.1.2"
41-
pyright = ">=1.1.284"
41+
pyright = ">=1.1.286"
4242
poethepoet = "0.16.0"
4343
loguru = ">=0.6.0"
4444
pandas = "1.5.2"
@@ -171,6 +171,7 @@ strict_equality = true
171171
show_error_context = false
172172
show_column_numbers = false
173173
show_error_codes = true
174+
always_true = "IS_TYPE_CHECKER_MYPY"
174175

175176
[tool.pyright]
176177
typeCheckingMode = "strict"
@@ -192,3 +193,5 @@ reportUnusedVariable = false
192193
reportPrivateUsage = false
193194
# enable optional checks
194195
reportMissingModuleSource = true
196+
defineConstant = { IS_TYPE_CHECKER_MYPY = false }
197+

tests/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
TYPE_CHECKING_INVALID_USAGE: Final = TYPE_CHECKING
2323
WINDOWS = os.name == "nt" or "cygwin" in platform.system().lower()
2424
PD_LTE_15 = Version(pd.__version__) < Version("1.5.999")
25+
IS_TYPE_CHECKER_MYPY = True
2526

2627
lxml_skip = pytest.mark.skipif(
2728
sys.version_info >= (3, 11), reason="lxml is not available for 3.11 yet"

tests/test_frame.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from pandas._typing import Scalar
3939

4040
from tests import (
41+
IS_TYPE_CHECKER_MYPY,
4142
PD_LTE_15,
4243
TYPE_CHECKING_INVALID_USAGE,
4344
check,
@@ -1775,18 +1776,20 @@ def test_iloc_tuple() -> None:
17751776
def test_set_columns() -> None:
17761777
# GH 73
17771778
df = pd.DataFrame({"a": [1, 2, 3], "b": [0.0, 1, 1]})
1778-
# Next line should work, but it is a mypy bug
1779+
# Next lines should work, but it is a mypy bug
17791780
# https://github.com/python/mypy/issues/3004
1780-
# pyright doesn't need the ignore
1781-
df.columns = ["c", "d"] # type: ignore[assignment]
1782-
df.columns = [1, 2] # type: ignore[assignment]
1783-
df.columns = [1, "a"] # type: ignore[assignment]
1784-
df.columns = np.array([1, 2]) # type: ignore[assignment]
1785-
df.columns = pd.Series([1, 2]) # type: ignore[assignment]
1786-
df.columns = np.array([1, "a"]) # type: ignore[assignment]
1787-
df.columns = pd.Series([1, "a"]) # type: ignore[assignment]
1788-
df.columns = (1, 2) # type: ignore[assignment]
1789-
df.columns = (1, "a") # type: ignore[assignment]
1781+
# pyright accepts this, so we only type check for pyright,
1782+
# and also test the code with pytest
1783+
if (TYPE_CHECKING and not IS_TYPE_CHECKER_MYPY) or not TYPE_CHECKING:
1784+
df.columns = ["c", "d"]
1785+
df.columns = [1, 2]
1786+
df.columns = [1, "a"]
1787+
df.columns = np.array([1, 2])
1788+
df.columns = pd.Series([1, 2])
1789+
df.columns = np.array([1, "a"])
1790+
df.columns = pd.Series([1, "a"])
1791+
df.columns = (1, 2)
1792+
df.columns = (1, "a")
17901793

17911794

17921795
def test_frame_index_numpy() -> None:

0 commit comments

Comments
 (0)