Skip to content
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

Merged
merged 9 commits into from
Apr 8, 2021

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Mar 2, 2021

  • Ensure all linting tests pass, see here for how to run them

Currently, lots of methods use the bool annotation in that file. I'm not sure it affects mypy (some experiments with reveal_type suggest it doesn't) - however, it does confuse VSCode, which takes you to the method bool if you hover over one of these annotations.

The file already contains

bool_t = bool  # Need alias because NDFrame has def bool:

(added in #26024), but it's not always used

@jbrockmendel
Copy link
Member

this looks fine, but im a bit wary of the amount of code being added

@MarcoGorelli
Copy link
Member Author

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.
No objection to closing if you think it's not worth it though

@simonjayhawkins
Copy link
Member

The file already contains

bool_t = bool  # Need alias because NDFrame has def bool:

(added in #26024), but it's not always used

IIRC the alias is only needed after the bool method is defined and within the scope of the class definition.

however, it does confuse VSCode

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.

@MarcoGorelli MarcoGorelli marked this pull request as draft March 3, 2021 10:13
@MarcoGorelli
Copy link
Member Author

MarcoGorelli commented Mar 3, 2021

shall we just remove bool_t = bool and use bool consistently then? (I think this'd be my preferred solution, am just seeing where it's really needed)

@simonjayhawkins
Copy link
Member

see #32365 for a bit of history.

maybe could move bool method to end of class definition to reduce bool_t usgaes.

@MarcoGorelli
Copy link
Member Author

MarcoGorelli commented Mar 3, 2021

IIRC the alias is only needed after the bool method is defined and within the scope of the class definition.

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.
If on line 2491 I put reveal_type(lines) (lines was annotated as bool_t), then we get

$ mypy pandas
pandas/core/generic.py:2491: note: Revealed type is 'builtins.bool'

Where we run into problems is if we put bar: bool after def: bool. Then we get

$ 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 bool_t = bool and just using bool seems fine to me

@MarcoGorelli
Copy link
Member Author

Sorry, I've done this "test" the wrong way around - if I try putting bool instead of bool_t in the annotations, then we get

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]

@MarcoGorelli
Copy link
Member Author

maybe could move bool method to end of class definition to reduce bool_t usgaes.

This works and is IMO the best solution

$ mypy pandas
Success: no issues found in 1244 source files

@MarcoGorelli MarcoGorelli changed the title TYP use bool_t instead of bool in pandas/core/generic.py TYP use bool instead of bool_t in pandas/core/generic.py Mar 3, 2021
@MarcoGorelli MarcoGorelli marked this pull request as ready for review March 3, 2021 11:31
@gfyoung gfyoung added Dtype Conversions Unexpected or buggy dtype conversions Typing type annotations, mypy/pyright type checking labels Mar 4, 2021
Copy link
Member

@simonjayhawkins simonjayhawkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @MarcoGorelli

@simonjayhawkins simonjayhawkins removed the Dtype Conversions Unexpected or buggy dtype conversions label Mar 14, 2021
@simonjayhawkins simonjayhawkins added this to the 1.3 milestone Mar 14, 2021
@WillAyd
Copy link
Member

WillAyd commented Mar 15, 2021

Is this a documented solution? I would be slightly against doing this unless it is promoted as a fix by mypy

@MarcoGorelli
Copy link
Member Author

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

@WillAyd
Copy link
Member

WillAyd commented Mar 16, 2021

Yea I think using an alias is a better solution. Some background info here:

#26029 (comment)

These comments are a bit dated so maybe things have changed in the interim, but if not an alias is the suggested approach

@MarcoGorelli MarcoGorelli marked this pull request as draft March 18, 2021 09:02
@jbrockmendel
Copy link
Member

Does from __future__ import annotations affect this at all?

@MarcoGorelli
Copy link
Member Author

I'll check, but I don't think so

@MarcoGorelli MarcoGorelli marked this pull request as ready for review April 4, 2021 09:15
@MarcoGorelli
Copy link
Member Author

Does from __future__ import annotations affect this at all?

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 bool_t everywhere in this file

Copy link
Member

@WillAyd WillAyd left a 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

@simonjayhawkins simonjayhawkins merged commit 2196004 into pandas-dev:master Apr 8, 2021
@simonjayhawkins
Copy link
Member

Thanks @MarcoGorelli

@simonjayhawkins simonjayhawkins changed the title TYP use bool instead of bool_t in pandas/core/generic.py TYP ensure bool_t is always used in pandas/core/generic.py Apr 8, 2021
@MarcoGorelli MarcoGorelli deleted the fixup-bool branch April 8, 2021 14:59
JulianWgs pushed a commit to JulianWgs/pandas that referenced this pull request Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Typing type annotations, mypy/pyright type checking
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants