pandas-dev / pandas-stubs

Public type stubs for pandas
BSD 3-Clause "New" or "Revised" License
234 stars 125 forks source link

`StringMethods.contains` should support `re.Pattern` in addition to `str` #988

Closed rowillia closed 2 months ago

rowillia commented 2 months ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce Code:

import pandas as pd
import re

s = pd.Series(['apple', 'banana', 'cherry', 'date', 'elderberry', 'fig'])
pattern1 = re.compile(r'a.e')
print(s.str.contains(pattern1))

Running the code works fine and as expected:

0    False
1    False
2    False
3     True
4    False
5    False
dtype: bool

Type checking we get an error:

$ pyright test.py
test.py
  test.py:6:22 - error: Argument of type "Pattern[str]" cannot be assigned to parameter "pat" of type "str" in function "contains"
    "Pattern[str]" is incompatible with "str" (reportArgumentType)
1 error, 0 warnings, 0 informations

Please complete the following information:

Dr-Irv commented 2 months ago

Thanks for the report and for doing a PR.