-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
TYP ensure bool_t is always used in pandas/core/generic.py #40175
Conversation
this looks fine, but im a bit wary of the amount of code being added |
Thanks @jbrockmendel - I share your concern, I just didn't find this to be feasible with a regular expression, and as pandas is using static typing more and more so I figured it'd be worthwhile to enforce using the correct type in that file. |
IIRC the alias is only needed after the bool method is defined and within the scope of the class definition.
possibly a VSCode bug? are you using pylance? I'm getting taken to the typing definitions at times instead of the code definition, but it's still in preview for now. |
shall we just remove |
see #32365 for a bit of history. maybe could move bool method to end of class definition to reduce bool_t usgaes. |
If I've understood correctly, then I'm only seeing this for class attributes, not for variable annotations. The bool method is defined on line 1486. $ mypy pandas
pandas/core/generic.py:2491: note: Revealed type is 'builtins.bool' Where we run into problems is if we put $ mypy pandas/core/generic.py
pandas/core/generic.py:1530: error: Function "pandas.core.generic.NDFrame.bool" is not valid as a type [valid-type]
pandas/core/generic.py:1530: note: Perhaps you need "Callable[...]" or a callback protocol?
Found 1 error in 1 file (checked 1 source file) However, pandas typically defines class attributes at the top of the class, before the various methods, so this wouldn't be an issue. And if it was, mypy would complain loudly (as above), it wouldn't let us assign a method as an annotation. So removing |
Sorry, I've done this "test" the wrong way around - if I try putting pandas/core/generic.py:9629: note: Perhaps you need "Callable[...]" or a callback protocol?
pandas/core/generic.py:10916: error: Function "pandas.core.generic.NDFrame.bool" is not valid as a type [valid-type] |
This works and is IMO the best solution $ mypy pandas
Success: no issues found in 1244 source files |
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.
Thanks @MarcoGorelli
Is this a documented solution? I would be slightly against doing this unless it is promoted as a fix by mypy |
Would you prefer to use bool_t everywhere in this file? I don't mind, it's having both bool and bool_t in the same file that I find confusing |
Yea I think using an alias is a better solution. Some background info here: These comments are a bit dated so maybe things have changed in the interim, but if not an alias is the suggested approach |
Does |
I'll check, but I don't think so |
It makes no difference - e.g.: # t.py
from __future__ import annotations
class Cat:
def bool(self): ...
def other_method(self, inplace: bool): ... results in $ mypy t.py
t.py:6: error: Function "t.Cat.bool" is not valid as a type [valid-type]
t.py:6: note: Perhaps you need "Callable[...]" or a callback protocol?
Found 1 error in 1 file (checked 1 source file) regardless of whether the future annotations are imported Anyway, have revisited and updated to use |
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.
lgtm. I think the pre-commit check is a little overkill but no objection to keeping
Thanks @MarcoGorelli |
Currently, lots of methods use the
bool
annotation in that file. I'm not sure it affectsmypy
(some experiments withreveal_type
suggest it doesn't) - however, it does confuse VSCode, which takes you to the methodbool
if you hover over one of these annotations.The file already contains
(added in #26024), but it's not always used