From 6e8bafe2b57fd5c3559d9260d612fa10888dd404 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Tue, 13 Oct 2020 15:26:40 -0500 Subject: [PATCH 1/8] TYP: resolve typing errors for mypy==0.790 --- environment.yml | 2 +- pandas/core/aggregation.py | 4 ++-- pandas/core/computation/parsing.py | 3 +-- pandas/core/groupby/ops.py | 4 ++-- pandas/io/common.py | 6 +++--- requirements-dev.txt | 2 +- 6 files changed, 10 insertions(+), 11 deletions(-) diff --git a/environment.yml b/environment.yml index 77a9c5fd4822d..298652bd9e3e8 100644 --- a/environment.yml +++ b/environment.yml @@ -20,7 +20,7 @@ dependencies: - flake8 - flake8-comprehensions>=3.1.0 # used by flake8, linting of unnecessary comprehensions - isort>=5.2.1 # check that imports are in the right order - - mypy=0.782 + - mypy=0.790 - pre-commit - pycodestyle # used by flake8 - pyupgrade diff --git a/pandas/core/aggregation.py b/pandas/core/aggregation.py index ba7638e269fc0..69e86d0b85ea1 100644 --- a/pandas/core/aggregation.py +++ b/pandas/core/aggregation.py @@ -480,7 +480,7 @@ def transform_dict_like( if obj.ndim != 1: # Check for missing columns on a frame - cols = sorted(set(func.keys()) - set(obj.columns)) + cols = set(func.keys()) - set(obj.columns) if len(cols) > 0: raise SpecificationError(f"Column(s) {cols} do not exist") @@ -603,7 +603,7 @@ def aggregate(obj, arg: AggFuncType, *args, **kwargs): if isinstance(selected_obj, ABCDataFrame) and len( selected_obj.columns.intersection(keys) ) != len(keys): - cols = sorted(set(keys) - set(selected_obj.columns.intersection(keys))) + cols = set(keys) - set(selected_obj.columns.intersection(keys)) raise SpecificationError(f"Column(s) {cols} do not exist") from pandas.core.reshape.concat import concat diff --git a/pandas/core/computation/parsing.py b/pandas/core/computation/parsing.py index 86e125b6b909b..a0643b6b3771a 100644 --- a/pandas/core/computation/parsing.py +++ b/pandas/core/computation/parsing.py @@ -36,9 +36,8 @@ def create_valid_python_identifier(name: str) -> str: # toke.tok_name contains a readable description of the replacement string. special_characters_replacements = { char: f"_{token.tok_name[tokval]}_" - # The ignore here is because of a bug in mypy that is resolved in 0.740 for char, tokval in ( - tokenize.EXACT_TOKEN_TYPES.items() # type: ignore[attr-defined] + tokenize.EXACT_TOKEN_TYPES.items() ) } special_characters_replacements.update( diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index bca71b5c9646b..86728546f3092 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -267,8 +267,8 @@ def groups(self) -> Dict[Hashable, np.ndarray]: return self.groupings[0].groups else: to_groupby = zip(*(ping.grouper for ping in self.groupings)) - to_groupby = Index(to_groupby) - return self.axis.groupby(to_groupby) + index = Index(to_groupby) + return self.axis.groupby(index) @cache_readonly def is_monotonic(self) -> bool: diff --git a/pandas/io/common.py b/pandas/io/common.py index c147ae9fd0aa8..6d897c6fe1004 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -448,9 +448,9 @@ def infer_compression( if compression in _compression_to_extension: return compression - msg = f"Unrecognized compression type: {compression}" - valid = ["infer", None] + sorted(_compression_to_extension) - msg += f"\nValid compression types are {valid}" + infer = {"infer": None} + msg = f"Unrecognized compression type: {compression}\n" \ + f"Valid compression types are {_compression_to_extension} and {infer}" raise ValueError(msg) diff --git a/requirements-dev.txt b/requirements-dev.txt index 17ca6b8401501..b983c861253a1 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -11,7 +11,7 @@ cpplint flake8 flake8-comprehensions>=3.1.0 isort>=5.2.1 -mypy==0.782 +mypy==0.790 pre-commit pycodestyle pyupgrade From 21a3605ff36ed4d54fa7bb5f77bdb61ed0471b39 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Tue, 13 Oct 2020 15:28:11 -0500 Subject: [PATCH 2/8] reformat --- pandas/core/computation/parsing.py | 4 +--- pandas/io/common.py | 6 ++++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/computation/parsing.py b/pandas/core/computation/parsing.py index a0643b6b3771a..ab7c1a9f0cf23 100644 --- a/pandas/core/computation/parsing.py +++ b/pandas/core/computation/parsing.py @@ -36,9 +36,7 @@ def create_valid_python_identifier(name: str) -> str: # toke.tok_name contains a readable description of the replacement string. special_characters_replacements = { char: f"_{token.tok_name[tokval]}_" - for char, tokval in ( - tokenize.EXACT_TOKEN_TYPES.items() - ) + for char, tokval in (tokenize.EXACT_TOKEN_TYPES.items()) } special_characters_replacements.update( { diff --git a/pandas/io/common.py b/pandas/io/common.py index 6d897c6fe1004..9ebdf41d5cc1a 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -449,8 +449,10 @@ def infer_compression( return compression infer = {"infer": None} - msg = f"Unrecognized compression type: {compression}\n" \ - f"Valid compression types are {_compression_to_extension} and {infer}" + msg = ( + f"Unrecognized compression type: {compression}\n" + f"Valid compression types are {_compression_to_extension} and {infer}" + ) raise ValueError(msg) From b9d17844403526e9522f7e9a7953b38b6e850ccb Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Tue, 13 Oct 2020 23:30:18 -0500 Subject: [PATCH 3/8] TYP: ignore type-var error --- pandas/core/aggregation.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/core/aggregation.py b/pandas/core/aggregation.py index 69e86d0b85ea1..8be753d5396f5 100644 --- a/pandas/core/aggregation.py +++ b/pandas/core/aggregation.py @@ -480,7 +480,7 @@ def transform_dict_like( if obj.ndim != 1: # Check for missing columns on a frame - cols = set(func.keys()) - set(obj.columns) + cols = sorted(set(func.keys()) - set(obj.columns)) # type: ignore[type-var] if len(cols) > 0: raise SpecificationError(f"Column(s) {cols} do not exist") @@ -603,7 +603,9 @@ def aggregate(obj, arg: AggFuncType, *args, **kwargs): if isinstance(selected_obj, ABCDataFrame) and len( selected_obj.columns.intersection(keys) ) != len(keys): - cols = set(keys) - set(selected_obj.columns.intersection(keys)) + cols = sorted( + set(keys) - set(selected_obj.columns.intersection(keys)) + ) # type: ignore[type-var] raise SpecificationError(f"Column(s) {cols} do not exist") from pandas.core.reshape.concat import concat From 276d92715bb2266132a98699e554d790e18be7d4 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Wed, 14 Oct 2020 22:02:34 -0500 Subject: [PATCH 4/8] TYP: ignore operator error --- pandas/io/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 9ebdf41d5cc1a..de61aff29ea96 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -448,10 +448,11 @@ def infer_compression( if compression in _compression_to_extension: return compression - infer = {"infer": None} + # error: Unsupported operand types for + ("List[Optional[str]]" and "List[str]") [operator] + valid = ["infer", None] + sorted(_compression_to_extension) # type: ignore[operator] msg = ( f"Unrecognized compression type: {compression}\n" - f"Valid compression types are {_compression_to_extension} and {infer}" + f"Valid compression types are {valid}" ) raise ValueError(msg) From 33cc5554cf2ad6d84025f00dfe86a6342a7badba Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Wed, 14 Oct 2020 22:11:52 -0500 Subject: [PATCH 5/8] fix line length --- pandas/io/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index de61aff29ea96..268530576afe5 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -448,8 +448,9 @@ def infer_compression( if compression in _compression_to_extension: return compression - # error: Unsupported operand types for + ("List[Optional[str]]" and "List[str]") [operator] - valid = ["infer", None] + sorted(_compression_to_extension) # type: ignore[operator] + # Unsupported operand types for + ("List[Optional[str]]" and "List[str]") [operator] + valid = ["infer", None] + \ + sorted(_compression_to_extension) # type: ignore[operator] msg = ( f"Unrecognized compression type: {compression}\n" f"Valid compression types are {valid}" From acc47cc9449a67a58025046eba403f4204fa7e9c Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Wed, 14 Oct 2020 22:19:15 -0500 Subject: [PATCH 6/8] fix line length --- pandas/io/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index 268530576afe5..7aad1a532eb3e 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -449,8 +449,9 @@ def infer_compression( return compression # Unsupported operand types for + ("List[Optional[str]]" and "List[str]") [operator] - valid = ["infer", None] + \ - sorted(_compression_to_extension) # type: ignore[operator] + valid = ["infer", None] + sorted( + _compression_to_extension + ) # type: ignore[operator] msg = ( f"Unrecognized compression type: {compression}\n" f"Valid compression types are {valid}" From 0d0ed9408d9241232ba79b2698baf6493a616926 Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Sat, 9 Jan 2021 11:09:27 +0000 Subject: [PATCH 7/8] update --- pandas/io/common.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pandas/io/common.py b/pandas/io/common.py index b548cb04b13e1..12a886d057199 100644 --- a/pandas/io/common.py +++ b/pandas/io/common.py @@ -179,7 +179,8 @@ def stringify_path( # this function with convert_file_like=True to infer the compression. return cast(FileOrBuffer[AnyStr], filepath_or_buffer) - if isinstance(filepath_or_buffer, os.PathLike): + # Only @runtime_checkable protocols can be used with instance and class checks + if isinstance(filepath_or_buffer, os.PathLike): # type: ignore[misc] filepath_or_buffer = filepath_or_buffer.__fspath__() return _expand_user(filepath_or_buffer) @@ -487,7 +488,8 @@ def infer_compression( if compression in _compression_to_extension: return compression - # Unsupported operand types for + ("List[Optional[str]]" and "List[str]") [operator] + # https://github.com/python/mypy/issues/5492 + # Unsupported operand types for + ("List[Optional[str]]" and "List[str]") valid = ["infer", None] + sorted( _compression_to_extension ) # type: ignore[operator] From 6e501b3c92798b1ba68d3b31462a7e245e1fb5fe Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Sat, 9 Jan 2021 15:25:07 -0600 Subject: [PATCH 8/8] whatsnew doc --- doc/source/whatsnew/v1.3.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.3.0.rst b/doc/source/whatsnew/v1.3.0.rst index 1d76c9d296255..8156b71f4eeb8 100644 --- a/doc/source/whatsnew/v1.3.0.rst +++ b/doc/source/whatsnew/v1.3.0.rst @@ -88,7 +88,7 @@ If installed, we now require: +-----------------+-----------------+----------+---------+ | pytest (dev) | 5.0.1 | | | +-----------------+-----------------+----------+---------+ -| mypy (dev) | 0.782 | | | +| mypy (dev) | 0.790 | | X | +-----------------+-----------------+----------+---------+ For `optional libraries `_ the general recommendation is to use the latest version.