Uh oh!
There was an error while loading. Please reload this page.
Add PEP 675 LiteralString overloads to str class - #7725
Conversation
JelleZijlstra
commented
Apr 27, 2022
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
56aab63Compare
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.
JelleZijlstra
commented
Apr 27, 2022
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). |
hauntsaninja
commented
Apr 27, 2022
@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? |
JelleZijlstra
commented
Apr 27, 2022
Oh sorry, thought it made it into 0.950! Then we'll definitely have to wait a bit longer. |
gbleaney
commented
May 25, 2022
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.
AlexWaygood
commented
May 25, 2022
Mypy 0.960 recognises |
AlexWaygood
commented
May 25, 2022
For both of these hits, mypy's logic for overloads and mypy's logic for higher-order functions is interacting badly. |
JelleZijlstra
commented
May 27, 2022
@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. |
gbleaney
commented
May 27, 2022
@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] |
JelleZijlstra
commented
Jun 1, 2022
Reproducer: fromtypingimportAnyStrdefescape(pattern: AnyStr) ->AnyStr: ...
CHAR: set[str] = {"x"}
"".join(map(escape, CHAR))
Repro: fromtypingimportAnydefsonos(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 |
gbleaney
commented
Jun 1, 2022
@JelleZijlstra |
JelleZijlstra
commented
Jun 1, 2022
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
strto 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
pyreon the following sample file:Prior to the changes, type errors were found (as expected):
After the changes, no errors were found (as expected):