Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this correct? It looks to me like the default dtype is the old-style one:
I think it should be
np.dtype[np.dtypes.StringDType()]
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently all the type stubs for dtypes are parametrized in terms of a scalar that subclasses
np.generic
, sonp.dtypes.StringDType
doesn't work. Neither doesstr
, because that's not a numpy scalar type either. The assumption that dtypes are parameterized in terms of the scalar type is baked in pretty deeply. For example, the type stub fornp.dtype
looks like:So I guess I'd need to have an alternate definition for
dtype
that isn't parametrized, specifically forStringDType
? Like I said in the PR description, I don't really know what I'm doing with the type stubs...Unfortunately I didn't realize this was an issue until pretty late in the game.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is a pretty fundamental issue it looks like.
I played around a bit trying to understand the situation.
And whether
np.dtype
can be used as a base:The class hierarchy for scalars (see https://numpy.org/neps/nep-0040-legacy-datatype-impl.html#numpy-scalars-and-type-hierarchy) is pretty deeply ingrained. There is no such hierarchy with
np.dtype
as the root I think.It's unclear to me what the desired solution here is, even leaving aside backwards compatibility. I am also unsure how much it matters - how many functions would we actually like to annotate to indicate that they take any numerical array and arrays with a
StringDtype
dtype?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to at least have type stubs for
np.strings
that say the functions in that namespace accept StringDType arrays. That doesn't work right now because fundamentally the type stubs assume all dtypes are parameterized by a scalar type.If you're curious about where the trouble is coming from, expand this details block:
This issue arises from trying to add new overloads for all the functions in
np.strings
that take string arrays. Right now, the overloads look like this:Where
U_co
is an alias for_ArrayLikeStr_co
:I'd like to make it so we have an overload like:
Where T_co is an alias for this, which I was hoping could work:
But mypy complains about the use of
_SupportsArray
:_SupportsArray
is defined in terms ofnp.dtype
and the type stub fornp.dtype
is defined to only accept a scalar type:where
So fundamentally numpy's type stubs assume that all dtypes have a scalar and it'll take a lot of surgery to break that assumption.
All that said, I think maybe the best way forward is to just bite the bullet and add a scalar type that interns a reference to a packed string entry. That should fix this problem in a way that doesn't require major surgery on the type stubs. I also suspect it will improve performance for operations that are sensitive to creating a scalar.
Unfortunately this may just have to be a known issue we ship it with.