-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add PEP 675 LiteralString overloads to str class #7725
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
Add PEP 675 LiteralString overloads to str class #7725
Conversation
Thanks! I think we'll have to upgrade our CI to mypy 0.950 first (which was released minutes ago) to make mypy "support" LiteralString (by treating it as an alias for str). Also, looks like a few Union and Tuple usages need to be modernized. |
This comment has been minimized.
This comment has been minimized.
7c6a5c7
to
56aab63
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Unfortunately the basic LiteralString support we added in mypy apparently doesn't work. We'll have to figure out what's wrong (python/mypy#12259). |
@JelleZijlstra I don't think the LiteralString aliasing made it into 0.950. Also I'm not sure I see the connection to the issue you link to, did you mean to link python/mypy#12554? |
Oh sorry, thought it made it into 0.950! Then we'll definitely have to wait a bit longer. |
Looks like a new version of mypy was released. Any chance we could bump this for review @JelleZijlstra? |
This comment has been minimized.
This comment has been minimized.
Mypy 0.960 recognises |
For both of these hits, mypy's logic for overloads and mypy's logic for higher-order functions is interacting badly. |
@gbleaney could you take a look at the CI failures and add Ideally we'd also fix the mypy-primer hits, though we can ignore them if they're due to mypy bugs we can't work around. Sometimes using overloads vs. constrained typevars makes a difference in that sort of thing. |
@JelleZijlstra and @AlexWaygood thanks for the suggestions! Might be a few days until I have the time to sit down at a computer, but I'm on it :) |
Diff from mypy_primer, showing the effect of this PR on open source code: aiohttp (https://github.com/aio-libs/aiohttp)
+ aiohttp/multipart.py:95: error: Argument 1 to "map" has incompatible type "Callable[[AnyStr], AnyStr]"; expected "Callable[[str], AnyStr]" [arg-type]
core (https://github.com/home-assistant/core)
+ homeassistant/components/sonos/media.py:180: error: List item 0 has incompatible type "Optional[str]"; expected "None" [list-item]
|
Reproducer: from typing import AnyStr
def escape(pattern: AnyStr) -> AnyStr: ...
CHAR: set[str] = {"x"}
"".join(map(escape, CHAR))
Repro: from typing import Any
def sonos(channel: str | None, radio_show: Any):
" • ".join(filter(None, [channel, radio_show])) These both seem like mypy getting the type context subtly wrong. I'm OK with merging, but it's likely that more people are going to run into this mypy bug. @gbleaney what do you think of dropping the LiteralString overload for |
@JelleZijlstra |
OK, then let's just merge this, as I can't think of a way to express the stubs that would avoid those mypy bugs. If the disruption for mypy users is bad enough, we may have to think of an alternative. |
This PR implements overloads on the
str
to supportLiteralString
, as suggested in PEP 675 (note that small changes have been made to fix some typos in the PEP as well as remove the__rmul__
overload).The changes were tested by running
pyre
on the following sample file:Prior to the changes, type errors were found (as expected):
After the changes, no errors were found (as expected):