-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
PyRight doesn't narrow type based on hasattr
#6717
Comments
This isn't a supported type guard pattern, so no type narrowing is performed in this case. For a full list of supported type guard patterns, refer to this documentation. Each new type guard pattern requires specialized logic and potentially slows down overall type analysis. We do occasionally add support for new type guard patterns but only if we have strong signal that it's a common pattern. I don't think that In the meantime, the recommended approach is to use a "user-defined type guard" using the I'm going to close this feature request for now because we have no current plans to add support for this type guard pattern. |
I mean, But sure, I guess I can use a |
For comparison, mypy (another popular type checker) also does not support The common approach is to use def foo(h: list | float):
if isinstance(h, list) and h[0] == 0:
pass The documentation you quoted applies to runtime protocols. The types you used in your example ( |
Well, sure, but my actual use case would have liked to use |
I get that there may be reasons not to implement this, but this example is rather irritating def _pytest_passed(request: pytest.FixtureRequest) -> bool:
"""Returns True if the test(s) a fixture was used in passed."""
if hasattr(request.node, '_function_outcome'):
return request.node._function_outcome.outcome in {'passed', 'skipped'}
# ~~~~~~~~~~~~~~~~~
# Cannot access member "_function_outcome" for type "Item"
# Member "_function_outcome" is unknownPylancereportAttributeAccessIssue |
MyPy supports it at least partially. |
Similarly I was surprised that Having an attribute doesn't necessarily mean you got the expected type, but at least you know it can't be None (which has no attributes). |
Describe the bug
PyRight puts a red squiggle under
h
inh[0]
, saying ""__getitem__" method not defined on type "float
". While canonically determining whether an object is indexable in Python is difficult enough that the Python documentation itself says that the only way to be sure is to try it and find out, in this case PyRight requires one specific method to exist, so it should also be able to tell that I've guaranteed that the method exists earlier in the line. It would be extra-nice if it could ascribeh
to a duck type whose only known feature is that it has__getitem__
, but at the least in this context it should be able to infer that, between the optionslist
andfloat
,h
must be alist
. (Movingh[0] == 0
into a newif
doesn't help.)VS Code extension or command-line
PyLance v2023.11.10 in VSCode on WSL
The text was updated successfully, but these errors were encountered: