pyre-check icon indicating copy to clipboard operation
pyre-check copied to clipboard

Python stdlib `string.Formatter` doesn't understand `LiteralString`

Open dvarrazzo opened this issue 3 years ago • 6 comments

Hello,

I have tried to use pyre to introduce LiteralString in psycopg 3 codebase.

I could fix all the internal usage of LiteralString, however, several methods of string.Formatter might be considered returning LiteralString on LiteralString input.

See: https://github.com/psycopg/psycopg/blob/600d6dfaf1366d2eca4486f7686c57da88f2b807/psycopg/psycopg/sql.py#L248-L251

This is more likely a typeshed issue rather than pyre. However I don't know the relation between the two and how experimental the use of LiteralString is. So I'm leaving this mostly as a FYI and in case you will want to propose something to typeshed.

Thank you very much!

dvarrazzo avatar Jun 27 '22 21:06 dvarrazzo

Please see the related issue https://github.com/psycopg/psycopg/issues/323

The OP also opened a ticket to typeshed at https://github.com/python/typeshed/issues/8195

dvarrazzo avatar Jun 27 '22 22:06 dvarrazzo

Thanks for letting us know!

At this point my guess is that Pyre won't need to do anything except bump our vendored typeshed stubs once the problem is fixed in upstream repositories, we ought to work out-of-the box against correct stubs. Feel free to ping us once that has been done, and if there are still problems we can circle back and figure out why.

stroxler avatar Jun 28 '22 20:06 stroxler

https://github.com/python/typeshed/pull/8268 is landed. @stroxler and chance of a typeshed upgrade + pyre release so we can close out this issue?

gbleaney avatar Jul 19 '22 13:07 gbleaney

Would the typeshed upgrade allow "from typing import LiteralString" to work?

PEP 675 implies should.

As a side note, "import typing" and "from typing_extensions import LiteralString" seem to work.

PS: I only have a basic understanding of Python.

craigfrancis avatar Nov 09 '22 09:11 craigfrancis

from typing import LiteralString will work only if you are on Python 3.11 or higher. from typing_extensions import LiteralString works on all supported versions (3.7+).

JelleZijlstra avatar Nov 09 '22 20:11 JelleZijlstra

Ahh, thanks @JelleZijlstra, it does work on my local machine (with 3.11); and I assume https://pyre-check.org/play will be updated at some point in the future.

craigfrancis avatar Nov 10 '22 11:11 craigfrancis