pyright icon indicating copy to clipboard operation
pyright copied to clipboard

set in a dict flagged as partial unknown

Open TheDavidFactor opened this issue 1 year ago • 2 comments

Environment data

  • Pylance version: 2025.9.1
  • OS and version: WSL on Windows 11
wsl -v
WSL version: 2.3.24.0
Kernel version: 5.15.153.1-2
WSLg version: 1.0.65
MSRDC version: 1.2.5620
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.26200.6901
  • Python version (& distribution if applicable, e.g. Anaconda): 3.13.3

Code Snippet

test_dict: dict[str, str | set[str]] = {
    'test_set': set(),
}
test_dict['test_set'].add('value1')
Image

Repro Steps

No specific steps

Expected behavior

Not flag this as partially unknown

Actual behavior

flags this as partially unknown

Logs

N/A

TheDavidFactor avatar Oct 28 '25 14:10 TheDavidFactor

Pyright is working correctly here. The type of expression test_dict['test_set'] is str | set[str]. While set has a method add, str does not. That explains why you see a type error indicating that Attribute "add" is unknown. This error then causes a second error if you have the strict-mode reportUnknownMemberType check enabled.

To eliminate both errors, you'll need to fix the type error in your code. You can do this by using a TypedDict rather than a normal dict. Or you can use a type guard to verify that the value you access from test_dict['test_set'] is a set[str] rather than a str.

erictraut avatar Oct 28 '25 17:10 erictraut

Thank you for the response. Clearly I have more learning to do.

TheDavidFactor avatar Oct 28 '25 17:10 TheDavidFactor