microsoft / pyright

Static Type Checker for Python
Other
13.12k stars 1.4k forks source link

Incorrect "variable is not accessed" message #8807

Closed jdayton3 closed 2 weeks ago

jdayton3 commented 3 weeks ago

Environment data

Code Snippet

# pyright: strict
from functools import partial
from dataclasses import dataclass

@dataclass
class Dog:
    name: str
    breed: str

def partial_dog(breed: str):
    formatted_breed = breed.title() # error: Variable "formatted_breed" is not accessed

    return partial(
        Dog,
        breed=f"Maybe a {formatted_breed}", # but it's accessed right here
    )

if __name__ == "__main__":
    print(partial_dog("beagle")(name="Buddy"))
C:\Users\jdayton\source\temp> python .\repro.py   
Dog(name='Buddy', breed='Maybe a Beagle')

Expected behavior

When using a function-local variable in an f-string as a dataclass constructor argument in functools.partial, Pylance should recognize that the variable is being accessed.

Actual behavior

Pylance reports the function-local variable as unused.

We get the same error when running pyright (pyright==1.1.377, installed with pip) in the terminal:

C:\Users\jdayton\source\temp> pyright .\repro.py
c:\Users\jdayton\source\temp\repro.py
  c:\Users\jdayton\source\temp\repro.py:11:5 - error: Variable "formatted_breed" is not accessed (reportUnusedVariable)
1 error, 0 warnings, 0 informations

We also get this error if Dog is a pydantic BaseModel:

from pydantic import BaseModel # pydantic==2.7.4

class Dog(BaseModel):
    name: str
    breed: str

We don't get this error if Dog is a regular class:

class Dog:
    def __init__(self, name: str, breed: str):
        self.name = name
        self.breed = breed

We also don't get the error if formatted_breed is passed in directly, not in an f-string:

def partial_dog(breed: str):
    formatted_breed = f"Maybe a {breed.title()}"

    return partial(
        Dog,
        breed=formatted_breed,
    )

If the variable in the f-string is a parameter instead of a local variable, we don't get this error, but Pylance sometimes can't tell what its type is:

image

rchiodo commented 3 weeks ago

Transferring to Pyright, typechecking issue.

erictraut commented 3 weeks ago

Thanks for the bug report. This will be addressed in the next release of pyright.

erictraut commented 2 weeks ago

This is addressed in pyright 1.1.378.