Pylance attribute completion for typing.Union
expressions includes false positive attribute names
#4941
Replies: 3 comments
-
The current behavior is by design, not a bug. I think most users of pyright and pylance want to see attributes that are available for all subtypes within a union rather than the intersection of available attribute types. This is especially true for users who don't enable type checking diagnostics (which is the majority of people who use pylance). I'll also note that pyright's current behavior is consistent with language servers for other programming languages that support unions. Since this is a request for a language server feature, I'll leave it to the pylance team to make a final decision about what to do with it. They may close it or leave it open as a discussion topic for additional user input. @rchiodo or @debonte, could you transfer this to the pylance-release project? Thanks! |
Beta Was this translation helpful? Give feedback.
-
Converting this to a discussion item. If |
Beta Was this translation helpful? Give feedback.
-
I agree, I would appreciate to have that behavior configurable. I definitely see the point that some people would still like to see the union of attributes. I think this strongly depends on the degree to which a code base is typed. In partially/gradually typed code it makes sense to see all possible attributes. In completely typed code, however, where one might even have git hooks that prevent type-erroneous code from being merged, it makes hardly sense to complete attribute names that unconditionally imply type errors. However, I would favor the intersection semantic to be implemented by means of applying a filter on the current union result: Not all types are suitable for computation of comprehensive candidate sets to be intersected. Especially if we think of generic types with heavily overloaded |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Describebe the bug
Pylance's attribute completion for expressions whose annotated/inferred type is a
typing.Union
includes attribute names that pylance itself knows not to be valid for all types of the union.Code or Screenshots
Since
x
has typeA | B
, i.e.,isinstance(x, (A, B))
, the correct set ofx
's attributes isintersection(attrs(A), attrs(B))
, i.e.,{m   }
(plus dunder stuff). Pylance completion, however, offersa, b, m, <dunder stuff>
(see screenshot attached), i.e. seems to build the union of the available attributes. If one selectsx.a
, pylance immediately and correctly reports"error: Cannot access member "a" for type "B"
.It always feel a bit like being fooled in that situation: first, pylance is suggesting me some attribute names and if I decide to use one of its completion options, it turns red and tells me: WRONG!
Expected Behavior
Attribute completion of union types should consist of the intersection of attribute completion candidates of the union's elements. In the example above,
x.
should complete only to{m}
.VS Code extension or command-line
Pylance: v2023.10.10
in Visual Studio Code
Beta Was this translation helpful? Give feedback.
All reactions