-
Notifications
You must be signed in to change notification settings - Fork 768
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
Type narrowing when one of two fields may be None #5350
Comments
The behavior of pyright (the type checker upon which pylance is built) is as designed here, so this isn't a bug. Type narrowing is applied to individual symbols (in this case, If you want to learn more about type narrowing, refer to this documentation. You can change your simple example as follows: def a_or_b(a: int | None = None, b: int | None = None):
if not b:
assert a
return a + 5 In your "real" example, you can fix it by doing the following: def make_pylance_unhappy(self):
if self.a is None:
assert self.b is not None
return self.b + 5 |
Thanks for the swift response, I suspected as much. Just noting that I clicked "question" when creating this issue, so I never intended to report it as a bug. |
@rchiodo, this makes me wonder if we should have a "Question" option when creating new issues or perhaps just steer questions towards the discussions area. Or maybe we need a "question" label. |
There's actually a I wonder if we can just redirect that to the discussions tab? |
Sorry, I didn't phrase that well -- what I meant was, maybe we should remove it. |
The following example:
complains about: Operator "+" not supported for "None" Pylance (reportOptionalOperand). However, from the context it is clear that
a
cannot beNone
. Is there a way to get successful type narrowing in this case?FYI, my real use case is a bit more complex, using pydantic validators to ensure the input is valid. Something like this, as discussed here.
The text was updated successfully, but these errors were encountered: