Skip to content

REGR: Don't ignore compiled patterns in replace #35697

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

Merged
merged 8 commits into from
Aug 17, 2020
Merged

REGR: Don't ignore compiled patterns in replace #35697

merged 8 commits into from
Aug 17, 2020

Conversation

dsaxton
Copy link
Member

@dsaxton dsaxton commented Aug 13, 2020

@simonjayhawkins simonjayhawkins added Regression Functionality that used to work in a prior pandas version Strings String extension data type and string data labels Aug 13, 2020
@simonjayhawkins simonjayhawkins added this to the 1.1.1 milestone Aug 13, 2020
@dsaxton
Copy link
Member Author

dsaxton commented Aug 13, 2020

Currently failing mypy and not quite sure of the fix. 5006980 did not work.

mypy --version
mypy 0.730
Performing static analysis using mypy
pandas/core/internals/managers.py:1952: error: Module has no attribute "Pattern"  [attr-defined]
Found 1 error in 1 file (checked 1030 source files)
Performing static analysis using mypy DONE
##[error]Process completed with exit code 1.

Copy link
Member

@arw2019 arw2019 left a comment

Choose a reason for hiding this comment

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

@dsaxton looked into that CI error you're getting.

xref python/typing#23

On my machine adding

from typing import Pattern

at the top of pandas/core/internals/managers.py and then using plain Pattern in the line you modify fixed the mypy complaint.

Hope this helps!

@dsaxton
Copy link
Member Author

dsaxton commented Aug 15, 2020

@dsaxton looked into that CI error you're getting.

xref python/typing#23

On my machine adding

from typing import Pattern

at the top of pandas/core/internals/managers.py and then using plain Pattern in the line you modify fixed the mypy complaint.

Hope this helps!

Awesome thanks @arw2019

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 @dsaxton generally lgtm

@@ -1949,7 +1959,7 @@ def _check_comparison_types(
else:
op = np.vectorize(
lambda x: bool(re.search(b, x))
if isinstance(x, str) and isinstance(b, str)
if isinstance(x, str) and isinstance(b, (str, Pattern))
Copy link
Member

Choose a reason for hiding this comment

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

b is typed as Scalar, where
Scalar = Union[PythonScalar, PandasScalar]

but b resolves to Union[builtins.str, builtins.float, Any] due to unresolved imports and hence mypy is green

but should probably update types and docstring anyway

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @simonjayhawkins. Would the proper type for b be Union[Scalar, Pattern], or should PythonScalar be extended to include Pattern?

Copy link
Member

Choose a reason for hiding this comment

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

I would keep PythonScalar unchanged for now.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated. Odd that b is typed as scalar but there's an isinstance check for np.ndarray inside the function, maybe that's not needed.

Copy link
Member

Choose a reason for hiding this comment

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

the docstring also implies b could be an array. I guess could add ArrayLike while here or leave. either works for me.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll add it. Actually a can also be a scalar so the type shouldn't only be ArrayLike I don't think:

In [17]: b
Out[17]: array(['a', 'a', 'a'], dtype='<U1')

In [18]: _compare_or_regex_search("a", b)
Out[18]: array([ True,  True,  True])

Copy link
Member

Choose a reason for hiding this comment

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

it looks like the types were originally this way when added, see also #32890 (comment), but changed in #33598

maybe best to keep the changes here minimal for now as this PR is being backported

(and separate PR for ensuring the types are correct and consistent with docstrings.)

@simonjayhawkins
Copy link
Member

simonjayhawkins commented Aug 15, 2020

Currently failing mypy and not quite sure of the fix. 5006980 did not work.

mypy --version
mypy 0.730
Performing static analysis using mypy
pandas/core/internals/managers.py:1952: error: Module has no attribute "Pattern"  [attr-defined]
Found 1 error in 1 file (checked 1030 source files)
Performing static analysis using mypy DONE
##[error]Process completed with exit code 1.

I think this is a mypy issue that is fixed in later mypy, but can't see a relevant issue on mypy issue tracker (probably a later typeshed)

@@ -1928,7 +1941,7 @@ def _compare_or_regex_search(
"""

def _check_comparison_types(
result: Union[ArrayLike, bool], a: ArrayLike, b: Scalar,
result: Union[ArrayLike, bool], a: ArrayLike, b: Union[Scalar, Pattern],
Copy link
Member

Choose a reason for hiding this comment

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

is a Pattern ever passed through to _check_comparison_types

Copy link
Member

Choose a reason for hiding this comment

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

I guess it's just for the error message

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 @dsaxton lgtm

@simonjayhawkins
Copy link
Member

not a regression, but I guess regex=True shouldn't be necessary when compiled regex is passed and if regex=False is explicitly passed should error.

@simonjayhawkins
Copy link
Member

Thanks @dsaxton merging and backporting this as addresses reported regression. followups can be left for 1.2.

@simonjayhawkins simonjayhawkins merged commit 934e9f8 into pandas-dev:master Aug 17, 2020
@simonjayhawkins
Copy link
Member

@meeseeksdev backport 1.1.x

meeseeksmachine pushed a commit to meeseeksmachine/pandas that referenced this pull request Aug 17, 2020
@simonjayhawkins simonjayhawkins added the replace replace method label Aug 17, 2020
simonjayhawkins pushed a commit that referenced this pull request Aug 17, 2020
@dsaxton dsaxton deleted the compiled-regex-replace branch August 17, 2020 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Regression Functionality that used to work in a prior pandas version replace replace method Strings String extension data type and string data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: Different results from Series.replace with compiled regex
3 participants