Skip to content
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

Unknown variable reported with .format but not with f-string? #5980

Closed
Holt59 opened this issue Sep 19, 2023 · 2 comments
Closed

Unknown variable reported with .format but not with f-string? #5980

Holt59 opened this issue Sep 19, 2023 · 2 comments
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working

Comments

@Holt59
Copy link

Holt59 commented Sep 19, 2023

Describe the bug

Using .format() instead of a format string shows a different behavior about an unknown variable type that does not make sense (to me).

Code or Screenshots
The code below shows:

Argument type is partially unknown
  Argument corresponds to parameter "args" in function "format"
  Argument type is "str | Unknown" Pylance(reportUnknownArgumentType)

...with .format(), but not with f-string.

from __future__ import annotations

class C:
    name: str
    outer: C | None

    def f(self) -> str:
        name = self.name
        oc = self.outer
        while oc is not None:
            # name = f"{oc.name}.{name}"
            name = "{}.{}".format(oc.name, name)
            oc = oc.outer

        return name

VS Code extension or command-line

Both:

  • VS Code extension, Pylance v2023.9.12.
  • pyright version 1.1.327
@Holt59 Holt59 added the bug Something isn't working label Sep 19, 2023
erictraut pushed a commit that referenced this issue Sep 19, 2023
…on when an overloaded function is called in a loop and one of the arguments to the call depends on the return value. This addresses #5980.
erictraut added a commit that referenced this issue Sep 19, 2023
…on when an overloaded function is called in a loop and one of the arguments to the call depends on the return value. This addresses #5980. (#5983)

Co-authored-by: Eric Traut <erictr@microsoft.com>
@erictraut
Copy link
Collaborator

Thanks for the bug report. This is due to a bug that occurs when an overloaded function (such as format) is called within a loop and the narrowed type of an argument (name) depends on the return type of that call. It doesn't affect f-string type evaluations because there are no overloaded calls involved in that case.

This will be addressed in the next release.

@erictraut erictraut added the addressed in next version Issue is fixed and will appear in next published version label Sep 19, 2023
@erictraut
Copy link
Collaborator

This is addressed in pyright 1.1.328, which I just published. It will also be included in a future release of pylance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addressed in next version Issue is fixed and will appear in next published version bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants