-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
chore[python]: Enforce strict type equality in mypy check #4058
Conversation
Note that the introduction of #3857 allows additional freedom that this change will revert sort of (had to add a couple of type ignores for this). I see the confusion on |
Codecov Report
@@ Coverage Diff @@
## master #4058 +/- ##
=======================================
Coverage 78.96% 78.96%
=======================================
Files 468 468
Lines 76982 76985 +3
=======================================
+ Hits 60785 60790 +5
+ Misses 16197 16195 -2
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
Python 3.7 tests fail because I used The only solution I see is for these instances to go back to pulling in types from the @stinodego, any other ideas? |
Thanks for looking into this! Two things. First, doing casts is usually a sign that something isn't typed properly somewhere up the chain. Can we properly type indexing into a DataFrame? That should be the goal of this exercise - to improve the type hints. Not to set all the flags to True. Second, a more direct answer to your question: I believe an alternative to result: list[int] = df[:, 0]
assert result == [1, 2, 3] Much more readable than |
ae28a69
to
5865e2a
Compare
I don't think we can, because we do not have a way to introspect the types of the dataframe on compilation time. We do not know in advance that a column named "amount" read from a csv file is a
So that will break, because the first line can't be verified at the mypy check (compilation time if you will). Hence if we know it must be an Is some of this ugly? Yes, a bit. But also unavoidable, unless we are willing to check dataframe dtypes at runtime, and offer an API in polars to support that. For instance, that you would be able to get column 0 by doing |
What if you make them strings? typing.cast("tuple[str, int]", df.row(-1)) == ("seafood", 1) That should be valid Python 3.7 code, and hopefully EDIT: this is also ugly, but you should be able to do from typing import TYPE_CHECKING
result = df[:, 0]
if TYPE_CHECKING:
cast(list[int], result)
assert result == [1, 2, 3] |
I don't see how this would be possible. The closest we can get is with generics, ie. A = TypeVar("A")
B = TypeVar("B")
C = TypeVar("C")
class DataFrame(Generic[A, B, C]):
def row(self, index: int) -> tuple[A, B, C]):
... but there is not yet a way to declare a variable number of generics (PEP-646 will get us closer, but that's due until Python 3.11). And even with PEP-646, we would still have to rely on def col(index: int) -> tuple[A] | tuple[B] | tuple[C]:
pass
df = pl.DataFrame[int, str, float]({"a": [1], "b": ["foo"], "c": [1.0]})
# MyPy doesn't know if this is an int, str, or float
df.col(1) == ("foo",)
# so we would still need to cast
cast(tuple[str], df.col(1)) == ("foo",) |
All right, so for now it seems like casting seems the way to go. Though I would prefer to do And I believe the suggestion by @matteosantama to stringify the types could work. Also wanted to mention that we could solve some of our problems by simply ignoring the warnings for unused ignores for problematic modules; something like: [[tool.mypy.overrides]]
module = "tests.*"
warn_unused_ignores = false |
hello. l have some problem, but l don npm ERR! A complete log of this run can be found in: C:\nfts\hashlips_art_engine-main (1)> |
@huijiong That looks unrelated to this PR -- I suggest you open an issue and explain exactly what you are trying to do that is causing the error. |
Should now be good to go. Had a bit of struggle with Py 3.7 vs 3.10, sometimes |
22f30a8
to
c91c829
Compare
c91c829
to
e1620ca
Compare
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 good to me! Nice, only one more flag to go 😄
In pola-rs#4058, I enforce strict type equality checks in mypy. Most cases can be fixed with `typing.cast`. For generic types such as tuple, we cannot use this yet, because py3.7 does not allow this, and thus we need a `type: ignore`. This one case of using the cast slipped through, fixing this here. As noted by pola-rs#4413 (comment)
Fixes mypy
strict_equality
in #4044.