Skip to content

add type hint for core/computation/eval #26992

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

Closed
wants to merge 9 commits into from
Closed

add type hint for core/computation/eval #26992

wants to merge 9 commits into from

Conversation

xcz011
Copy link
Contributor

@xcz011 xcz011 commented Jun 21, 2019

@xcz011
Copy link
Contributor Author

xcz011 commented Jun 21, 2019

I am not fully sure about target variable type and return type. If anyone could take a look and give some feedback, that will be great. Thanks

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.

Looks good some comments

parser: str = 'pandas',
engine: str = None,
truediv: bool = True,
local_dict: dict = None,
Copy link
Member

Choose a reason for hiding this comment

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

Instead of using dict can you use the generic Dict from the typing module and add subscripts for the key / value types?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for this feedback. I looked through the where local_dict be used https://github.com/pandas-dev/pandas/blob/master/pandas/core/computation/scope.py#L103, but I cannot find out what kind of dict is this? [str, str] ? Thanks !

Copy link
Member

Choose a reason for hiding this comment

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

Hmm I'm not intimately familiar with this code but from what I gather should probably be Dict[str, Any] to simulate what can be injected into locals() and/or globals()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I will update soon !

@@ -89,7 +93,7 @@ def _check_resolvers(resolvers):
'the __getitem__ method'.format(name=name))


def _check_expression(expr):
def _check_expression(expr: Any) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Kind of vague from the docstring but looks like the type here should be str (Any should be avoided as it doesn't really offer much in the way of type checking)

@@ -107,7 +111,7 @@ def _check_expression(expr):
raise ValueError("expr cannot be an empty string")


def _convert_expression(expr):
def _convert_expression(expr: Any) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

I think also string here as well (inferring from actual usage rather than docstring)

@@ -136,7 +140,7 @@ def _convert_expression(expr):
return s


def _check_for_locals(expr, stack_level, parser):
def _check_for_locals(expr: Any, stack_level: int, parser: str) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Same here

global_dict: dict = None,
resolvers: Iterable = (),
level: int = 0,
target: Union[np.ndarry, pd.DataFrame, pd.Series] = None,
Copy link
Member

Choose a reason for hiding this comment

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

Typo on ndarray but this can also return a scalar right?

@WillAyd WillAyd added the Typing type annotations, mypy/pyright type checking label Jun 21, 2019
@pep8speaks
Copy link

pep8speaks commented Jun 22, 2019

Hello @xcz011! Thanks for updating this PR. We checked the lines you've touched for PEP 8 issues, and found:

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2019-07-31 02:58:32 UTC

@WillAyd WillAyd added this to the 0.25.0 milestone Jun 22, 2019
@codecov
Copy link

codecov bot commented Jun 22, 2019

Codecov Report

Merging #26992 into master will decrease coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #26992      +/-   ##
==========================================
- Coverage   91.99%   91.99%   -0.01%     
==========================================
  Files         180      180              
  Lines       50774    50777       +3     
==========================================
- Hits        46711    46710       -1     
- Misses       4063     4067       +4
Flag Coverage Δ
#multiple 90.63% <100%> (ø) ⬆️
#single 41.83% <100%> (-0.08%) ⬇️
Impacted Files Coverage Δ
pandas/core/computation/eval.py 97.16% <100%> (+0.08%) ⬆️
pandas/io/gbq.py 88.88% <0%> (-11.12%) ⬇️
pandas/core/frame.py 96.89% <0%> (-0.12%) ⬇️
pandas/util/testing.py 90.84% <0%> (-0.11%) ⬇️
pandas/core/dtypes/missing.py 93.93% <0%> (+0.6%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 171615a...8998a0b. Read the comment docs.

@codecov
Copy link

codecov bot commented Jun 22, 2019

Codecov Report

Merging #26992 into master will increase coverage by 0.3%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master   #26992     +/-   ##
=========================================
+ Coverage   91.68%   91.99%   +0.3%     
=========================================
  Files         182      180      -2     
  Lines       50246    50779    +533     
=========================================
+ Hits        46069    46713    +644     
+ Misses       4177     4066    -111
Flag Coverage Δ
#multiple 90.63% <100%> (-1.06%) ⬇️
#single 41.85% <83.33%> (?)
Impacted Files Coverage Δ
pandas/core/computation/eval.py 97.22% <100%> (+0.13%) ⬆️
pandas/io/excel/_openpyxl.py 84.71% <0%> (-3.23%) ⬇️
pandas/util/_decorators.py 92.13% <0%> (-3.22%) ⬇️
pandas/core/dtypes/cast.py 90.72% <0%> (-2.34%) ⬇️
pandas/io/formats/printing.py 86.72% <0%> (-2.11%) ⬇️
pandas/core/internals/blocks.py 94.38% <0%> (-1.56%) ⬇️
pandas/core/indexing.py 93.48% <0%> (-1.55%) ⬇️
pandas/core/dtypes/common.py 96.35% <0%> (-1.44%) ⬇️
pandas/core/arrays/categorical.py 95.92% <0%> (-1.14%) ⬇️
pandas/core/groupby/ops.py 96% <0%> (-0.92%) ⬇️
... and 164 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 143bc34...8a1ed2b. Read the comment docs.

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.

Comments on 3/4 CI failures. Not sure about last one regarding metaclass but may be related to python/mypy#730 if you can take a look

from pandas.core.computation.engines import _engines
from pandas.core.computation.scope import _ensure_scope

from pandas.io.formats.printing import pprint_thing


def _check_engine(engine):
def _check_engine(engine: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

engine needs to be Optional[str] since None is valid here (cause of one CI failure)

@@ -136,7 +140,7 @@ def _convert_expression(expr):
return s


def _check_for_locals(expr, stack_level, parser):
def _check_for_locals(expr: str, stack_level: int, parser: str) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

Can just add return None at the end of the body to appease CI

truediv: bool = True,
local_dict: Dict[str, Any] = None,
global_dict: Dict[str, Any] = None,
resolvers: Iterable = (),
Copy link
Member

Choose a reason for hiding this comment

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

I think this needs to be List[Dict] per documentation (may also help CI failure)

@@ -353,3 +366,5 @@ def eval(expr, parser='pandas', engine=None, truediv=True,
# We want to exclude `inplace=None` as being False.
if inplace is False:
return target if target_modified else ret
else:
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't need the else here

truediv: bool = True,
local_dict: List[Dict[str, Any]] = None,
global_dict: List[Dict[str, Any]] = None,
resolvers: Tuple = (),
Copy link
Member

Choose a reason for hiding this comment

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

So does this only accept a Tuple of Dict? If so we want to add Tuple[Dict[?, ?], ...] as the annotation (replacing ? with appropriate types) and update the docstring which currently suggests a list is appropriate

@@ -80,7 +84,7 @@ def _check_parser(parser):
' {valid}'.format(parser=parser, valid=_parsers.keys()))


def _check_resolvers(resolvers):
def _check_resolvers(resolvers: Iterable) -> None:
Copy link
Member

Choose a reason for hiding this comment

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

If it's possible we would want to specify the type of object within container classes, so Iterable[<type>] if it can be inferred here

@WillAyd
Copy link
Member

WillAyd commented Jul 1, 2019

Removing from 0.25 milestone - @xcz011 if you can merge master and address comments can keep with this though

@WillAyd WillAyd removed this from the 0.25.0 milestone Jul 1, 2019
@xcz011
Copy link
Contributor Author

xcz011 commented Jul 31, 2019

Removing from 0.25 milestone - @xcz011 if you can merge master and address comments can keep with this though

Sorry about leaving this open, I think I will try to finish this soon. Thanks

@WillAyd
Copy link
Member

WillAyd commented Aug 26, 2019

Can you merge master? Couldn't see old build failures

@jreback
Copy link
Contributor

jreback commented Sep 8, 2019

closing as stale, if you'd like to continue pls ping to reopen

@jreback jreback closed this Sep 8, 2019
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.

Annotate DataFrame (API exposed items from pandas.core.api)
4 participants