From 2971949944bebd413877abe8d6720ab375ea8c5b Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Mon, 13 May 2019 14:55:07 -0500 Subject: [PATCH 1/4] Fix type annotations in pandas.core.ops --- mypy.ini | 3 --- pandas/core/ops.py | 16 +++++++++++++++- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/mypy.ini b/mypy.ini index 54b2c59e1ba33..8ede269482f83 100644 --- a/mypy.ini +++ b/mypy.ini @@ -26,9 +26,6 @@ ignore_errors=True [mypy-pandas.core.internals.blocks] ignore_errors=True -[mypy-pandas.core.ops] -ignore_errors=True - [mypy-pandas.core.panel] ignore_errors=True diff --git a/pandas/core/ops.py b/pandas/core/ops.py index ae9ce5ea669e1..2747cf6e4acb8 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -6,8 +6,10 @@ import datetime import operator import textwrap +from typing import Dict, Optional import warnings +from mypy_extensions import TypedDict import numpy as np from pandas._libs import algos as libalgos, lib, ops as libops @@ -560,6 +562,18 @@ def _get_op_name(op, special): dtype: float64 """ +Operator_description = TypedDict( + 'Operator_description', + { + 'op': str, + 'desc': str, + 'reverse': Optional[str], + 'series_examples': Optional[str], + 'df_examples': Optional[str], + 'reversed': bool + }, + total=False) + _op_descriptions = { # Arithmetic Operators 'add': {'op': '+', @@ -625,7 +639,7 @@ def _get_op_name(op, special): 'desc': 'Greater than or equal to', 'reverse': None, 'series_examples': None} -} +} # type: Dict[str, Operator_description] _op_names = list(_op_descriptions.keys()) for key in _op_names: From d88bdb13cca56d46fde556519a1c926d96de075e Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Mon, 13 May 2019 16:31:03 -0500 Subject: [PATCH 2/4] Remove unsupported library --- pandas/core/ops.py | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 2747cf6e4acb8..e2985d556f0c3 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -6,10 +6,9 @@ import datetime import operator import textwrap -from typing import Dict, Optional +from typing import Dict, Optional, Union, cast import warnings -from mypy_extensions import TypedDict import numpy as np from pandas._libs import algos as libalgos, lib, ops as libops @@ -562,18 +561,6 @@ def _get_op_name(op, special): dtype: float64 """ -Operator_description = TypedDict( - 'Operator_description', - { - 'op': str, - 'desc': str, - 'reverse': Optional[str], - 'series_examples': Optional[str], - 'df_examples': Optional[str], - 'reversed': bool - }, - total=False) - _op_descriptions = { # Arithmetic Operators 'add': {'op': '+', @@ -639,16 +626,21 @@ def _get_op_name(op, special): 'desc': 'Greater than or equal to', 'reverse': None, 'series_examples': None} -} # type: Dict[str, Operator_description] +} # type: Dict[str, Dict[str, Optional[Union[bool, str]]]] + +# When TypedDict becomes available, this annotation would be much better and +# more readable if defined using that structure. The casts() below would not be +# necessary, because only the dictionary values keyed to 'reversed' would be +# typed as bool. See GH#26377. _op_names = list(_op_descriptions.keys()) for key in _op_names: _op_descriptions[key]['reversed'] = False reverse_op = _op_descriptions[key]['reverse'] if reverse_op is not None: - _op_descriptions[reverse_op] = _op_descriptions[key].copy() - _op_descriptions[reverse_op]['reversed'] = True - _op_descriptions[reverse_op]['reverse'] = key + _op_descriptions[cast(str, reverse_op)] = _op_descriptions[key].copy() + _op_descriptions[cast(str, reverse_op)]['reversed'] = True + _op_descriptions[cast(str, reverse_op)]['reverse'] = key _flex_doc_SERIES = """ Return {desc} of series and other, element-wise (binary operator `{op_name}`). From b031c878d2c0e4c980314246ae4412c7363bb9ab Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Tue, 14 May 2019 16:18:36 -0500 Subject: [PATCH 3/4] Refactor out reversed flag --- pandas/core/ops.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index e2985d556f0c3..60d48f4406bc6 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -6,7 +6,7 @@ import datetime import operator import textwrap -from typing import Dict, Optional, Union, cast +from typing import Dict, Optional import warnings import numpy as np @@ -626,7 +626,7 @@ def _get_op_name(op, special): 'desc': 'Greater than or equal to', 'reverse': None, 'series_examples': None} -} # type: Dict[str, Dict[str, Optional[Union[bool, str]]]] +} # type: Dict[str, Dict[str, Optional[str]]] # When TypedDict becomes available, this annotation would be much better and # more readable if defined using that structure. The casts() below would not be @@ -635,12 +635,10 @@ def _get_op_name(op, special): _op_names = list(_op_descriptions.keys()) for key in _op_names: - _op_descriptions[key]['reversed'] = False reverse_op = _op_descriptions[key]['reverse'] if reverse_op is not None: - _op_descriptions[cast(str, reverse_op)] = _op_descriptions[key].copy() - _op_descriptions[cast(str, reverse_op)]['reversed'] = True - _op_descriptions[cast(str, reverse_op)]['reverse'] = key + _op_descriptions[reverse_op] = _op_descriptions[key].copy() + _op_descriptions[reverse_op]['reverse'] = key _flex_doc_SERIES = """ Return {desc} of series and other, element-wise (binary operator `{op_name}`). @@ -1016,7 +1014,7 @@ def _make_flex_doc(op_name, typ): op_name = op_name.replace('__', '') op_desc = _op_descriptions[op_name] - if op_desc['reversed']: + if op_name.startswith('r'): equiv = 'other ' + op_desc['op'] + ' ' + typ else: equiv = typ + ' ' + op_desc['op'] + ' other' From a1f29fd80430e7478098df70bfedc05830151f7f Mon Sep 17 00:00:00 2001 From: Gregory Rome Date: Wed, 15 May 2019 07:34:08 -0500 Subject: [PATCH 4/4] Remove comment --- pandas/core/ops.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/pandas/core/ops.py b/pandas/core/ops.py index 60d48f4406bc6..0996eab4befa7 100644 --- a/pandas/core/ops.py +++ b/pandas/core/ops.py @@ -628,11 +628,6 @@ def _get_op_name(op, special): 'series_examples': None} } # type: Dict[str, Dict[str, Optional[str]]] -# When TypedDict becomes available, this annotation would be much better and -# more readable if defined using that structure. The casts() below would not be -# necessary, because only the dictionary values keyed to 'reversed' would be -# typed as bool. See GH#26377. - _op_names = list(_op_descriptions.keys()) for key in _op_names: reverse_op = _op_descriptions[key]['reverse']