tests: Add backcompat for time_unit#3098
Conversation
FBruzzesi
left a comment
There was a problem hiding this comment.
Thanks @dangotbanned, I left a couple of non-blocking comments
| if _CONSTRUCTOR_FIXTURE_NAMES.isdisjoint(request.fixturenames): # pragma: no cover | ||
| msg = ( | ||
| f"`time_unit_compat` requires the test function to use a `constructor*` fixture.\n" | ||
| f"Hint:\n\n" | ||
| f"Try adding one of these as a parameter:\n {sorted(_CONSTRUCTOR_FIXTURE_NAMES)!r}" | ||
| ) | ||
| raise NotImplementedError(msg) |
There was a problem hiding this comment.
I am having a brain fart - what's happening here?
IIUC it's a way to avoid using this function outside a test that has a constructor. I must say that the "hint" was not very clear to me 🙈
There was a problem hiding this comment.
I am having a brain fart - what's happening here?
😂
So pretty recently I added a constructor_pandas_like fixture and learned a bit more pytest 😄
So the first check is to make sure the test function which request: pytest.FixtureRequest came from has got one of these 3 as a parameter
Lines 55 to 57 in 3b3b481
If it does, then request.node.callspec.id ...
Lines 240 to 241 in 3b3b481
... will be this string ...
timestamp_test.py::test_timestamp_datetimes_tz_aware[duckdb-ms-ms-expected8]'
# ^^^^^^^^^^^^^^^^^^^^^^... which gets the name from here
Lines 217 to 234 in 3b3b481
But why?
I don't know about you, but I've been bitten a few times by getting this pattern wrong
narwhals/tests/expr_and_series/dt/timestamp_test.py
Lines 158 to 174 in 3b3b481
Mainly with
"pyarrow"unintentionally matching pandas-like- always forgetting how to select every/a subset of pandas 😞
There was a problem hiding this comment.
Bringing this all back ...
time_unit_compat should just require that you've written a test like any of these:
def test_concat_diagonal(
constructor: Constructor, request: pytest.FixtureRequest
) -> None:
def test_array_dunder(
request: pytest.FixtureRequest, constructor_eager: ConstructorEager
) -> None:Then once you're in the test, you can do this without even knowing about constructor 🤯
def test_array_dunder(
request: pytest.FixtureRequest, constructor_eager: ConstructorEager
) -> None:
replaced = time_unit_compat("us", request)There was a problem hiding this comment.
Then once you're in the test, you can do this without even knowing about
constructor🤯
It was specifically that part I was thinking might lead to someone hitting that error message
|
Thanks @dangotbanned, feel free to merge if you are happy with it |

What type of PR is this? (check all applicable)
Related issues
TimeUnitinpandas<2#3007 (comment)TimeUnitinpandas<2#3007Checklist
If you have comments or can explain your changes, please do so below
Will avoid introducing 22 new warnings in the other PR