-
-
Notifications
You must be signed in to change notification settings - Fork 294
Add some typing to astroid.inference
#1415
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d4d25e3
Fix typing issues related to ``util.Uninferable``
DanielNoord 39559df
Fix the assertion :)
DanielNoord 397911a
Review
DanielNoord 28c4a12
Review 2
DanielNoord ca659eb
Review
DanielNoord 06176b2
Oops
DanielNoord 004f48d
Add ignore
DanielNoord e1ff89c
Apply suggestions from code review
DanielNoord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
import functools | ||
import itertools | ||
import operator | ||
from typing import Any, Callable, Dict, Iterable, Optional | ||
from typing import Any, Callable, Dict, Iterable, Iterator, Optional, Union | ||
|
||
import wrapt | ||
|
||
|
@@ -824,7 +824,7 @@ def _to_literal(node: nodes.NodeNG) -> Any: | |
|
||
def _do_compare( | ||
left_iter: Iterable[nodes.NodeNG], op: str, right_iter: Iterable[nodes.NodeNG] | ||
) -> "bool | type[util.Uninferable]": | ||
) -> Union[bool, util.Uninferable]: | ||
""" | ||
If all possible combinations are either True or False, return that: | ||
>>> _do_compare([1, 2], '<=', [3, 4]) | ||
|
@@ -839,17 +839,17 @@ def _do_compare( | |
""" | ||
retval = None | ||
if op in UNINFERABLE_OPS: | ||
return util.Uninferable | ||
return util.Uninferable # type: ignore[return-value] | ||
op_func = COMPARE_OPS[op] | ||
|
||
for left, right in itertools.product(left_iter, right_iter): | ||
if left is util.Uninferable or right is util.Uninferable: | ||
return util.Uninferable | ||
return util.Uninferable # type: ignore[return-value] | ||
|
||
try: | ||
left, right = _to_literal(left), _to_literal(right) | ||
except (SyntaxError, ValueError, AttributeError): | ||
return util.Uninferable | ||
return util.Uninferable # type: ignore[return-value] | ||
|
||
try: | ||
expr = op_func(left, right) | ||
|
@@ -859,17 +859,18 @@ def _do_compare( | |
if retval is None: | ||
retval = expr | ||
elif retval != expr: | ||
return util.Uninferable | ||
return util.Uninferable # type: ignore[return-value] | ||
# (or both, but "True | False" is basically the same) | ||
|
||
assert retval is not None | ||
return retval # it was all the same value | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a type hint to the definition of |
||
|
||
|
||
def _infer_compare( | ||
self: nodes.Compare, context: Optional[InferenceContext] = None | ||
) -> Any: | ||
) -> Iterator[Union[nodes.Const, util.Uninferable]]: | ||
"""Chained comparison inference logic.""" | ||
retval = True | ||
retval: Union[bool, util.Uninferable] = True | ||
|
||
ops = self.ops | ||
left_node = self.left | ||
|
@@ -881,13 +882,13 @@ def _infer_compare( | |
try: | ||
retval = _do_compare(lhs, op, rhs) | ||
except AstroidTypeError: | ||
retval = util.Uninferable | ||
retval = util.Uninferable # type: ignore[assignment] | ||
break | ||
if retval is not True: | ||
break # short-circuit | ||
lhs = rhs # continue | ||
if retval is util.Uninferable: | ||
yield retval | ||
yield retval # type: ignore[misc] | ||
else: | ||
yield nodes.Const(retval) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.