Pylance doesn't recognize attrs.field with converters

66 Views Asked by At

Pylance seems to recognize an attrs field as a function (rather than a variable) when the paramter converter= is specified. Am I doing right and how to correct it? (Python v3.8.13, Pylance v2023.12.100)

"""
Why does Pylance recognize Test.x as a function (rather than a variable)?

"""
from typing import Optional

from attrs import define, field


def asstr(a: Optional[str]) -> str:
    """Return '' if a is None."""
    return "" if a is None else a


@define
class Test:
    """Test class"""

    x: str = field(converter=asstr)

    def test(self):
        """???"""
        self.x # Pylance: (function) x: str

Here's what Pylance displays!!!

1

There are 1 best solutions below

0
j-hil On

For a temporary fix you might want to try rolling back your version of Pylance to 2023.6.30 which seems to be the latest version of Pylance where this wasn't a bug. This worked for me, but introduced other worse bugs so your millage may vary.

I've also raised it as a bug on the pylance GitHub here.