-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
TYP: Remove Sequence[str] in type annotations #47232
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
Comments
Is this a problem because:
|
Mostly it is this:
So we document a method like |
Our normal runtime validation in cases that accept a list-like (but not in this case I think?) is here Lines 1073 to 1112 in df8acf4
we need a type that matches that (maybe need an extension) or the type would be either wider or narrower. wider doesn't alert users that they have passed the wrong type. narrower gives false positives. Because the normal practice is to use So it appears that if if the docs say However @Dr-Irv I think we had a similar discussion elsewhere and your preference is for narrower types? |
In case of Personally, I would start with testing which functions accept strings, then tighten the types of the (internal) functions that fail, and propagate the changes based on the mypy errors/call stack (in case of un-typed functions). Creating one type that matches as much but not too much will be difficult. |
This is not really a pandas issue. see python/typing#256 and https://mail.python.org/archives/list/[email protected]/thread/JSZ4Q7B7FBF5LBVG2V6AAHLCQJSMDEAK/#3275P4426ARXTSDBU72O4KJLCSMRD24E IMO leave it as it is and hope that in the future the Python type system can exclude types. |
My concern is that
Yes, I prefer the narrower types. I think that most users when they see "list-like" will pass a list anyway.
Looking through the code, whenever we used
I agree. See discussion in python/typing#256 for various proposals. As @simonjayhawkins pointed out, it's not a pandas issue, and we just have to decide whether to be too narrow or too wide. Right now, we are too wide.
Well, I have the PR #47233 that addresses it. |
At the very least, we could/should create a type alias |
I agree on creating the ListLike = Union[AnyArrayLike, List] I'm having a hard time coming up with something that is a I should note that for |
Not sure whether that is the case: in some cases, the internal code might explicitly raise because we didn't want to accept strings but the code would probably work with strings. If it is okay to change the code to work with strings, we don't have this typing issue :) |
I think that is a much more substantial change! |
pytype doesn't have this issue python/typing#256 (comment)
easier to change the type checker (or contribute that change to mypy/pyright) |
Yes, I agree with you there, but this issue has been around for so long in the typing community, we may have to wait a long time for mypy and pyright to do anything about it. |
Without a PEP, pyright/mypy are very unlikely to "fix" this behavior. Speculating/hoping: If all/most non-string Sequences have a method that str doesn't has, we could simply create our own protocol (Inherit from sequence and add a string-incompatible method). |
I don't see that as a big deal. (it's not a pandas issue) I assume the |
It's a big deal from a user of the public API. We can always use a narrower type in
Fair point because of Having said that, I think the error of doing |
Personally, I'm not a big fan of addressing one exception (str in Sequence[str]) and then creating a whitelist of accepted types. Seems like we are adding more exceptions. I would be fine having something like I think before going forward we should answer some of the following questions:
|
For I think the debate here is more about what we do internally in the code. For the PR's I submitted, narrowing the type to The other issue is more of a documentation one, where we are not necessarily precise in the meanings of |
The protocol idea to avoid str but include all "proper" Sequences (proper: that are compatible with its methods, unlike str) seems to gain more traction: hauntsaninja/useful_types#14 Could use that in pandas (and pandas-stubs) to avoid some lengthy unions. |
In various places, we use
Sequence[str]
as a type annotation. The problem is that a regular string also matchesSequence[str]
. So we need to look at the various places we useSequence[str]
and change the type accordingly. Maybe justList
orArrayLike
, dependent on the context.The text was updated successfully, but these errors were encountered: