pandas-dev / pandas-stubs

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

fix(typing): #983 return type of StringMethods.match #990

Closed cmp0xff closed 2 months ago

cmp0xff commented 2 months ago
cmp0xff commented 2 months ago

Hi @Dr-Irv thank you for the comment. In https://github.com/pandas-dev/pandas-stubs/issues/983#issuecomment-2301853575 you mentioned:

The declaration of match() in core/strings.pyi is incorrect.

I partially fixed it.

How can I capture this expectation?

But to fix it, the StringMethods class will need an additional argument to pass in the expected result of match(), similar to what is done with str.split().

I do not get this suggestion. Could you elaborate?

PR with tests welcome

990

Dr-Irv commented 2 months ago

How can I capture this expectation?

But to fix it, the StringMethods class will need an additional argument to pass in the expected result of match(), similar to what is done with str.split().

I do not get this suggestion. Could you elaborate?

In core/strings.pyi, we have this:

# The _TS type is what is used for the result of str.split with expand=True
_TS = TypeVar("_TS", DataFrame, MultiIndex)

class StringMethods(NoNewAttributesMixin, Generic[T, _TS]):

You need to create an additional TypeVar, let's call it _MR (for match result). Then the Generic part of StringMethods adds _MR to the parameters of Generic.

Then the match() method would return _MR.

Then you change the references to StringMethods in core/series.pyi and core/indexes/base.pyi to pass in the correct type of result you want match to return as the 3rd argument.

See the pattern used with split() - that's how we control the return type of that particular method.

I'll let you address that change before firing off the CI.

cmp0xff commented 2 months ago

Hi @Dr-Irv , thanks for the explanations. I have made the changes.