-
Notifications
You must be signed in to change notification settings - Fork 770
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
Pylance flags a reportOptionalMemberAccess for an Optional item in a list even after a test for None #799
Comments
Pyright (the type checker that underlies pylance) does not perform type narrowing on expressions that contain subscripts. This is by design. There are just too many cases where the values of such expressions can change between the conditional test and the second usage of the expression without the type checker knowing. For example, if you check the first element of a list but then pop and push a value onto the list, the first element will now be different. Type checkers in other languages that offer type narrowing often make the same choice with respect to subscripts. For a full list of supported type guards (expressions that are narrowed in a conditional statement) in pyright, refer to this documentation. There is an easy workaround: assign the value of the subscripted expression to a local variable before the test. This not only allows for safe type narrowing, but it also makes the code faster and generally more readable. first_elem = some_list[0]
if first_elem is not None:
print(first_elem.upper()) |
It's been over a month since we marked this issue as waiting for info; if you believe the above fix to not be satisfactory we can reopen, but we do try and follow the typing rules as best as possible. Note that you can also use the walrus operator in newer versions of python to make the temp var bit a bit cleaner. See also microsoft/pyright#1494. |
Here is it advices to do following, check with
|
@avatar-lavventura, please don't comment on closed issues. If you think there is a bug present, please open a new issue, and make sure to provide a minimal, self-contained code sample. The sample you've provided above is not complete, so I can't tell what it's trying to demonstrate. |
I am not presenting any bug. I am just pointing out there is no control made if the |
Environment data
Expected behaviour
When using using a list that contains Optional types, no reportOptionalMemberAccess should be reported if the list item is checked to not be None before it's used.
Actual behaviour
When checking a list item to see if it's None using the index operator, a reportOptionalMemberAccess error occurs when trying to use use it from within an "if" block. However, if the list item is first assigned to a variable then checked for None, the error doesn't occur:
In cases 1 and 3 the error occurs, but not in cases 2 and 4. This only happens when the Pylance's Type Checking Mode is set to "strict".
Code Snippet / Additional information
The text was updated successfully, but these errors were encountered: