-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Wrong LiteralString type set for the join(list[Uknown]) result #9603
Comments
Pyright is working as intended here, so I don't consider this a bug. The typeshed stubs declare class str(Sequence[str]):
...
@overload
def join(self: LiteralString, iterable: Iterable[LiteralString], /) -> LiteralString: ...
@overload
def join(self, iterable: Iterable[str], /) -> str: ... # type: ignore[misc] In your second example, the type of The Python typing spec doesn't currently provide guidance to type checkers about how to evaluate calls to overloaded functions. I recently wrote and submitted a proposed addition to the typing spec. I also recently gave a talk about this proposal in a typing meetup. If you'd like to view the presentation, refer to this thread. Once we get agreement on this proposal and it is officially accepted into the typing spec, I will update pyright to conform to the spec. Mypy and other type checkers will likewise need to be changed. I don't plan to make any changes to pyright's overload behaviors prior to this because it will result in unnecessary churn for pyright users if the proposal is changed before it is ratified. If the current draft of the proposal is ratified, your second example will evaluate to a return type of I'm going to close this issue for now because I don't plan to make any changes unless/until the spec is clarified. |
@erictraut: thanks for the detailed explanation. I see your point, but the problem is that in case of And in general that makes problems - my actual case was also with @overload
def foo(with_status: Literal[False] = ...) -> str:
...
@overload
def foo(with_status: bool = ...) -> tuple[str,int]:
...
def foo(with_status = False):
status = int(0)
output = ''.join([]) # Here we get LiteralString
if with_status:
return output, status
return output And then I've got:
Which is confusing, at least for me - as, yes, I could fix it by adding explicit return type to the actual |
Describe the bug
The result of join is improperly tagged as
LiteralString
even if the list contains (non-literal) strCode or Screenshots
pyright output:
First is correct, third is wrong but acceptable (should be LiteralString), but the second is the problem. For the
list[Unknown]
it should returnstr
as we have no idea if the list contains any non-literal strings (and it definitely does in this example).VS Code extension or command-line
I'm using Pylance, but I checked also on the pyright, version
1.1.391
The text was updated successfully, but these errors were encountered: