-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC, fix: use mypy
pre-commit in local environment
#1966
base: main
Are you sure you want to change the base?
Conversation
i like the look of this, thanks for doing it it looks like this fails in pre-commit-ci, so we may need to find another way of running it in ci? (i'm not averse to removing pre-commit-ci) |
Thanks @MarcoGorelli ! β€οΈ This could be a possible approach that doesn't change super much what we currently have:
The errors are now showing up in the CI! Happy to hear your thoughts (also @FBruzzesi and @dangotbanned ) |
nice, happy to go with this - if we can get it green and then gradually address things, happy to get this in |
This comment was marked as outdated.
This comment was marked as outdated.
@MarcoGorelli sounds reasonable Maybe this is a dumb question, but what is the issue with running |
that's fine as well. seeing as we're using pre-commit for other checks, i don't it hurt to have a |
My goal here was trying to find a better and more consistent way to run typing tests without disrupting too much the current workflow.
Here I meant that it seems that most of typing errors (we are not already ignoring) come from the Still testing a couple of things, hope to have this PR properly ready for review later today π€ |
@@ -33,7 +33,7 @@ | |||
from narwhals.utils import Version | |||
|
|||
|
|||
class DuckDBExpr(CompliantExpr["duckdb.Expression"]): | |||
class DuckDBExpr(CompliantExpr["duckdb.Expression"]): # type: ignore[type-var] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This ignore should actually be solved. duckdb.Expression
is not a CompatibleSeries
Is it ok for a follow-up?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup definitely
@@ -159,7 +159,7 @@ def select( | |||
): | |||
return self._from_native_frame( | |||
self._native_frame.aggregate( | |||
[val.alias(col) for col, val in new_columns_map.items()] | |||
[val.alias(col) for col, val in new_columns_map.items()] # type: ignore[arg-type] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
based on the duckdb stubs, only str
are allowed in .aggregate
. π€
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks wrong, it should be str | list[Expression]
, right? want to make a PR to duckdb?
"narwhals[tests]", | ||
"narwhals[typing]", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should separate the dev dependencies into tests
and typing
.
The latest pinned mypy
is available for python >=3.9 and if we simply include it in dev
our python 3.8 tests will fail.
@@ -58,7 +58,7 @@ | |||
from narwhals.typing import CompliantDataFrame | |||
from narwhals.typing import CompliantLazyFrame | |||
|
|||
CLASSICAL_NUMPY_DTYPES = frozenset( | |||
CLASSICAL_NUMPY_DTYPES: frozenset[np.dtype] = frozenset( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You might be able to get away with this?
Possibly an issue for 3.8
though
CLASSICAL_NUMPY_DTYPES: frozenset[np.dtype] = frozenset( | |
CLASSICAL_NUMPY_DTYPES = frozenset["np.dtype"]( |
`DuckDBPyRelation.cross` isn't in the stubs
This comment was marked as resolved.
This comment was marked as resolved.
@@ -60,23 +60,23 @@ def second(self: Self) -> DuckDBExpr: | |||
def millisecond(self: Self) -> DuckDBExpr: | |||
return self._compliant_expr._from_call( | |||
lambda _input: FunctionExpression("millisecond", _input) | |||
- FunctionExpression("second", _input) * 1_000, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've got a feeling they want to nudge us towards using * ConstantExpression(1000)
, then mypy accepts that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that was what I went for in (bf9e0d9)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@MarcoGorelli to clarify, that was only on _duckdb.expr
(not this module _duckdb.expr_dt
)
return ( | ||
FunctionExpression("var_pop", _input) | ||
* n_samples | ||
/ (n_samples - ConstantExpression(ddof)) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was just fixing a pyright
warning here.
If anyone has a meaningful name for the parts of this expression, I think it'd be more readable with an assignment first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Like:
n_samples = n_samples = FunctionExpression("count", _input)
meaningful = (n_samples - ConstantExpression(ddof)) # don't use this name!
return FunctionExpression("var_pop", _input) * n_samples / meaningful
The last warning seems legit to me https://github.com/narwhals-dev/narwhals/actions/runs/13240255809/job/36953842381?pr=1966 Both uses of narwhals/narwhals/_pandas_like/series.py Lines 1057 to 1067 in 4b1d778
I don't see any tests covering The one @camriddell just wanted to run this by you following #1859 - feel like I must be missing something |
What type of PR is this? (check all applicable)
Related issues
mypy
passing withpyarrow-stubs
installedΒ #1961Checklist
If you have comments or can explain your changes, please do so below
I did some digging, looking for how other projects run typing and general best practices with
mypy
.One thing people seem to agree with is that
mirrors-mypy
has its gotchas and may not be ideal for every project.In our case we would need to add every backend dependencies as
additional-dependencies
. In this case we would have to maintain a script that keep those dependencies up to date.One alternative would be to run mypy in the local environment where all the dependencies are already installed. This is what this PR is trying to do.
I have still some open questions, making this draft to test the CI too.~I am expecting to see
300 errors frommypy
πUpdate: I propose to move to this solution where we:
pre-commit
for linting but movemypy
to run in the local env -> this is to avoid having to add manyadditional-dependencies
make typing
mypy
in a separate CI pipeline on the entire code